配置apache虚拟主机,实现一台服务器上运行多个网站
Apache虚拟主机实现方法有三种:
1、通过不同ip地址;
2、通过不同端口号;
3、通过不同域名(常用);
第一种:
1、通过不同ip地址实现
做法一:
[root@x101 ~]# ip addr add 192.168.0.102/24 dev eth0 label eth0:1
[root@x101 ~]# vim /etc/httpd/conf.d/virtualhost.conf
<VirtualHost 192.168.0.101:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/html
ServerNamewww.xiaofeng.com
ErrorLoglogs/www.xiaofeng.com-error_log
CustomLoglogs/www.xiaofeng.com-access_log common
</VirtualHost>
<VirtualHost 192.168.0.102:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot/var/www/html/wcg
ServerName www.xiaofeng.com
ErrorLog logs/www.xiaofeng.com-error_log
CustomLoglogs/www.xiaofeng.com-access_log common
</VirtualHost>
[root@x101 ~]# systemctl restart httpd
[root@x101 ~]# cat /var/www/html/wcg/index.html
welcome to WCG
[root@x101 ~]# cat /var/www/html/index.html
welcome to yuqianwan
做法二:(推荐)
[root@x101 network-scripts]# cp ifcfg-eth0{,:1}
[root@x101 conf.d]# cat /etc/sysconfig/network-scripts/ifcfg-eth0:1
NAME="eth0:1"
DEVICE="eth0:1"
ONBOOT=yes
NETBOOT=yes
IPV6INIT=yes
BOOTPROTO=none
TYPE=Ethernet
IPADDR=192.168.0.102
PREFIX=24
GATEWAY=192.168.0.1
DNS=192.168.0.1
[root@x101 network-scripts]# cd /etc/httpd/conf.d/
[root@x101 conf.d]# vim virtualhost101.conf
<VirtualHost 192.168.0.101:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/html
ServerName www.xiaofeng.com
ErrorLog logs/www.xiaofeng.com-error_log
CustomLog logs/www.xiaofeng.com-access_log common
</VirtualHost>
[root@x101 conf.d]# vim virtualhost102.conf
<VirtualHost 192.168.0.102:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/html/wcg
ServerName www.xiaofeng.com
ErrorLog logs/www.xiaofeng.com-error_log
CustomLog logs/www.xiaofeng.com-access_log common
</VirtualHost>
[root@x101 ~]# systemctl restart httpd
[root@x101 ~]# cat /var/www/html/wcg/index.html
welcome to WCG
[root@x101 ~]# cat /var/www/html/index.html
welcome to yuqianwan
测试:
第二种:
2、通过不同域名访问
[root@x101 conf.d]# vim virtualhost101.conf
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/html
ServerName www.xiaofeng.com
ErrorLog logs/www.xiaofeng.com-error_log
CustomLog logs/www.xiaofeng.com-access_log common
</VirtualHost>
[root@x101 conf.d]# vim virtualhost102.conf
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/html/wcg
ServerName www.bbs.com
ErrorLog logs/www.bbs.com-error_log
CustomLog logs/www.bbs.com-access_log common
</VirtualHost>
[root@x101 conf.d]# systemctl restart httpd
测试:
Windows主机添加hosts解析,C:WindowsSystem32driversetchosts
192.168.0.101 www.bbs.com
192.168.0.101 www.xiaofeng.com
第三种:
3、通过不同端口访问
开启监听端口
[root@x101 conf.d]# vim /etc/httpd/conf/httpd.conf
Listen 80
Listen 81
[root@x101 conf.d]# vim virtualhost81.conf
<VirtualHost 192.168.0.101:81>
或者<VirtualHost *:81>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/html/wcg
ServerName www.xiaofeng.com
ErrorLog logs/www.xiaofeng.com-error_log
CustomLog logs/www.xiaofeng.com-access_log common
</VirtualHost>
[root@x101 conf.d]# vim virtualhost80.conf
<VirtualHost 192.168.0.101:80>
或者<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/html
ServerName www.xiaofeng.com
ErrorLog logs/www.xiaofeng.com-error_log
CustomLog logs/www.xiaofeng.com-access_log common
</VirtualHost>
[root@x101 conf.d]# systemctl restart httpd
访问测试: