您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
马哥linux运维学习笔记-httpd虚拟主机
发布时间:2019-01-13 17:31:36编辑:雪饮阅读()
基于ip的虚拟主机
首先在中心主机配置文件中将配置项"DocumentRoot"注释
然后建立配置文件如:
[root@localhost ~]# cat /etc/httpd/conf.d/virtual.conf
<VirtualHost 192.168.43.175:80>
ServerName hello.magedu.com
DocumentRoot "/www/magedu.com"
</VirtualHost>
<VirtualHost 192.168.43.176:80>
ServerName hello.magedu.com
DocumentRoot "/www/a.org"
</VirtualHost>
这里用了两个ip地址,我们可以给一个网卡绑定两个ip地址,如:
[root@localhost ~]# ip addr add 192.168.43.176/24 dev eth0
[root@localhost ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:ac:c4:fe brd ff:ff:ff:ff:ff:ff
inet 192.168.43.175/24 brd 192.168.43.255 scope global eth0
inet 192.168.43.176/24 scope global secondary eth0
inet6 fe80::20c:29ff:feac:c4fe/64 scope link
valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
然后建立网页文件如:
[root@localhost ~]# cat /www/magedu.com/index.html
this is 192.168.43.175
[root@localhost ~]# cat /www/a.org/index.html
this is 192.168.43.176
如此以来就可以实现访问不同ip地址相同的80端口就会进入不同的虚拟主机。
基于主机名的虚拟主机
虚拟主机配置文件
[root@localhost ~]# cat /etc/httpd/conf.d/virtual.conf
NameVirtualHost 192.168.43.175:80
<VirtualHost 192.168.43.175:80>
ServerName hello.magedu.com
DocumentRoot "/www/magedu.com"
</VirtualHost>
<VirtualHost 192.168.43.175:80>
ServerName a.org
DocumentRoot "/www/a.org"
</VirtualHost>
站点文件
[root@localhost ~]# cat /www/magedu.com/index.html
this is hello.mageedu.com
[root@localhost ~]# cat /www/a.org/index.html
this is a.org
host解析新增
[root@localhost ~]# cat /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
192.168.43.175 hello.magedu.com a.org
重启httpd后测试效果
[root@localhost ~]# elinks -source http://hello.magedu.com
this is hello.mageedu.com
[root@localhost ~]# elinks -source http://a.org
this is a.org
基于端口的虚拟主机
主机中心配置文件:
在listen80后面新增8080的监听如
#Listen 12.34.56.78:80
Listen 80
Listen 8080
#
虚拟主机配置文件
[root@localhost ~]# cat /etc/httpd/conf.d/virtual.conf
#NameVirtualHost 192.168.43.175:80
<VirtualHost 192.168.43.175:80>
ServerName hello.magedu.com
DocumentRoot "/www/magedu.com"
</VirtualHost>
<VirtualHost 192.168.43.175:8080>
ServerName a.org
DocumentRoot "/www/a.org"
</VirtualHost>
网页文件
[root@localhost ~]# cat /www/magedu.com/index.html
this is 80
[root@localhost ~]# cat /www/a.org/index.html
this is 8080
httpd重启后效果
[root@localhost ~]# elinks -source 192.168.43.175
this is 80
[root@localhost ~]# elinks -source 192.168.43.175:8080
this is 8080
htpasswd
用于创建、更新apache授权站点用户的配置文件
当配置文件不存在时则需要使用-c进行创建,否则就可以直接在原文件基础上进行新增用户
[root@localhost ~]# htpasswd -m /etc/httpd/htpasswd xy3
htpasswd: cannot modify file /etc/httpd/htpasswd; use '-c' to create it
[root@localhost ~]# htpasswd -m /etc/httpd/conf/htpasswd xy3
New password:
Re-type new password:
Adding password for user xy3
重复虚拟主机配置
虚拟主机配置(仅仅主机目录不同)
[root@localhost ~]# cat /etc/httpd/conf.d/virtual.conf
<VirtualHost 192.168.43.175:80>
ServerName hello.magedu.com
DocumentRoot "/www/a.org"
</VirtualHost>
<VirtualHost 192.168.43.175:80>
ServerName hello.magedu.com
DocumentRoot "/www/magedu.com"
</VirtualHost>
网页文件
[root@localhost ~]# cat /www/magedu.com/index.html
this is 80
[root@localhost ~]# cat /www/a.org/index.html
this is 8080
效果
[root@localhost ~]# elinks -source 192.168.43.175
this is 8080
结论:在相同虚拟主机配置下,访问结果将会自动匹配虚拟主机列表中第一项虚拟主机
查看httpd服务器状态
在主机中心配置文件中找到<Location /server-status>去除注释并修改为如下
<Location /server-status>
SetHandler server-status
Order deny,allow
</Location>
重启httpd后效果
关键字词:linux,httpd,虚拟主机