1 配置环境说明
系统环境: CentOS7
Apache 环境: 编译安装的 httpd-2.4.7
系统限制: 关闭了防火墙和 selinux
hosts 文件中配置以下域名解析
- 192.168.115.150 www.web1.com
- 192.168.115.150 www.web2.com
2Apache 虚拟主机简介
虚拟主机是指在一个机器上运行多个网站 (例如: www.web1.com 和 www.web2.com) 每个站点希望用不同的域名和站点目录, 或者是不同端口不同的 IP 就需要虚拟主机
3Apache 虚拟主机的分类
(1)基于 IP 地址的虚拟主机: 一台服务器上使用不同 IP 地址对应不同站点
(2)基于域名的虚拟主机: 一台服务器上使用不同的域名对应不同站点
(3)基于端口的虚拟主机: 一台服务器上使用不同的端口对应不同站点
4Apache 基于 IP 的虚拟主机配置方法
(1)创建 web1 和 web2 站点的目录和网页文件
- [root@localhost ~]# mkdir -p /www/web{1..2}/logs/
- [root@localhost ~]#echo 'www.web1.com'>/www/web1/index.html
- [root@localhost ~]# echo 'www.web2.com'>/www/web2/index.html
- [root@localhost ~]# tree /www
- /www
- web1
- index.html
- logs
- web2
- index.html
- logs
(2)修改 Apache 相关配置文件
[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf
Include conf/extra/httpd-vhosts.conf 删除这一行的注释
- [root@localhost ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
- <VirtualHost 192.168.115.150:80>
- DocumentRoot "/www/web1"
- ServerName www.web1.com
- ServerAlias web1.com
- ErrorLog "/www/web1/logs/web1_error.log"
- CustomLog "/www/web1/logs/web1_access.log" common
- <Directory "/www/web1">
- Options Indexes
- AllowOverride None
- Require all granted
- </Directory>
- </VirtualHost>
- <VirtualHost 192.168.115.160:80>
- DocumentRoot "/www/web2"
- ServerName www.web2.com
- ErrorLog "/www/web2/logs/web2_error.log"
- CustomLog "/www/web2/logs/web2_access.log" common
- <Directory "/www/web2">
- Options Indexes
- AllowOverride None
- Require all granted
- </Directory>
- </VirtualHost>
- [root@localhost ~]# /usr/local/apache2/bin/apachectl -t 检查语法
- Syntax OK
- [root@localhost ~]# systemctl restart httpd 重启服务
(3)浏览器或 Linux 中进行访问测试
为了方便我这里直接在 Linux 中测试了
- [root@localhost ~]# curl 192.168.115.150
- www.web1.com
- [root@localhost ~]# curl 192.168.115.160
- www.web2.com
5Apache 虚拟主机基于域名配置方法
(1)创建 web1 和 web2 站点的目录和网页文件
- [root@localhost ~]# mkdir -p /www/web{1..2}/logs/
- [root@localhost ~]#echo 'www.web1.com'>/www/web1/index.html
- [root@localhost ~]# echo 'www.web2.com'>/www/web2/index.html
- [root@localhost ~]# tree /www
- /www
- web1
- index.html
- logs
- web2
- index.html
- logs
(2)修改 Apache 相关配置文件
[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf
Include conf/extra/httpd-vhosts.conf 删除这一行的注释
- [root@localhost ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
- <VirtualHost www.web1.com:80>
- DocumentRoot "/www/web1"
- ServerName www.web1.com
- ServerAlias web1.com
- ErrorLog "/www/web1/logs/web1_error.log"
- CustomLog "/www/web1/logs/web1_access.log" common
- <Directory "/www/web1">
- Options Indexes
- AllowOverride None
- Require all granted
- </Directory>
- </VirtualHost>
- <VirtualHost www.web2.com:80>
- DocumentRoot "/www/web2"
- ServerName www.web2.com
- ErrorLog "/www/web2/logs/web2_error.log"
- CustomLog "/www/web2/logs/web2_access.log" common
- <Directory "/www/web2">
- Options Indexes
- AllowOverride None
- Require all granted
- </Directory>
- </VirtualHost>
- [root@localhost ~]# systemctl restart httpd
(3)浏览器或 Linux 中进行访问测试
为了方便我这里直接在 Linux 中测试了
- [root@localhost ~]# curl www.web1.com
- www.web1.com
- [root@localhost ~]# curl www.web2.com
- www.web2.com
6Apache 虚拟主机基于端口配置方法
(1)创建 web1 和 web2 站点的目录和网页文件
- [root@localhost ~]# mkdir -p /www/web{1..2}/logs/
- [root@localhost ~]#echo 'www.web1.com'>/www/web1/index.html
- [root@localhost ~]# echo 'www.web2.com'>/www/web2/index.html
- [root@localhost ~]# tree /www
- /www
- web1
- index.html
- logs
- web2
- index.html
- logs
(2)修改 Apache 相关配置文件
[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf
Include conf/extra/httpd-vhosts.conf 删除这一行的注释
Listen 80 添加监听的端口
- Listen 8080
- [root@localhost ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
- <VirtualHost *:80>
- DocumentRoot "/www/web1"
- ServerName www.web1.com
- ServerAlias web1.com
- ErrorLog "/www/web1/logs/web1_error.log"
- CustomLog "/www/web1/logs/web1_access.log" common
- <Directory "/www/web1">
- Options Indexes
- AllowOverride None
- Require all granted
- </Directory>
- </VirtualHost>
- <VirtualHost *:8080>
- DocumentRoot "/www/web2"
- ServerName www.web2.com
- ErrorLog "/www/web2/logs/web2_error.log"
- CustomLog "/www/web2/logs/web2_access.log" common
- <Directory "/www/web2">
- Options Indexes
- AllowOverride None
- Require all granted
- </Directory>
- </VirtualHost>
- [root@localhost ~]# systemctl restart httpd
(3)浏览器或 Linux 中进行访问测试
为了方便我这里直接在 Linux 中测试了
- [root@localhost ~]# curl 192.168.115.150:80
- www.web1.com
- [root@localhost ~]# curl 192.168.115.150:8080
- www.web2.com
以上就是 Apache 虚拟主机配置的三种不同方法, 可以根据实际需要进行配置
来源: http://www.bubuko.com/infodetail-2546072.html