下载 Apache windows 编译版
有两个网站提供 windows 环境的编译版下载
本文下载的版本:httpd-2.4.33-o110h-x64-vc14-r2
下载 vc15 运行环境
下载 PHP7.2.6
与 nginx 不一样,Apache 要下载线程安全版,即 ts 版。
php 最新版本下载列表页面
本文下载的,php7 针对 Apache 的版本:php-7.2.6-Win32-VC15-x64
配置 PHP7.2.6
将 PHP 解压至目录:c:/dev/php7TS
复制 php.ini-development 为 php.ini,修改配置。
以后如果需要修改 php.ini, 修改之后,重启 apache 服务即可生效。
常规配置
找到下面的配置项,并作相应的修改
1
2
3
4
5
6
7
8
9
10error_log = C:\dev\php7TS\logs\php_errors.log
extension_dir = "C:\dev\php7TS\ext"
date.timezone = "Asia/Shanghai"
cgi.fix_pathinfo=1
extension=curl
extension=gd2
extension=openssl
extension=mysqli
extension=pdo_mysql在 [opcache] 下面添加如下配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16; dll地址
zend_extension=php_opcache.dll
; 开关打开
opcache.enable=1
; 开启CLI
opcache.enable_cli=1
; 可用内存, 酌情而定, 单位为:Mb
opcache.memory_consumption=528
; Zend Optimizer + 暂存池中字符串的占内存总量.(单位:MB)
opcache.interned_strings_buffer=8
; 对多缓存文件限制, 命中率不到 100% 的话, 可以试着提高这个值
opcache.max_accelerated_files=10000
; Opcache 会在一定时间内去检查文件的修改时间, 这里设置检查的时间周期, 默认为 2, 定位为秒
opcache.revalidate_freq=1
; 打开快速关闭, 打开这个在PHP Request Shutdown的时候回收内存的速度会提高
opcache.fast_shutdown=1
配置 XDebug
下载列表,找到对应的版本,记得是针对 PHP 7.2 线程安全版(不带 nts)
下载之后,剪切至 PHP 的 ext 目录,在 php.ini 最后添加如下信息:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30[xdebug]
zend_extension = C:\dev\php7TS\ext\php_xdebug-2.6.0-7.2-vc15-x86_64.dll
;开启远程调试
xdebug.remote_enable=1
xdebug.profiler_enable=1
;xdebug.profiler_enable=off
xdebug.profiler_enable_trigger=off
xdebug.profiler_output_name=cachegrind.out.%t.%p
xdebug.profiler_output_dir="tmp"
xdebug.show_local_vars=0
xdebug.remote_connect_back=1
;开启自动跟踪
xdebug.auto_trace=1
;开启异常跟踪
xdebug.show_exception_trace=1
;开启异常跟踪
xdebug.remote_autostart=1
;收集变量
xdebug.collect_vars=1
;收集参数
xdebug.collect_params=1
;trace输出路径
xdebug.trace_output_dir="tmp/xdebug"
;以下三个分别是主机、端口、句柄
xdebug.remote_host="localhost"
;注意,这里的端口,在 nginx 环境需要手动运行 php-cgi,而php-cgi 的端口默认为 9000,所以这里需要配置 xdebug 为不同的端口。
;但在 Apache 环境,不需要运行 php-cgi,所以,不存在端口冲突,用 xdebug 的默认端口 9000 即可,这样也不用修改 PHPStorm 的配置。
;xdebug.remote_port=9001
xdebug.remote_handler="dbgp"
配置 Apache 支持 PHP
nginx 支持 PHP 需要先运行 php-cgi,而 Apache 只要配置好就可自动支持 php。
在 http.conf 文件中 LoadModule 的最后一行添加如下内容:1
2
3
4
5
6
7
8# added manual
Define PHPROOT C:/dev/php7TS
LoadModule php7_module ${PHPROOT}/php7apache2_4.dll
AddType application/x-httpd-php .php .html .htm
PHPIniDir ${PHPROOT}
LoadFile ${PHPROOT}/libssh2.dll
#LoadFile ${PHPROOT}/libssl-1_1-x64.dll #网上解决 cURL 库无法开启有这一行,但是会报错(httpd.exe: Syntax error on line 191 of C:/dev/Apache24/conf/httpd.conf: Cannot load C:/dev/php7TS/libssl-1_1-x64.dll into server:),注释掉就好了。
配置 Apache 的虚拟主机
找到下面的配置,去掉前面的 #,配置虚拟主机需要用到 httpd-vhosts.conf1
Include conf/extra/httpd-vhosts.conf
修改 httpd-vhosts.conf,添加如下内容
建议:将 php 项目都放到变量 devProjectRoot 指定的目录下,这个目录下的子目录不用单独配置访问权限,否则需要单独配置目录访问权限。1
2
3
4
5
6
7
8
9
10
11
12
13Define devProjectRoot "C:/workspace/php"
<Directory "${devProjectRoot}">
AllowOverride None
Require all granted
</Directory>
<VirtualHost *:80>
DocumentRoot "${devProjectRoot}/dz"
ServerName dz.dev.com
ErrorLog "logs/dz.dev.com-error.log"
CustomLog "logs/dz.dev.com-access.log" common
</VirtualHost>
安装 Apache 作为 windows 服务
以管理员身份运行 cmd,进入 Apache 的解压目录下的 bin 目录,执行下面的命令1
2
3
4
5
6C:\dev\Apache24\bin>httpd -k install -n Apache
Installing the 'Apache' service
The 'Apache' service is successfully installed.
Testing httpd.conf....
Errors reported here must be corrected before the service can be started.
httpd: Syntax error on line 39 of C:/dev/Apache24/conf/httpd.conf: ServerRoot must be a valid directory
对应的卸载命令:httpd -k uninstall -n Apache
上面的错误是因为,Apache 的 ServerRoot 配置项,不支持相对目录,需要改为绝对目录,修改:http.conf1
Define SRVROOT "C:/dev/Apache24"
再次启动,成功1
2
3C:\dev\Apache24\bin>net start apache
Apache 服务正在启动 ..
Apache 服务已经启动成功。
其它问题
- 如果出现提示443端口被占用,可以通过命令 netstat -aon|findstr “443” 查看是哪个软件占用了该端口,视情况调整或关闭该软件(不推荐修改apache配置绕过该问题)。
检查 Apache 的配置是否正确:httpd.exe -n “apache” -t
1
2
3
4
5
6
7
8
9
10
11C:\dev\Apache24\bin>httpd.exe -n "apache" -t
AH00526: Syntax error on line 43 of C:/dev/Apache24/conf/extra/httpd-vhosts.conf:
Invalid command 'Order', perhaps misspelled or defined by a module not included in the server configuration
C:\dev\Apache24\bin>net start apache
Apache 服务正在启动 .
Apache 服务无法启动。
发生服务特定错误: 1.
请键入 NET HELPMSG 3547 以获得更多的帮助。修复错误配置之后,再次检查并启动
1
2
3
4
5
6C:\dev\Apache24\bin>httpd.exe -n "apache" -t
Syntax OK
C:\dev\Apache24\bin>net start apache
Apache 服务正在启动 ..
Apache 服务已经启动成功。如果访问一个不存在的页面,会提示:Forbidden
1
2Forbidden
You don't have permission to access /install/ on this server.卸载旧的 apache 后,安装另一个版本,启动报错
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15C:\dev\Apache24\bin>httpd -k uninstall -n Apache
Removing the 'Apache' service
The 'Apache' service has been removed successfully.
C:\dev\Apache24\bin>d:
D:\>cd D:\server\Apache24\bin
D:\server\Apache24\bin>httpd -k install -n Apache
[Sat Aug 17 09:28:50.386657 2019] [mpm_winnt:error] [pid 20536:tid 536] AH00433: Apache: Service is already installed.
D:\server\Apache24\bin>net start apache
发生系统错误 1058。
无法启动服务,原因可能是已被禁用或与其相关联的设备没有启动。
原因是,之前的版本在卸载之前并没有停止,所以,安装“新”的版本其实并没有成功。在服务里找到 apache 并停止。继续下面的步骤。1
2
3
4
5
6D:\server\Apache24\bin>httpd -k install -n Apache
Installing the 'Apache' service
The 'Apache' service is successfully installed.
Testing httpd.conf....
Errors reported here must be corrected before the service can be started.
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.56.1. Set the 'ServerName' directive globally to suppress this message
在 httpd.conf
文件中定义:ServerName:win10
,服务名其实可以随便填。下面再次卸载后重新安装:1
2
3
4
5
6
7
8
9D:\server\Apache24\bin>httpd -k uninstall -n Apache
Removing the 'Apache' service
The 'Apache' service has been removed successfully.
D:\server\Apache24\bin>httpd -k install -n Apache
Installing the 'Apache' service
The 'Apache' service is successfully installed.
Testing httpd.conf....
Errors reported here must be corrected before the service can be started.
- 在调用httpd.exe注册Apache的服务时,弹出这句话并不是error,而是提示:如果这行下边出现错误则解决错误后再启动。
- 很多人看到Errors都会想怎么启动就遇到错误啊,首先需要强调的是这句话并不是说Apache启动错误。
- 这句话是Apache内置的启动提示语句,暂时没有找到取消和更改的方法。
- 这句话英文翻译后可以理解为:Apache每次启动都会检测配置文件httpd.conf有没有错误,如果在Errors reported here must be corrected before the service can be started.这句话下边出现提示或错误时必须先把这些问题解决了才能成功启动Apache服务。
- 所以大家看到Errors reported here这句下边没有任何提示则说明Apache启动很成功!可以放心进行其他相关操作。
下面检查配置,并用命令方式启动服务:1
2
3
4
5
6D:\server\Apache24\bin>httpd.exe -n "apache" -t
Syntax OK
D:\server\Apache24\bin>net start apache
Apache 服务正在启动 ..
Apache 服务已经启动成功。
配置检查没有问题,成功启动,说明安装已大功告成!