CentOS7 配置 httpd虚拟机(Apache设置多个网站)

本文在 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. 创建虚拟机文件目录

  • mkdir -pv /var/www/bing1 #www.bing1.com网站目录
  • mkdir -pv /var/www/bing2 #www.bing2.com网站目录
  • mkdir -pv /var/log/httpd/bing1 #www.bing1.com日志目录
  • mkdir -pv /var/log/httpd/bing2 #www.bing2.com日志目录

2. 创建虚拟主机配置文件

  • vi /etc/httpd/conf.d/vhost.conf
# 虚拟主机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>
  • #保存后重启apache
  • systemctl restart httpd #重启apache

完成上面配置后就可以了

在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:WindowsSystem32driversetc。在该文件中添加两行

192.168.31.197  www.bing1.com

192.168.31.197  www.bing2.com

访问结果如下:

未分类