虚拟主机的实现

示例1:基于ip

编辑配置文件,切换到最后一行,增加:

<VirtualHost 192.168.1.117:80>

    ServerName web1.ams.com

    DocumentRoot "/vhosts/web1/htdocs"

</VirtualHost>

<VirtualHost 192.168.1.115:80>

    ServerName web2.ams.com

    DocumentRoot "/vhosts/web2/htdocs"

</VirtualHost>

[root@bluee htdocs]# service httpd configtest

Warning: DocumentRoot [/vhosts/web1/htdocs] does not exist

Warning: DocumentRoot [/vhosts/web2/htdocs] does not exist

Syntax OK

[root@bluee htdocs]#

提示说虚拟主机配置文件不存在,于是创建:

[root@bluee htdocs]# cd

[root@bluee ~]# mkdir -pv /vhosts/{web1,web2,web3,web4}/htdocs

mkdir: created directory `/vhosts'

mkdir: created directory `/vhosts/web1'

mkdir: created directory `/vhosts/web1/htdocs'

mkdir: created directory `/vhosts/web2'

mkdir: created directory `/vhosts/web2/htdocs'

mkdir: created directory `/vhosts/web3'

mkdir: created directory `/vhosts/web3/htdocs'

mkdir: created directory `/vhosts/web4'

mkdir: created directory `/vhosts/web4/htdocs'

[root@bluee ~]#

先给前2个创建网页文件:web1,web2

[root@bluee ~]# vim /vhosts/web1/htdocs/index.html

[root@bluee ~]# vim /vhosts/web2/htdocs/index.html

[root@bluee ~]#

[root@bluee ~]# httpd -t

Syntax OK

[root@bluee ~]#

语法测试,ok

再加个ip地址:192.168.1.118/16 

[root@bluee ~]# ip addr add 192.168.1.118/16 dev eth0

[root@bluee ~]# ip addr list

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host 

       valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:c1:45:55 brd ff:ff:ff:ff:ff:ff

    inet 192.168.1.117/24 brd 192.168.1.255 scope global eth0

    inet 192.168.1.118/16 scope global eth0

    inet6 fe80::20c:29ff:fec1:4555/64 scope link 

       valid_lft forever preferred_lft forever

3: pan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN 

    link/ether e6:63:37:68:33:9d brd ff:ff:ff:ff:ff:ff

[root@bluee ~]#

reload

浏览器访问:http://192.168.1.117/  显示page at web1 

            http://192.168.1.118/  显示page at web2 

都没问题。

示例2:基于port

再增加一个虚拟主机,端口改为:8080

并监听;

<VirtualHost 192.168.1.117:80>

    ServerName web1.ams.com

    DocumentRoot "/vhosts/web1/htdocs"

</VirtualHost>

<VirtualHost 192.168.1.118:80>

    ServerName web2.ams.com

    DocumentRoot "/vhosts/web2/htdocs"

</VirtualHost>

<VirtualHost 192.168i.1.118:8080>

    ServerName web3.ams.com

    DocumentRoot "/vhosts/web3/htdocs"

</VirtualHost>

#Listen 12.34.56.78:80

Listen 80

Listen 8080  这项改下,表示监听所有地址的8080端口

再创建个网页文件:web3

[root@bluee ~]# vim /vhosts/web3/htdocs/index.html

[root@bluee ~]# service httpd restart

访问 http://192.168.1.118:8080/  没问题。

示例3:基于hostname

先启用这项:

NameVirtualHost 192.168.1.117:80  这项也改为80,保持一致。  

表示基于主机名的虚拟主机;不过 httpd2.4不需要了

ip全改为:192.168.1.117:80

<VirtualHost 192.168.1.117:80>

    ServerName web1.ams.com

    DocumentRoot "/vhosts/web1/htdocs"

</VirtualHost>

<VirtualHost 192.168.1.117:80>

    ServerName web2.ams.com

    DocumentRoot "/vhosts/web2/htdocs"

</VirtualHost>

<VirtualHost 192.168.1.117:80>

    ServerName web3.ams.com

    DocumentRoot "/vhosts/web3/htdocs"

</VirtualHost>

[root@bluee ~]# httpd -t

Syntax OK

[root@bluee ~]#

检查下没错误

service httpd restart

再找台主机使用curl命令访问:

做host绑定:

vim /etc/hosts

把ip地址以及这几个主机名添加进来:(确保这台主机以及服务器端主机的iptables,selinux全部关掉,我老是忘!)

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.1.117 web1.ams.com web2.ams.com web3.ams.com

保存退出。

reload

再使用curl命令访问:

[root@Ams ~]# curl http://web3.ams.com

page at web3

[root@Ams ~]# curl http://web2.ams.com

page at web2

[root@Ams ~]# curl http://web1.ams.com

page at web1

[root@Ams ~]#

可以访问到。

还可以添加日志:

注意字母大小写,引号等问题,错一个字符,启动不起来。。。

<VirtualHost 192.168.1.117:80>

    ServerName web1.ams.com

    DocumentRoot "/vhosts/web1/htdocs"

    CustomLog logs/web1_access_log combined

</VirtualHost>

<VirtualHost 192.168.1.117:80>

    ServerName web2.ams.com

    DocumentRoot "/vhosts/web2/htdocs"

    CustomLog logs/web2_access_log combined

</VirtualHost>

<VirtualHost 192.168.1.117:80>

    ServerName web3.ams.com

    DocumentRoot "/vhosts/web3/htdocs"

    CustomLog logs/web3_access_log combined

</VirtualHost>

我们多访问几次:

[root@Ams ~]# curl http://web1.ams.com

page at web1

[root@Ams ~]# curl http://web2.ams.com

page at web2

[root@Ams ~]# curl http://web3.ams.com

page at web3

[root@Ams ~]# curl http://web2.ams.com

page at web2

[root@Ams ~]# curl http://web1.ams.com

page at web1

[root@Ams ~]#

查看服务器端日志:

已经有了。每个虚拟机使用各自独立的访问日志

[root@bluee ~]# ls /var/log/httpd/

access_log  error_log  web1_access_log  web2_access_log  web3_access_log

[root@bluee ~]# 

原创文章,作者:mississippi,如若转载,请注明出处:http://www.178linux.com/28014