您的位置:首页 > 运维架构 > Apache

Apache虚拟主机配置

2014-02-18 23:47 369 查看
1、使用不同的端口,使用不方便,仅限于组织内部使用 80、81、88等2、使用不同的IP(端口都是80),需要租赁多个ip地址,费用不划算3、使用不同的主机名称(domain),比较常用的方式。 实现方式:在http的请求报文中,request报文中,有一项叫请求的host表示的不是ip地址而是域名,所以第三种方式就是这么实现的

虚拟主机配置:首先先禁用掉“主”服务器配置[root@Wiker ~]# grep"Section" /etc/httpd/conf/httpd.conf ### Section 1: GlobalEnvironment### Section 2: 'Main'server configuration### Section 3:Virtual Hosts[root@Wiker ~]# vim/etc/httpd/conf/httpd.conf #DocumentRoot "/var/www/html" #注释掉此选项即可首先进行不同端口的虚拟主机的配置[root@Wiker ~]# vim/etc/httpd/conf/httpd.confListen 80 #设置监听不同的虚拟主机需要使用的端口Listen 8080Listen 8088<Virtualhost *:80 > #三个不同端口的主机 ServerName www.80.com DocumentRoot /web/vhosts/www.80.com</Virtualhost>
<Virtualhost*:8080 > ServerName www.8080.com DocumentRoot /web/vhosts/www.8080.com</Virtualhost><Virtualhost*:8088 > ServerName www.8088.com DocumentRoot /web/vhosts/www.8088.com</Virtualhost>[root@Wiker ~]# mkdir-pv /web/vhosts/www.{80,8080,8088}.commkdir: createddirectory `/web'mkdir: createddirectory `/web/vhosts'mkdir: createddirectory `/web/vhosts/www.80.com'mkdir: createddirectory `/web/vhosts/www.8080.com'mkdir: createddirectory `/web/vhosts/www.8088.com'[root@Wiker ~]# echo"80" > /web/vhosts/www.80.com/index.html[root@Wiker ~]# echo"8080" > /web/vhosts/www.8080.com/index.html[root@Wiker ~]# echo"8088" > /web/vhosts/www.8088.com/index.html[root@Wiker ~]# httpd-tSyntax OK[root@Wiker ~]# setenforce 0 #先关掉selinux,要不会报错[root@Wiker ~]#service httpd restartStopping httpd: [ OK ]Starting httpd: [ OK ]你就会发现三个端口都处于监听状态[root@Wiker ~]#netstat -tlnp | grep httpdtcp 0 0 :::8080 :::* LISTEN 2947/httpd tcp 0 0 :::80 :::* LISTEN 2947/httpd tcp 0 0 :::8088 :::* LISTEN 2947/httpd然后打开网页测试

使用三个域名的任何一个加上三个不同的端口都可以访问这三个网站,因为这个虚拟主机使用的是不同的端口号来区分,不是不同的域名。
使用不同的ip地址配置虚拟主机首先给主机附加一个ip地址,临时性的就可以[root@Wiker ~]#ifconfig br0:1 192.168.0.2/24[root@Wiker ~]#ifconfig br0:3 192.168.0.3/24[root@Wiker ~]# vim/etc/httpd/conf/httpd.conf <Virtualhost192.168.0.1:80 > ServerName www.80.com DocumentRoot /web/vhosts/www.80.com</Virtualhost>
<Virtualhost192.168.0.2:80 > ServerName www.8080.com DocumentRoot /web/vhosts/www.8080.com</Virtualhost><Virtualhost192.168.0.3:80 > ServerName www.8088.com DocumentRoot /web/vhosts/www.8088.com</Virtualhost>[root@Wiker ~]# httpd-tSyntax OK[root@Wiker ~]#service httpd restartStopping httpd: [ OK ]Starting httpd: [ OK ]使用使用浏览器进行测试


端口和ip地址也可以结合使用[root@Wiker ~]# vim/etc/httpd/conf/httpd.conf <Virtualhost192.168.0.1:80 > ServerName www.80.com DocumentRoot /web/vhosts/www.80.com</Virtualhost>
<Virtualhost192.168.0.2:80 > ServerName www.8080.com DocumentRoot /web/vhosts/www.8080.com</Virtualhost><Virtualhost192.168.0.1:8080 > ServerName www.8088.com DocumentRoot /web/vhosts/www.8088.com</Virtualhost>[root@Wiker ~]#service httpd restartStopping httpd: [ OK ]Starting httpd: [ OK ]使用浏览器进行测试



基于主机名的虚拟主机[root@Wiker ~]# httpd-vServer version:Apache/2.2.15 (Unix)Server built: Oct 62011 11:06:33[root@Wiker ~]# vim /etc/httpd/conf/httpd.confNameVirtualHost 192.168.0.1:80 #apache2.2.xx版本需要开启此选项,而且要和下面的保持一致;2.4.x版本就不需要此项设置了<Virtualhost 192.168.0.1:80 > #这三个虚拟主机如果输入ip地址来访问的话,访问的都是第一个,使用域名可以访问三个内容 ServerName www.80.com DocumentRoot /web/vhosts/www.80.com</Virtualhost>
<Virtualhost 192.168.0.1:80 > ServerName www.8080.com DocumentRoot /web/vhosts/www.8080.com</Virtualhost><Virtualhost192.168.0.1:80 > ServerName www.8088.com DocumentRoot /web/vhosts/www.8088.com</Virtualhost>[root@Wiker ~]#service httpd restartStopping httpd: [ OK ]Starting httpd: [ OK ]使用浏览器进行测试


虚拟主机的别名我们在本地hosts添加一条记录192.168.0.1www.80.com192.168.0.1www.8080.com192.168.0.1www.8088.com192.168.0.1bbs.8080.com想让bbs.8080.com显示的和www.8080.com一样的内容,但是我们浏览器访问的时候发现是

因为bbs.8080.com自动匹配httpd.conf配置里面的第一个虚拟主机了,如果想实现此要求可以进行如下设置,设置虚拟主机的别名[root@Wiker ~]# vim/etc/httpd/conf/httpd.conf<Virtualhost192.168.0.1:80 > ServerName www.80.com DocumentRoot /web/vhosts/www.80.com</Virtualhost>
<Virtualhost192.168.0.1:80 > ServerAlias bbs.8080.com ServerName www.8080.com DocumentRoot /web/vhosts/www.8080.com</Virtualhost><Virtualhost192.168.0.1:80 > ServerName www.8088.com DocumentRoot /web/vhosts/www.8088.com [root@Wiker ~]#service httpd restartStopping httpd: [ OK ]Starting httpd: [ OK ]然后现在访问就可以实现上面的要求了

默认虚拟主机定义[root@Wiker ~]# vim/etc/httpd/conf/httpd.con<Virtualhost192.168.0.1:80 > ServerName default DocumentRoot /web/vhosts/default ErrorDocument 404 /site_list.html</Virtualhost>或者[root@Wiker ~]# vim/etc/httpd/conf/httpd.conf<Virtualhost_default_:80 > DocumentRoot /web/vhosts/default</Virtualhost>

生成一批虚拟主机:如果想生成一批虚拟主机,比如使用的名字是www.a.com,就存放在/web/vhosts/www.a.com/目录下www.b.com就存放在/web/vhosts/www.b.com/目录下VirtualDocumentRoot /web/vhosts/%0/VirtualScriptAlias/www/vhosts/%0/cgi-bin

将虚拟主机的配置信息放置于数据库中(第三方模块)mod_vhost_dbi下载地址:http://www.outoforder.cc/projects/httpd/mod_vhost_dbi/需要先安装libdbi库文件Libdbilibdbi-driverslibdbi-dbd-mysql

本文出自 “IT没有圈” 博客,请务必保留此出处http://wiker.blog.51cto.com/8481872/1360285
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: