4. 基于端口的多站点配置
适用场景:让同一个IP的不同端口伺服多个域名。例如:通过http://www.test01.com:80访问站点test01,通过http://www.test01.com:8080访问站点test02。配置要点:同一个IP端口不同;域名相同根目录不同
具体配置如下:
i.配置http监听80和8080端口:Listen:80
Listen:8080
ii.配置NameVirtualHost:NameVirtualHost 192.168.17.128:80
NameVirtualHost 192.168.17.128:8080
iii.配置/etc/httpd/conf.d/www.test01.conf,配置如下:
[root@Slaver conf.d]# cat www.test01.conf
<VirtualHost 192.168.17.128:80>
DocumentRoot /var/www/html/test01
ServerName www.test01.com
<Directory /var/www/html/test01>
Options Indexes FollowSymLinks
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost 192.168.17.128:8080>
DocumentRoot /var/www/html/test02
ServerName www.test01.com
<Directory /var/www/html/test02>
Options Indexes FollowSymLinks
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>
[root@Slaver conf.d]#
5. 只允许某个网段访问站点
适用场景:发布到外网的web站点,后台管理文件夹administrator不想让外网的用户可以访问,只允许内网192.168.17.0/24网段的客户机访问。
配置如下:
[root@Slaver conf.d]# cat www.test01.conf
<VirtualHost 192.168.17.128:80>
DocumentRoot /var/www/html/test01
ServerName www.test01.com
ErrorLog /var/log/httpd/www.test01.err
CustomLog /var/log/httpd/www.test01.access common
<Directory /var/www/html/test01/administrator>
Options Indexes FollowSymLinks
AllowOverride All
Order Allow,Deny
Allow from 192.168.17.0/24
</Directory>
</VirtualHost>
[root@Slaver conf.d]#
当不是通过这个网段进来的访问请求,会出现如下的提示错误:

6. 只允许通过认证的用户访问站点
适用场景:当访问后台时,通过提供用户名和密码进行认证,只有通过认证的用户才能访问后台。
配置文件如下:
[root@Slaver conf.d]# cat www.test01.conf
<VirtualHost 192.168.17.128:80>
DocumentRoot /var/www/html/test01
ServerName www.test01.com
ErrorLog /var/log/httpd/www.test01.err
CustomLog /var/log/httpd/www.test01.access common
<Directory /var/www/html/test01/administrator>
Options Indexes FollowSymLinks
AllowOverride AuthConfig
AuthName "Please input username and password!"
AuthType Basic
AuthUserFile /etc/httpd/.htpasswd
Require user houqd2012 houqd2015
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>
[root@Slaver conf.d]#
配置项代表的含义如下:
AllowOverride AuthConfig #采用认证机制
AuthName “Please input username and password!” #认证时显示的提示语
AuthType Basic #用户认证类型
AuthUserFile #认证时用户的账号密码文件
Require user houqd2012 houqd2015 #只有houqd2012和houqd2015用户才能登陆
创建用户名和密码的命令如下:
htpasswd -cm /etc/httpd/.htpasswd houqd2012
htpasswd -c /etc/httpd/.htpasswd houqd2015
-c代表创建一个新文件,-m代表密码使用MD5加密,执行完后会生成/etc/httpd/.htpasswd文件。
效果图如下:

三、总结
在开发中好几次遇到虚拟主机的配置问题,每次都是临时查一查解决没有系统的了解,等下次碰到问题时还是解决不了,浪费了很多时间,这次索性集中整理一次,望更多的朋友也了解如何利用虚拟主机更好的我们的生产环境服务。
四、参考链接
a. VirtualHost Examples:http://httpd.apache.org/docs/2.4/vhosts/examples.html
b. Ubuntu Apache 2配置详解(含虚拟主机配置方法) http://www.linuxidc.com/Linux/2011-07/38324.htm
c. 维基百科虚拟主机的解释:http://zh.wikipedia.org/zh-cn/%E8%99%9A%E6%8B%9F%E4%B8%BB%E6%9C%BA
d. Linux入门教程:Apache服务配置 http://www.linuxidc.com/Linux/2014-07/104454.htm
e. 教你如何实现Apahce的用户认证:http://www.linuxidc.com/Linux/2013-04/82422.htm
本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-02/112871.htm
