Apache配置虚拟主机的三种实现方式
2017-12-04 来源:互联网
不管你是进行Apache配置虚拟主机,还是其他配置主机,启用虚拟主机的准备工作是必不可少的,首先需要安装httpd和禁用默认的主机模式。
安装httpd:
[rootmail@ httpd]# yum install httpd -y
禁用默认的主机模式:
[root@mail httpd]# vim /etc/httpd/conf/httpd.conf
注释下面这行内容
#DocumentRoot "/var/www/html"
接下来,开始Apache配置虚拟主机:
一、基于IP的虚拟主机
1、为主机添加多个IP
[root@localhost conf.d]# ip addr show dev eth0 #查看原有IP
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:77:77:7d brd ff:ff:ff:ff:ff:ff
inet 192.168.137.200/24 brd 192.168.137.255 scope global eth0
inet6 fe80::20c:29ff:fe77:777d/64 scope link
valid_lft forever preferred_lft forever
[root@localhost conf.d]# ip addr add 192.168.137.201/24 dev eth0 #添加一个IP
[root@localhost conf.d]# ip addr show dev eth0 #查看添加后的IP信息, 此时有2个IP地址了。 200,201
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:77:77:7d brd ff:ff:ff:ff:ff:ff
inet 192.168.137.200/24 brd 192.168.137.255 scope global eth0
inet 192.168.137.201/24 scope global secondary eth0
inet6 fe80::20c:29ff:fe77:777d/64 scope link
valid_lft forever preferred_lft forever
2、添加虚拟主机配置文件
[root@mail conf.d]# cd /etc/httpd/conf.d/ #进入配置目录
[root@mail conf.d]# vim virtualhost.conf #创建一个配置文件, 编辑内容如下
[root@mail conf.d]# cat virtualhost.conf #查看并检查配置文件
<VirtualHost 192.168.137.200:80>
DocumentRoot "/var/www/test200"
ServerName www.test200.com
</VirtualHost>
<VirtualHost 192.168.137.201:80>
DocumentRoot "/var/www/test201"
ServerName www.test201.com
</VirtualHost>
[root@mail conf.d]# cd /var/www #切换目录
[root@mail www]# mkdir test200 test201 #创建目录
[root@mail www]# echo test200 >>./test200/index.html #创建IP为200的主页
[root@mail www]# echo test201 >>./test201/index.html #创建IP为200的主页
3、接下来开始测试
[root@localhost www]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[ OK ]
我们这里使用elinks进行测试, 当然用浏览器测试是一样的
[root@localhost conf]# elinks -source 192.168.137.200
test200
[root@localhost conf]# elinks -source 192.168.137.201
test201
二、基于端口的虚拟主机配置
1、在主配置文件添加监听端口
[root@localhost conf]# vim /etc/httpd/conf/httpd.conf
在原有行Listen 80行的基础上, 在添加一行
Listen 8080
2、添加8080的端口虚拟配置
[root@localhost conf.d]# cat virtualhost.conf
<VirtualHost 192.168.137.200:80>
DocumentRoot "/var/www/test200"
ServerName www.test200.com
</VirtualHost>
<VirtualHost 192.168.137.201:80>
DocumentRoot "/var/www/test201"
ServerName www.test201.com
</VirtualHost>
#下面的内容是在上面的配置的基础上添加的。
<VirtualHost 192.168.137.201:8080>
DocumentRoot "/var/www/test201-8080"
ServerName www.test201-8080.com
</VirtualHost>
[root@localhost conf.d]# cd /var/www/ #切换目录
[root@localhost www]# mkdir test201-8080 #创建目录
[root@localhost www]# echo "test201-8080" >>./test201-8080/index.html #创建主页
3、测试
[root@localhost www]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[ OK ]
[root@localhost conf]# elinks -source 192.168.137.201:80
test201
[root@localhost conf]# elinks -source 192.168.137.201
test201
[root@localhost conf]# elinks -source 192.168.137.201:8080
test201-8080
三、基于域名的虚拟主机配置
1、添加域名的虚拟主机配置
[root@localhost conf.d]# vim virtualhost.conf #编辑虚拟主机配置文件
[root@localhost conf.d]# cat virtualhost.conf #内容如下, 红色部分是在上面的基础上添加的
NameVirtualHost 192.168.137.200:80
<VirtualHost 192.168.137.200:80>
DocumentRoot "/var/www/test200"
ServerName www.test200.com
</VirtualHost>
<VirtualHost 192.168.137.200:80>
DocumentRoot "/var/www/test200net"
ServerName www.test200.net
</VirtualHost>
<VirtualHost 192.168.137.201:80>
DocumentRoot "/var/www/test201"
ServerName www.test201.com
</VirtualHost>
<VirtualHost 192.168.137.201:8080>
DocumentRoot "/var/www/test2018080"
ServerName www.test2018080.com
</VirtualHost>
[root@localhost conf.d]# !ser
service httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[ OK ]
[root@localhost conf.d]# cd /var/www #切换目录
[root@localhost www]# mkdir test200net #创建目录
[root@localhost www]# echo "test200net" >>./test200net/index.html #创建主页
2、测试
这里需要添加域名解析,没有提供dns去解析,简单的使用hosts文件区解析就可以了。
[root@localhost www]# vim /etc/hosts 编辑hosts文件, 添加两行
[root@localhost www]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.137.200 www.test200.com
192.168.137.200 www.test200.net
接下来可以测试了
[root@localhost www]# elinks -source http://www.test200.com #测试.com域
test200
[root@localhost www]# elinks -source http://www.test200.net #测试.net域
test200net
西部数码服务器:https://www.west.cn/services/server/
标签: dns https 服务器 配置虚拟主机 西部数码 虚拟主机 虚拟主机配置 域名 域名解析
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:服务器软件防火墙推荐
下一篇:哪些因素影响着服务器的价格