虚拟主机的介绍
虚拟主机,简单的说指一个物理机服务多个站点,每个站点可通过一个或多个虚拟主机来实现
http有3中类型的虚拟主机:
(1)基于IP,配置时一定要配置好所以的IP
(2)基于PORT,配置时一定要启用Listen PORT这一项,开启所有的监听端口
(3)基于FQDN,配置时要启用NameVirtualHost这一项
相关配置
配置前提:
已经安装好httpd服务器
配置步骤:
1. 基于端口的虚拟主机配置
#vim/etc/httpd/conf/httpd.conf …..添加如下内容……….. Listen 8080 …………………. <VirtualHost *:80> ServerName www.a.com DocumentRoot/vhosts/a.com/htdocs/ </VirtualHost> <VirtualHost *:8080> ServerName www.b.org DocumentRoot/vhosts/b.org/htdocs </VirtualHost> # mkdir/vhosts/{a.com,b.org}/htdocs –pv [[email protected] ~]# cd /vhosts/a.com/htdocs/ [[email protected] htdocs]# vimindex.html <h1>a.com</h1> [[email protected] htdocs]#vim /vhosts/b.org/htdocs/index.html <h1>b.org</h1> #vim /etc/hosts …………添加 192.168.1.11 www.a.com 192.168.1.12 www.b.org #httpd –t [[email protected] htdocs]# service httpd restart Stopping httpd: [ OK ] Starting httpd: httpd:Could not reliably determine the server‘s fully qualified domain name, using172.16.33.1 for ServerName [ OK ]
到这里我们基于端口的虚拟主机就配置完成,现在在浏览器中输入:
观察测试结果
2. 基于FQDN的虚拟主机的配置
编辑配置文件,具体内容如下:
NameVirtualHost 192.168.1.11:80 #启用NameVirtualHost 这一项 ………. <VirtualHost192.168.1.11:80> ServerName www.a.com DocumentRoot /vhosts/a.com/htdocs/ </VirtualHost> <VirtualHost192.168.1.11:80> ServerName www.b.org DocumentRoot /vhosts/b.org/htdocs </VirtualHost> <VirtualHost192.168.1.11:80> ServerName www.c.net DocumentRoot /vhosts/c.net/htdocs </VirtualHost> [[email protected] htdocs]# mkdir/vhosts/c.net/htdocs -p [[email protected] htdocs]#vim /vhosts/c.net/htdocs/index.html <h1>c.net</h1> [[email protected] vhosts]# vim/etc/hosts ……添加….. 192.168.1.11 www.a.com 192.168.1.11 www.b.org 192.168.1.11 www.c.net
基于FQDN的虚拟主机配置完毕,下面进行访问测试:
[[email protected] vhosts]# curlhttp://www.c.net
<h1>c.net</h1>
[[email protected] vhosts]# curlhttp://www.b.org
<h1>b.org</h1>
[[email protected] vhosts]# curlhttp://www.a.com
<h1>a.com</h1>
- 3. 基于IP的虚拟主机配置
[[email protected] vhosts]#ifconfig eth0:0 192.168.1.21 [[email protected] vhosts]# vim/etc/httpd/conf/httpd.conf ……….. <VirtualHost192.168.1.11:80> ServerName www.a.com DocumentRoot /vhosts/a.com/htdocs/ </VirtualHost> <VirtualHost192.168.1.21:80> ServerName www.b.org DocumentRoot /vhosts/b.org/htdocs/ </VirtualHost> [[email protected] vhosts]# httpd-t httpd: Could not reliablydetermine the server‘s fully qualified domain name, using 172.16.33.1 forServerName Syntax OK [[email protected] vhosts]# !s service httpd restart Stopping httpd: [ OK ] Starting httpd: httpd:Could not reliably determine the server‘s fully qualified domain name, using172.16.33.1 for ServerName [ OK ]
到这里我们基于IP的虚拟主机就配置完了,在浏览器中测试输入:
- 4. 基于IP+PORT的虚拟主机的配置
编辑配置文件/etc/httpd/conf/httpd.conf: Listen 8080 #添加这一项 …………….. <VirtualHost192.168.1.11:80> ServerName www.a.com DocumentRoot /vhosts/a.com/htdocs/ </VirtualHost> <VirtualHost 192.168.1.21:80> ServerName www.b.org DocumentRoot /vhosts/b.org/htdocs/ </VirtualHost> <VirtualHost192.168.1.11:8080> ServerName www.c.net DocumentRoot /vhosts/c.net/htdocs/ </VirtualHost> #httpd -t #service httpd restart
到这里我们基于IP+PORT的虚拟主机就配置完成了,在浏览器中访问192.168.1.199:80,192.168.1.101:80,192.168.1.101:8080测试,观察结果
时间: 2024-12-15 19:20:38