Apache虚拟主机配置详解
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Apache虚拟主机配置详解相关的知识,希望对你有一定的参考价值。
Apache虚拟主机配置详解
1、配置环境说明
系统环境:CentOS7
Apache环境:编译安装的httpd-2.4.7
系统限制:关闭了防火墙和selinux
hosts文件中配置以下域名解析
192.168.115.150 www.web1.com
192.168.115.150 www.web2.com
2、Apache虚拟主机简介
虚拟主机是指在一个机器上运行多个网站(例如:www.web1.com和www.web2.com)。每个站点希望用不同的域名和站点目录,或者是不同端口、不同的IP就需要虚拟主机。
3、Apache虚拟主机的分类
(1)基于IP地址的虚拟主机:一台服务器上使用不同IP地址对应不同站点
(2)基于域名的虚拟主机:一台服务器上使用不同的域名对应不同站点
(3)基于端口的虚拟主机:一台服务器上使用不同的端口对应不同站点
4、Apache基于IP的虚拟主机配置方法
(1)创建web1和web2站点的目录和网页文件
[[email protected] ~]# mkdir -p /www/web{1..2}/logs/
[[email protected] ~]#echo 'www.web1.com'>/www/web1/index.html
[[email protected] ~]# echo 'www.web2.com'>/www/web2/index.html
[[email protected] ~]# tree /www
/www
├── web1
│ ├── index.html
│ └── logs
└── web2
├── index.html
└── logs
(2)修改Apache相关配置文件
[[email protected] ~]# vim /usr/local/apache2/conf/httpd.conf
Include conf/extra/httpd-vhosts.conf 删除这一行的注释
[[email protected] ~]# 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>
[[email protected] ~]# /usr/local/apache2/bin/apachectl -t 检查语法
Syntax OK
[[email protected] ~]# systemctl restart httpd 重启服务
(3)浏览器或Linux中进行访问测试
为了方便我这里直接在Linux中测试了
[[email protected] ~]# curl 192.168.115.150
www.web1.com
[[email protected] ~]# curl 192.168.115.160
www.web2.com
5、Apache虚拟主机基于域名配置方法
(1)创建web1和web2站点的目录和网页文件
[[email protected] ~]# mkdir -p /www/web{1..2}/logs/
[[email protected] ~]#echo 'www.web1.com'>/www/web1/index.html
[[email protected] ~]# echo 'www.web2.com'>/www/web2/index.html
[[email protected] ~]# tree /www
/www
├── web1
│ ├── index.html
│ └── logs
└── web2
├── index.html
└── logs
(2)修改Apache相关配置文件
[[email protected] ~]# vim /usr/local/apache2/conf/httpd.conf
Include conf/extra/httpd-vhosts.conf 删除这一行的注释
[[email protected] ~]# 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>
[[email protected] ~]# systemctl restart httpd
(3)浏览器或Linux中进行访问测试
为了方便我这里直接在Linux中测试了
[[email protected] ~]# curl www.web1.com
www.web1.com
[[email protected] ~]# curl www.web2.com
www.web2.com
6、Apache虚拟主机基于端口配置方法
(1)创建web1和web2站点的目录和网页文件
[[email protected] ~]# mkdir -p /www/web{1..2}/logs/
[[email protected] ~]#echo 'www.web1.com'>/www/web1/index.html
[[email protected] ~]# echo 'www.web2.com'>/www/web2/index.html
[[email protected] ~]# tree /www
/www
├── web1
│ ├── index.html
│ └── logs
└── web2
├── index.html
└── logs
(2)修改Apache相关配置文件
[[email protected] ~]# vim /usr/local/apache2/conf/httpd.conf
Include conf/extra/httpd-vhosts.conf 删除这一行的注释
Listen 80 添加监听的端口
Listen 8080
[[email protected] ~]# 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>
[[email protected] ~]# systemctl restart httpd
(3)浏览器或Linux中进行访问测试
为了方便我这里直接在Linux中测试了
[[email protected] ~]# curl 192.168.115.150:80
www.web1.com
[[email protected] ~]# curl 192.168.115.150:8080
www.web2.com
以上就是Apache虚拟主机配置的三种不同方法,可以根据实际需要进行配置。
以上是关于Apache虚拟主机配置详解的主要内容,如果未能解决你的问题,请参考以下文章
Apache httpd(apache2)服务配置详解,Mac下设置虚拟主机部署多个web项目,及反向代理部署Java项目