一、目标
在centos6.9下配置Web服务器Apache2.2.15版本,添加多个Web站点多域名,且共用80端口。由于项目是几年前搭建的,版本有点低,您可以自行升级版本。
二、说明
Linux版本CentOS release 6.9 (Final):
[root@ etc]# cat /etc/redhat-release
CentOS release 6.9 (Final)
Apache版本Apache/2.2.15 (Unix):
[root@ etc]# httpd -v
Server version: Apache/2.2.15 (Unix)
Server built: Oct 19 2017 16:43:38
三、步骤
1.修改Apache httpd.conf 默认配置文件
本机配置目录/etc/httpd/conf/httpd.conf
Apache需要重定向功能,可在httpd.conf开启mod_rewrite模块功能:
LoadModule rewrite_module modules/mod_rewrite.so
#vhost conf file
Include "vhost/*.conf"
2.修改vhost中虚拟站点配置文件
在配置目录/etc/httpd/conf同级目录下创建vhost文件夹,您可以通过修改vhost里的xxx.conf文件来增加、删除或修改您的二级域名和所指向的实际路径,不需要重启Apache服务。:
[root@ httpd]# pwd
/etc/httpd
[root@ httpd]# ll
total 12
drwxr-xr-x 2 root root 4096 Jun 28 19:46 conf
drwxr-xr-x 2 root root 4096 Oct 24 2017 conf.d
lrwxrwxrwx 1 root root 19 Oct 24 2017 logs -> ../../var/log/httpd
lrwxrwxrwx 1 root root 29 Oct 24 2017 modules -> ../../usr/lib64/httpd/modules
lrwxrwxrwx 1 root root 19 Oct 24 2017 run -> ../../var/run/httpd
drwxr-xr-x 2 root root 4096 Jun 28 19:47 vhost
web1.conf文件可以自定义
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/code/mall
ServerName www.xxx.cn
ServerAlias xxx.cn
<Directory "/var/code/mall">
Options Indexes FollowSymLinks
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
web2.conf文件可以自定义
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/code/mall
ServerName mall.phpersay.cn
<Directory "/var/code/mall">
Options Indexes FollowSymLinks
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
web3.conf文件可以自定义
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/code/blog
ServerName www.phpersay.cn
<Directory "/var/code/blog">
Options Indexes FollowSymLinks
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
3.重启apache让配置生效
重启WEB服务器Apache的进程httpd
[root@ etc]#service httpd restart
4.测试效果
web1站点访问正常,web2、web3访问的结果始终是web1的页面。配置存在问题。
四、遇到的问题
网友有云:hosts文件需要映射
增加配置
127.0.0.1 phpersay.com
127.0.0.1 mall.phpersay.com
127.0.0.1 xxxx.cn
重启网络
[root@ etc]# /etc/init.d/network
重启web服务器Apache的进程httpd
[root@ etc]#service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
然并非有用答案。
排查阶段:
首先确认语法拼写是否有误,
无误。
问题原因定位:
开启Apache的虚拟主机功能
#NameVirtualHost *:80
其他错误:
Starting httpd: [Mon Jun 28 23:37:17 2021] [warn] NameVirtualHost xxx.xx.xx.110:80 has no VirtualHosts
[root@iZ23hnwptztZ etc]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [Mon Jun 28 18:40:25 2021] [warn] NameVirtualHost xxx.xx.xx.110:80 has no VirtualHosts
[ OK ]
引起这个警告的原因一般是NameVirtualHost语法含有错误 ,因为对于同一台主机支持多个虚拟主机的情况,只需要命名一次NameVirtualHost就可以了,如果在每个虚拟主机配置文件中都加上NameVirtualHost *:80 ,就会报这个警告。
httpd.conf
NameVirtualHost *:80
NameVirtualHost xxx.xx.xx.xxx:80
vhost/web.conf
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/code/blog
ServerName www.phpersay.cn
<Directory "/var/code/blog">
Options Indexes FollowSymLinks
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
错误原因就是在httpd.conf文件中,重复定义了NameVirtualHost,删除该行,重启Apache即可。