光影工作室

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

发布时间:5年前热度: 3411 ℃评论数:

本文在 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-releaseCentOS Linux release 7.3.1611 (Core)

这里配置两个例子

虚拟机1:www.a.com

虚拟机2:www.b.com

1. 创建虚拟机文件目录

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

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

  • vi /etc/httpd/conf.d/vhost.conf

#禁止IP直接访问

<VirtualHost *:80>

ServerName a.b.c.d

#a.b.c.d为公网IP地址

<Location />

Order Allow,Deny

Deny from all

</Location>

</VirtualHost>


#虚拟主机a的配置

<VirtualHost *:80>

   ServerName www.a.com

   ServerAlias www.a.com

   DocumentRoot /var/www/a

   ErrorLog /var/log/httpd/a/error.log

   CustomLog /var/log/httpd/a/access.log common

<Directory /var/www/a>

   Options -Indexes +Includes +FollowSymLinks +MultiViews

   AllowOverride All

   Require all granted

</Directory>

</VirtualHost>


#虚拟主机b的配置

<VirtualHost *:80>

   ServerName www.b.com

   ServerAlias www.b.com

   DocumentRoot /var/www/b

   ErrorLog /var/log/httpd/b/error.log

   CustomLog /var/log/httpd/b/access.log common

<Directory /var/www/b>

   Options -Indexes +Includes +FollowSymLinks +MultiViews

   AllowOverride All

   Require all granted

</Directory>

</VirtualHost>

  • #保存后重启apache
  • systemctl restart httpd #重启apache

完成上面配置后就可以了

在a和b目录创建html页面测试,内容如下

[root@bing /]# cat var/www/a/index.html
this is a
[root@bing /]# cat var/www/b/index.html
this is b


CentOS7,虚拟机,多站点