centos6.5搭建Apache-虛擬主機

1、配置基於域名的虛擬用戶html

1.建立虛擬用戶的網頁根目錄node

cd /usr/local/httpd/htdocs/web

mkdir benetcomvim

cd benetcom服務器

echo "<h1>benet</h1>" > index.htmlcurl

cd ../測試

mkdir accpcomurl

cd accpcomspa

echo "<h1>accp</h1>" > index.htmlrest

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>

顯示以上結果爲虛擬用戶配置成功

 

2、配置基於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

 

 

3、配置基於端口的虛擬主機

注意:註釋掉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

相關文章
相關標籤/搜索