CentOS 7.3配置Apache虚拟主机
2019-02-08 00:00
465 查看
实验环境
- 一台最小化安装的CentOS 7.3虚拟机
配置基础环境
1. 安装apache
yum install -y httpd
2. 建立虚拟主机的根目录
mkdir /var/wwwroot
mkdir /var/wwwroot/site1
mkdir /var/wwwroot/site2
echo -e "site1" >> /var/wwwroot/site1/index.html
echo -e "site2" >> /var/wwwroot/site2/index.html
3. 关闭CentOS的防火墙
setenforce 0
systemctl stop firewalld
systemctl disable firewalld
配置基于端口的虚拟主机
1. 编辑apache配置文件
vim /etc/httpd/conf.d/vhost.conf
2. 添加以下内容
Listen 8081
<VirtualHost *:8081>
DocumentRoot "/var/wwwroot/site1"
ErrorLog "logs/site1.error.log"
CustomLog "logs/site1.access.log" common
<Directory "/var/wwwroot/site1">
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
Listen 8082
<VirtualHost *:8082>
DocumentRoot "/var/wwwroot/site2"
ErrorLog "logs/site2.error.log"
CustomLog "logs/site2.access.log" common
<Directory "/var/wwwroot/site2">
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
3. 启动apache服务
systemctl start httpd
4. 在宿主机访问两个站点
http://192.168.204.133:8081
http://192.168.204.133:8082
配置基于域名的虚拟主机
1. 编辑apache配置文件
vim /etc/httpd/conf.d/vhost.conf
2. 修改为以下内容
<VirtualHost *:80>
DocumentRoot "/var/wwwroot/site1"
ServerName site1.test.com
ErrorLog "logs/site1.error.log"
CustomLog "logs/site1.access.log" common
<Directory "/var/wwwroot/site1">
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/wwwroot/site2"
ServerName site2.test.com
ErrorLog "logs/site2.error.log"
CustomLog "logs/site2.access.log" common
<Directory "/var/wwwroot/site2">
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
3. 重启apache服务
systemctl restart httpd
4. 在Windows上修改hosts
文件
编辑C:\Windows\System32\drivers\etc\hosts
文件,
添加以下内容(根据实际情况自己修改)
192.168.204.135 site1.test.com
192.168.204.135 site2.test.com
5. 在宿主机访问两个站点
http://site1.test.com/
http://site2.test.com/
配置基于IP的虚拟主机
1. 在虚拟机上增加两个IP地址
ifconfig ens33:1 192.168.204.135
ifconfig ens33:2 192.168.204.136
2. 重新编辑apache配置文件
vim /etc/httpd/conf.d/vhost.conf
3. 修改配置文件为以下内容
<VirtualHost 192.168.204.135:80>
DocumentRoot "/var/wwwroot/site1"
ErrorLog "logs/site1.error.log"
CustomLog "logs/site1.access.log" common
<Directory "/var/wwwroot/site1">
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.204.136:80>
DocumentRoot "/var/wwwroot/site2"
ErrorLog "logs/site2.error.log"
CustomLog "logs/site2.access.log" common
<Directory "/var/wwwroot/site2">
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
4. 重启apache服务
systemctl restart httpd
5. 在宿主机访问两个站点
http://192.168.204.135/
http://192.168.204.136/
相关文章推荐
- CentOS下Apache虚拟主机VirtualHost配置
- CentOS 7运维管理笔记(8)----Apache基于域名的虚拟主机配置
- centos apache 多域名配置虚拟主机
- CentOS 7运维管理笔记(6)----Apache 基于 IP 的虚拟主机配置
- centos6.4下apache配置支持ssl的多台虚拟主机
- centos7.2 利用yum安装配置apache2.4多虚拟主机
- CentOS 下apache基于域名的多虚拟主机配置问题
- CentOS 5上Apache配置虚拟主机范例
- 【CentOS】Apache多虚拟主机多版本PHP(5.3+5.6+N)共存运行配置全过程
- CentOS下配置apache虚拟主机
- CentOS 7运维管理笔记(7)----Apache 基于端口的虚拟主机配置
- CentOS 下apache基于域名的多虚拟主机配置问题-新问题
- CentOS 7运维管理笔记(8)----Apache基于域名的虚拟主机配置
- CentOS中增加apache虚拟主机的配置
- Centos 7 Apache配置虚拟主机
- CentOS 7.3配置Nginx虚拟主机
- Centos解决Apache配置虚拟主机问题。Apache欢迎页可以打开,但是网站目录定位不上
- CentOS中增加apache虚拟主机的配置
- ubuntu下Apache虚拟主机的配置
- apache配置虚拟主机