什么是虚拟主机
在一个Apache服务器上可以配置多个虚拟主机,实现一个服务器提供多站点服务,其实就是访问同一个服务器上的不同目录。
httpd支持多种方式的虚拟主机的配置,主要有以下种:
- 基于IP
- 基于端口
- 基于域名(最常用)
配置基于域名的虚拟主机
域名1:www1.frank.com
页面文件:/var/www/vhosts/www1
日志文件:/var/log/httpd/www1
域名2:www2.frank.com
页面文件:/var/www/vhosts/www2
日志文件:/var/log/httpd/www2
创建指定文件目录
[root@master ~]# mkdir -p /var/www/vhosts/www{1,2}
[root@master ~]# mkdir -p /var/log/httpd/www{1,2}
关闭中心主机配置
打开主配置文件注释掉这几行
# vi /etc/httpd/conf/httpd.conf
#DocumentRoot "/var/www/html"
#<Directory "/var/www">
# AllowOverride None
# # Allow open access:
# Require all granted
#</Directory>
配置虚拟主机
# vi /etc/httpd/conf.d/virhosts.conf
#virtual host 1
<VirtualHost 192.168.138.200:80>
ServerName www1.frank.com
DocumentRoot "/var/www/vhosts/www1"
ErrorLog "/var/log/httpd/www1/error_log"
CustomLog "/var/log/httpd/www1/access_log" combined
<Location /server-status>
SetHandler server-status
</Location>
<Directory /var/www/vhosts/www1>
<RequireAll>
Require all granted
</RequireAll>
</Directory>
</VirtualHost>
#virtual host 2
<VirtualHost 192.168.138.200:80>
ServerName www2.frank.com
DocumentRoot "/var/www/vhosts/www2"
ErrorLog "/var/log/httpd/www2/error_log"
CustomLog "/var/log/httpd/www2/access_log" combined
<Directory /var/www/vhosts/www2>
<RequireAll>
Require all granted
</RequireAll>
</Directory>
</VirtualHost>