centos6.5搭建Apache-虚拟主机
一、配置基于域名的虚拟用户
1.创建虚拟用户的网页根目录
cd /usr/local/httpd/htdocs/
mkdir benetcom
cd benetcom
echo "<h1>benet</h1>" > index.html
cd ../
mkdir accpcom
cd accpcom
echo "<h1>accp</h1>" > index.html
2.修改虚拟用户的配置文件
cd /usr/local/httpd/conf/extra/
vim httpd-vhosts.conf #更改配置文件,修改红色部分内容
NameVirtualHost 192.168.25.105 #更改19行更改为web服务器(本机)的IP地址
<VirtualHost 192.168.25.105>
DocumentRoot /usr/local/httpd/htdocs/benetcom/
ServerName www.benet.com
ErrorLog "logs/benet-error_log"
CustomLog "logs/benet-access_log" common
</VirtualHost>
<VirtualHost 192.168.25.105>
DocumentRoot /usr/local/httpd/htdocs/accpcom/
ServerName www.accp.com
ErrorLog "logs/accp-error_log"
CustomLog "logs/accp-access_log" common
</VirtualHost>
3.修改主配置文件
还原一下配置httpd的原有配置。
cp httpd.conf.bak httpd.conf
vim /usr/local/httpd/conf/httpd.conf
#在最后一行添加以下内容,加载外部的虚拟用户的配置文件
Include conf/extra/httpd-vhosts.conf
重启服务
service httpd restart
配置完虚拟主机后 重启报错:
[warn] NameVirtualHost *:80 has no VirtualHosts
解决:
vim httpd-vhosts.conf
将19行注释掉
#NameVirtualHost *:80
4.修改hosts文件
vim /etc/hosts #添加以下内容
192.168.3.50 www.benet.com
192.168.3.50 www.accp.com
5.测试虚拟用户配置
[root@CentOS6-node1 accpcom]# curl http://www.accp.com
<h1>accp</h1>
[root@CentOS6-node1 accpcom]# curl http://www.benet.com
<h1>benet</h1>
显示以上结果为虚拟用户配置成功
二、配置基于IP的虚拟主机
<VirtualHost 192.168.25.105>
DocumentRoot /usr/local/httpd/htdocs/benetcom/
ServerName www.benet.com
ErrorLog "logs/benet-error_log"
CustomLog "logs/benet-access_log" common
<VirtualHost 192.168.25.106>
DocumentRoot /usr/local/httpd/htdocs/benetcom/
ServerName www.accp.com
ErrorLog "logs/benet-error_log"
CustomLog "logs/benet-access_log" common
三、配置基于端口的虚拟主机
注意:注释掉NameVirtualHost 192.168.25.105
<VirtualHost 192.168.25.105:81>
……………
</VirtuealHost>
<VirtualHost 192.168.25.105:81>
……………….
</VirtuealHost>
(2)在httpd.conf中加入以下配置
Include conf/extra/httpd-vhosts.conf
Listen 192.168.25.105:81
Listen 192.168.25.105:82