参照文章:https://httpd.apache.org/docs/2.2/vhosts/name-based.html
1、安装httpd
yum install httpd -y
2、配置httpd的主配置文件
vi /etc/httpd/conf/httpd.conf输入如下内容:
1 2 3 4 5 6 7 8 9 10 |
…… Listen 80 ServerName www.test.com LoadModule vhost_alias_module modules/mod_vhost_alias.so #base on port 80 vhost begin NameVirtualHost *:80 Include http_conf.d/*.conf #base on port 80 vhost end …… |
3、创建虚拟主机文件
mkdir /etc/httpd/http_conf.d
cd /etc/httpd/http_conf.d
vi 0-software.tyumen.cn.conf #匹配域名software.tyumen.cn时候,进入第一个目录服务
1 2 3 4 5 6 7 8 |
<VirtualHost *:80> ServerAdmin tyumen@zhoufengjie.cn DocumentRoot "/data/www/software" ServerName software.tyumen.cn #ServerAlias download.tyumen.cn ErrorLog "logs/software.tyumen.cn-error.log" CustomLog "logs/software.tyumen.cn-access.log" common </VirtualHost> |
vi 1-mirror.tyumen.cn.conf #匹配域名mirror.tyumen.cn时候,进入第二个目录服务
1 2 3 4 5 6 7 8 |
<VirtualHost *:80> ServerAdmin tyumen@zhoufengjie.cn DocumentRoot "/data/www/yummirror" ServerName mirror.tyumen.cn #ServerAlias yum.tyumen.cn ErrorLog "logs/mirror.tyumen.cn-error.log" CustomLog "logs/mirror.tyumen.cn-access.log" common </VirtualHost> |
vi z-default.conf #域名不匹配的话就拒绝提供服务
1 2 3 4 5 6 7 8 |
<VirtualHost *:80> ServerName fail ServerAlias * <Directory "/data/www"> Order allow,deny Deny from All </Directory> </VirtualHost> |
3、重启httpd进程
centos6: service restart httpd
centos7:systemctl restart httpd