一、应用场景
+--------------+----------------+---------------------+
| 域名 | 主机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
#:针对目录浏览权限的设置
#:对documentRoot、customlog(日志目录)进行设置
options Followsymlinks
allowoverride none
order allow,deny
allow from all
serverName www.100.com
documentroot /var/www/html
customlog /var/log/httpd/www.access_log combined
#设置sns.100.com
Options FollowSymLinks
AllowOverride None
Order allow,deny
allow from all
serverName sns.100.com
documentRoot /var/www/html/sns
customlog /var/log/httpd/sns.access_log combined
#设置bbs.100.com
Options FollowSymLinks
AllowOverride None
Order allow,deny
allow from all
servername bbs.100.com
documentroot /var/www/html/bbs
customlog /var/log/httpd/bbs.access_log combined
# 设置ftp.100.com
options Indexes FollowSymlinks
allowoverride none
order allow,deny
allow from all
servername ftp.100.com
documentroot /var/www/ftp
customlog /var/log/httpd/ftp.access_log combined
ErrorLog /var/log/httpd/ftp.error_log combined
三、注意事项
在虚拟主机的设置中,还有很多可用的功能,不过,最低的限度是需要ServerName以及documentRoot。
使用了虚拟主机后,原本的主机名称www.100.com也要同时写入到对应的虚拟主机中,否则这个主机域名可能将不知道去向。
CustomLog表示向对应主机访问时,日志将被写入到该文件中。但这个新增的日志必须要加入到logrotate的管理文件中,否则日志文件将会超级大。
ErrorLog表示错误日志
# 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
}