一、应用场景
+--------------+----------------+---------------------+
| 域名 | 主机ip | 对应的主目录 |
+--------------+----------------+---------------------+
| www.100.com |192.168.80.135 | /var/www/html |
| bbs.100.com |192.168.80.135 | /var/www/html/bbs |
| sns.100.com |192.168.80.135 | /var/www/html/sns |
| ftp.100.com |192.168.80.135 | /var/www/ftp |
+--------------+----------------+---------------------+
二、实现
# vim /etc/httpd/conf.d/virtual.conf
#本机任何页面的 port 80 所指定的虚拟主机
NameVirtualHost *:80
#设置www.100.com
#<directory>:针对目录浏览权限的设置
#<virtualhost>:对documentRoot、customlog(日志目录)进行设置
<directory "var/www/html">
options Followsymlinks
allowoverride none
order allow,deny
allow from all
</directory>
<virtualhost *:80>
serverName www.100.com
documentroot /var/www/html
customlog /var/log/httpd/www.access_log combined
</virtualhost>
#设置sns.100.com
<directory "/var/www/html/sns/">
Options FollowSymLinks
AllowOverride None
Order allow,deny
allow from all
</directory>
<virtualHost *:80>
serverName sns.100.com
documentRoot /var/www/html/sns
customlog /var/log/httpd/sns.access_log combined
</virtualHost>
#设置bbs.100.com
<directory "/var/www/html/bbs/">
Options FollowSymLinks
AllowOverride None
Order allow,deny
allow from all
</directory>
<virtualhost *:80>
servername bbs.100.com
documentroot /var/www/html/bbs
customlog /var/log/httpd/bbs.access_log combined
</virtualhost>
# 设置ftp.100.com
<directory "/var/www/ftp">
options Indexes FollowSymlinks
allowoverride none
order allow,deny
allow from all
</directory>
<virtualhost *:80>
servername ftp.100.com
documentroot /var/www/ftp
customlog /var/log/httpd/ftp.access_log combined
ErrorLog /var/log/httpd/ftp.error_log combined
</virtualhost>
三、注意事项
- 在虚拟主机的设置中,还有很多可用的功能,不过,最低的限度是需要ServerName以及documentRoot。
- 使用了虚拟主机后,原本的主机名称www.100.com也要同时写入到对应的虚拟主机中,否则这个主机域名可能将不知道去向。
- CustomLog表示向对应主机访问时,日志将被写入到该文件中。但这个新增的日志必须要加入到logrotate的管理文件中,否则日志文件将会超级大。
# cd /etc/logrotate.d/
# vim httpd
/var/log/httpd/*log {
missingok
notifempty
compress
sharedscripts
delaycompress
postrotate
/sbin/service httpd reload > /dev/null 2>/dev/null || true
endscript
}