本文在 CentOS 7系统中使用httpd配置两个基于域名访问的虚拟主机。
当前环境
[root@bing /]# httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built: Oct 19 2017 20:39:16
[root@bing /]# cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core)
这里配置两个例子
虚拟机1:www.bing1.com
虚拟机2:www.bing2.com
1. 创建虚拟机文件目录
2. 创建虚拟主机配置文件
# 虚拟主机1的配置
<VirtualHost *:80>
ServerName www.bing1.com #虚拟机1设置域名为www.bing1.com
DocumentRoot /var/www/bing1 #虚拟机网站目录
ErrorLog “/var/log/httpd/bing1/error_log” #错误日志
CustomLog “/var/log/httpd/bing1/access_log” combined #访问日志
<Directory /var/www/bing1>
Options -Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted #允许所有请求
</Directory>
</VirtualHost>
# 虚拟主机2的配置
<VirtualHost *:80>
ServerName www.bing2.com
DocumentRoot /var/www/bing2
ErrorLog “/var/log/httpd/bing2/error_log”
CustomLog “/var/log/httpd/bing2/access_log” combined
<Directory /var/www/bing2>
Options -Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
完成上面配置后就可以了
在bing1和bing2目录创建html页面测试,内容如下
[root@bing /]# cat var/www/bing1/index.html
this is bing1
[root@bing /]# cat var/www/bing2/index.html
this is bing2
修改客户端主机的hosts文件,以便能解析域名
hosts在windows环境下的路径为C:\Windows\System32\drivers\etc。在该文件中添加两行
192.168.31.197 www.bing1.com
192.168.31.197 www.bing2.com
访问结果如下: