windows环境apache2.4基于域名的虚拟主机配置

参考文档

安装参考的文档

apache2.4 服务器的下载、安装和运行

下载

下载地址:https://www.apachelounge.com/download/

当前最新版本:https://www.apachelounge.com/download/VS17/binaries/httpd-2.4.62-240904-win64-VS17.zip

安装

把下载后的zip压缩包解压到任意目录即完成了安装。

配置

修改 apache 安装目录下的 conf/httpd.conf,需要修改的内容如下:

  • 查找 Define SRVROOT,修改为 apache 的安装目录,例如:Define SRVROOT "D:/dev/Apache24"
  • 默认启动端口是80,如要修改端口号,查找 Listen,然后修改为指定端口,例如:Listen 8421

创建系统服务

 1D:\> cd D:\dev\Apache24\bin
 2
 3
 4D:\dev\Apache24\bin> httpd -k install -n apache
 5Installing the 'apache' service
 6The 'apache' service is successfully installed.
 7Testing httpd.conf....
 8Errors reported here must be corrected before the service can be started.
 9AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::70a5:325b:5ac7:e7a2. Set the 'ServerName' direc
10tive globally to suppress this message
11
12
13D:\dev\Apache24\bin> sc start apache
14SERVICE_NAME: apache
15        TYPE               : 10  WIN32_OWN_PROCESS
16        STATE              : 2  START_PENDING
17                                (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
18        WIN32_EXIT_CODE    : 0  (0x0)
19        SERVICE_EXIT_CODE  : 0  (0x0)
20        CHECKPOINT         : 0x2
21        WAIT_HINT          : 0x7530
22        PID                : 45656
23        FLAGS              :

-n 为可选项,表示系统服务的名称

上面的安装有个Error,因为没有在httpd.conf中设置 ServerName

这个错误对服务运行没有影响。

由于是本地环境,没有域名,就不需要理会。外网访问的话,可以加上。

测试

在浏览器中访问: http://localhost:8421/

如果看到:It works!表示安装成功。

配置虚拟主机

修改 conf/httpd.conf,修改的内容如下:

  • 去掉 Include conf/extra/httpd-vhosts.conf 前面的注释
  • 去掉 LoadModule vhost_alias_module modules/mod_vhost_alias.so 前面的注释

修改 conf/extra/httpd-vhosts.conf,修改示例如下:

 1<VirtualHost *:8421>
 2    ServerAdmin conggege325@qq.com
 3    directoryIndex  index.html index.php index.htm
 4    ServerName demo.zcily.net
 5    DocumentRoot "D:/tmp/php-demo"
 6    <Directory "D:/tmp/php-demo">
 7        Options -Indexes +FollowSymlinks
 8        AllowOverride All
 9        Require all granted
10    </Directory>
11</VirtualHost>
12
13<VirtualHost *:8421>
14    ServerAdmin conggege325@qq.com
15    directoryIndex  index.html index.php index.htm
16    ServerName bbs.zcily.net
17    DocumentRoot "D:/dev/workspaces/bbs-discuz"
18    <Directory "D:/dev/workspaces/bbs-discuz">
19        Options -Indexes +FollowSymlinks
20        AllowOverride All
21        Require all granted
22    </Directory>
23</VirtualHost>

说明:

  • Require all granted # 允许所有用户访问我的网站
  • Require all denied # 拒绝所有人访问我的网站
  • AllowOverride All # 开启.htaccess文件
  • Options Indexes # 允许浏览目录结构,也可以写成 +Indexes
  • Options -Indexes # 不允许浏览目录结构
  • options FollowSymLinks # 允许将站点目录下的文件创建软链接到其他目录中使用,也可以写成:+FollowSymLinks
  • options -FollowSymLinks # 服务器不允许此站点目录使用链接符号

重启apache服务即可。