标签:Apache虚拟主机分类详解 Apache基于IP虚拟主机配置 Apache基于域名的虚拟主机配置 Apache基于端口的虚拟主机配置
Apache虚拟主机配置详解系统环境:CentOS7
Apache环境:编译安装的httpd-2.4.7
系统限制:关闭了防火墙和selinux
hosts文件中配置以下域名解析
192.168.115.150 www.web1.com
192.168.115.150 www.web2.com
虚拟主机是指在一个机器上运行多个网站(例如:www.web1.com和www.web2.com)。每个站点希望用不同的域名和站点目录,或者是不同端口、不同的IP就需要虚拟主机。
(1)基于IP地址的虚拟主机:一台服务器上使用不同IP地址对应不同站点
(2)基于域名的虚拟主机:一台服务器上使用不同的域名对应不同站点
(3)基于端口的虚拟主机:一台服务器上使用不同的端口对应不同站点
[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
[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 重启服务
为了方便我这里直接在Linux中测试了
[root@localhost ~]# curl 192.168.115.150
www.web1.com
[root@localhost ~]# curl 192.168.115.160
www.web2.com
[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
[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
为了方便我这里直接在Linux中测试了
[root@localhost ~]# curl www.web1.com
www.web1.com
[root@localhost ~]# curl www.web2.com
www.web2.com
[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
[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
为了方便我这里直接在Linux中测试了
[root@localhost ~]# curl 192.168.115.150:80
www.web1.com
[root@localhost ~]# curl 192.168.115.150:8080
www.web2.com
以上就是Apache虚拟主机配置的三种不同方法,可以根据实际需要进行配置。
标签:Apache虚拟主机分类详解 Apache基于IP虚拟主机配置 Apache基于域名的虚拟主机配置 Apache基于端口的虚拟主机配置
原文地址:http://blog.51cto.com/longlei/2093471