Apache配置虚拟主机

切记,在修改之前请先备份修改的文件

1.在httpd.conf中有Define SRVROOT "G:/Apache24" ServerRoot "${SRVROOT}"这两行,那么等下可以使用${SRVROOT}这个作为变量去配置虚拟主机目录

2.开启httpd.conf中的Include conf/extra/httpd-vhosts.conf,让apache读取虚拟主机配置文件

3.在conf/extra/httpd-vhosts.conf文件中添加

  <VirtualHost *:80>
  DocumentRoot "${SRVROOT}/htdocs/shop"
  ServerName shop.com
  </VirtualHost>

4.配置本地的hosts文件:C:WindowsSystem32driversetc下的hosts文件,新增

  127.0.0.1 shop.com

做完以上步骤,重启apache,浏览器访问shop.com即可打开页面

流程为:

1.浏览器访问shop.com,先在本地hosts文件中找,匹配到127.0.0.1,访问本地服务器

2.apache解析请求,由于httpd.conf中启用了httpd-vhosts.conf,apache将能识别shop.com,找到它对应的${SRVROOT}/htdocs/shop,最终访问到G:/Apache24/htdocs/shop

3.由于设置了httpd.conf中默认加载index.html或index.php,所以会访问G:/Apache24/htdocs/shop/index.html

原文地址:https://www.cnblogs.com/chuwu/p/9613730.html