当年的笔记_apache配置虚拟主机

下午需要,在网上找了一堆,没找到合适的,翻出来自己当年的笔记,还是自己记的容易理解。

解决方案1:通过端口来区分

1>添加一个虚拟主机
1.在d盘下新建www目录,如:d:/www。

2.修改httpd.conf中:
# Virtual hosts
#Include conf/extra/httpd-vhosts.conf
去掉前面的#号,启用虚拟主机

3.修改httpd-vhosts.conf文件,在后面添加以下:
<VirtualHost 127.0.0.1:80>
    DocumentRoot "d:/www"
    DirectoryIndex index.html index.htm index.php
    <Directory />
    options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
    </Directory>
</VirtualHost>

4.在hosts文件中添加ip和域名的对应关系
127.0.0.1       www.xxx.com

5.建议注销,在httpd.conf以下代码前面加#号
DocumentRoot "D:/wamp/apache/htdocs"

6.测试 http://www.xxx.com
如只需加一虚拟主机,可以不往下看了。


2>添加另一个虚拟主机
1.开发新的网站 d"/www2
2.修改httpd-vhosts.conf文件,在后面添加以下:
<VirtualHost 127.0.0.1:81>
    DocumentRoot "d:/www"
    DirectoryIndex index.html index.htm index.php
    <Directory />
    options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
    </Directory>
</VirtualHost>
3.配置httpd.conf文件,让apache监听81端口,添加listen 81
4.在hosts文件中添加
127.0.0.1       www.xxx2.com
5.测试  http://www.xxx2.com:81 //需指定端口号


解决方案2:通过ServerName段来区分不同的域名

1.开发新的站点 d:/www1  d:/www2

2.修改httpd.conf中:
# Virtual hosts
#Include conf/extra/httpd-vhosts.conf
去掉前面的#号,启用虚拟主机

3.在httpd-vhosts.conf文件中添加以下代码
<VirtualHost *:80>
    DocumentRoot "d:/www"
    #这里指定域名
    ServerName www.xxx.com
    DirectoryIndex index.html index.htm index.php
    <Directory />
    options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
    </Directory>
</VirtualHost>


<VirtualHost *:80>
    DocumentRoot "d:/www2"
    #这里指定域名
    ServerName www.xxx2.com
    DirectoryIndex index.html index.htm index.php
    <Directory />
    options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
    </Directory>
</VirtualHost>

4.重启apache,测试
http://www.xxx.com
http://www.xxx2.com
方案1刚试了可行,方案2有点小问题,没达到自己的要求。

大家有更好的建议也分享下。


原文地址:https://www.cnblogs.com/lltong/p/3153417.html