LAMP系列之Apache虚拟主机详解
配置虚拟主机前的一些必备的开胃小菜:1、2.2版本和2.4版本的关于开启虚拟主机的不同区别
apache2.2x系列的版本,需要启用一个指令NameVirtualHost来开启虚拟主机的功能
Apache2.4 不需要开启NameVirtualHost
2、定义站点的核心要素有哪些?
如在同一个物理主机上,提供了两个不同的站点,每一个站点应该独立具有的资源或者配置、甚至是信息应该有哪些?
(1)DocumentRoot
(2)ip地址
(3)ServerName ,每个主机都应该有主机名
(4)对于任何一个目录都应该封装一个容器
(5)Alias 路径别名
(6) ServerAlias服务器别名
(7)Errorlog
(8)CustomLog
(9)
(10)ScriptsAlias 脚本别名
例子:
DocumentRoot/www/test1.com/
options
AllowOVerrride
#Directory封装定义用户访问每一个本地文件系统目录下的文件该具有什么样的访问属性
#而Location则用来定义RUL
Alias
期望两个名称访问的是同一个主机,怎么办?ServerAlias,服务器端别名来解决
3、虚拟主机的定义,使用VirtualHost标签封装起来即可;要需要注意虚拟主机和中心主机不能同时使用,要使用虚拟主机首先得取消中心主机,注释中心主机的DocumentRoot即可
指令1......
指令2......
1)基于IP的Host的写法:ip:端口的格式即可
ip1:80
ip2:80
2)基于端口的Host的写法:
ip:80
ip:8080
3)基于域名的Host写法:
ip:80
*80#当前主机上每一个ip都监听
基于域名的ServerName一定是不同的Apache虚拟主机的具体实现
1)首先需要取消中心主机
2)直接在httpd.conf里面添加VirutalHost标签对定义即可,或者在conf.d目录下新建文件virtual.conf文件也可以,这样将来管理起来更方便
# vim /etc/httpd/conf.d/virtual.conf
服务器端代码实现:
#定义第一个虚拟主机
ServerName test1.com
DocumentRoot "/data/html/test1"
#定义第二个虚拟主机
ServerName test2.com
DocumentRoot "/data/html/test2"
1、基于ip的虚拟主机的实现
1>创建测试目录文件
# mkdir /data/html/{test1,test2} -pv
mkdir: created directory `/data/html'
mkdir: created directory `/data/html/test1'
mkdir: created directory `/data/html/test2'
2>创建测试页面文件
# echo "This is a test1 site ">> index.html
# cd ../test2
# echo "This is a test2 site ">> index.html
3>创建ip地址别名
# ip addr add 192.168.1.11/24 dev eth0
# ip addr show
1: lo:mtu 16436 qdisc noqueue state UNKNOWN
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:mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:fb:17:2d brd ff:ff:ff:ff:ff:ff
inet 192.168.1.10/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.11/24 scope global secondary eth0
inet6 fe80::20c:29ff:fefb:172d/64 scope link
valid_lft forever preferred_lft forever
4>测试ip别名的连通性
# ping 192.168.1.11 -c 2
PING 192.168.1.11 (192.168.1.11) 56(84) bytes of data.
64 bytes from 192.168.1.11: icmp_seq=1 ttl=64 time=0.599 ms
64 bytes from 192.168.1.11: icmp_seq=2 ttl=64 time=0.067 ms
--- 192.168.1.11 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1003ms
rtt min/avg/max/mdev = 0.067/0.333/0.599/0.266 ms
5>检查配置文件语法并重启服务
# httpd -t
Syntax OK
# service httpd restart
Stopping httpd:
Starting httpd:
6>客户端测试
2、基于端口的虚拟主机的实现
1>配置文件:
ServerName test3.com
DocumentRoot "/data/html/test3"
2>创建相关目录:
# mkdir /data/html/test3 -pv
mkdir: created directory `/data/html/test3'
# echo "This is a test3 website" >>index.html
3>重启服务
# httpd -t
Syntax OK
# service httpd restart
Stopping httpd:
Starting httpd:
4>基于端口的虚拟主机要需要在httpd.conf主配置文件中指定
5>验证
# httpd -t
Syntax OK
# service httpd restart
Stopping httpd:
Starting httpd:
6>客户端访问测试
3、基于主机名的虚拟主机的实现
NameVirtualHost 192.168.1.10:80
ServerName test4.com
DocumentRoot "/data/html/test4"
# mkdir /data/html/test4
# cd /data/html/test4
# echo "This istest4 web site" >index.html
在客户端设置基于/etc/hosts主机名的解析:
%SystemRoot%\system32\drivers\etc\
使用ip地址来访问,默认将返回基于名称主机的第一个
补充扩展内容1:为虚拟主机增加日志
CustomLog"/etc/httpd/conf/test1/access_log" combined
创建日志目录:
# mkdir /etc/httpd/conf/test1 -pv
mkdir: created directory `/etc/httpd/conf/test1'
# mkdir /etc/httpd/conf/test4 -pv
mkdir: created directory `/etc/httpd/conf/test4'
测试:
# httpd -t
Syntax OK
# service httpd restart
Stopping httpd:
Starting httpd:
# service httpd restart
Stopping httpd:
Starting httpd:
客户端测试日志分析:
# tail -f /etc/httpd/conf/test1/access_log
192.168.1.3 - - "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36"
192.168.1.3 - - "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36"
192.168.1.3 - - "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36"
192.168.1.3 - - "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36"
192.168.1.3 - - "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36"
192.168.1.3 - - "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36"
192.168.1.3 - - "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36"
192.168.1.3 - - "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36"
192.168.1.3 - - "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36"
192.168.1.3 - - "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36"
192.168.1.3 - - "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36"
定义test4.com下的/data/html/test4拒绝某个ip访问
13
14 options none
15 AllowOverride none
16 Order deny, allow
17 deny from 192.168.1.100
18 补充扩展内容2:基于虚拟主机的用户认证
只需要在Directory里面添加以下语句即可
Options none
AllowOverride AuthConfig
AuthType Basic
AuthName "Allow trust people"
AuthUserFile "/etc/httpd/.htpasswd"
Required User test1
Requiredvalid-user
附录:基于上文的所有配置文件及配置文件截图
1NameVirtualHost 192.168.1.10:80
2 #定义第一个虚拟主机
3
4 ServerName www.test1.com
5 DocumentRoot"/data/html/test1"
6 CustomLog"/etc/httpd/conf/test1/access_log"combined
7
8
9
10 ServerName www.test4.com
11 DocumentRoot "/data/html/test4"
12 CustomLog "/etc/httpd/conf/test4/access_log" combined
13
14 options none
15 AllowOverride none
16 Order deny, allow
17 deny from 192.168.1.100
18
19
20
21 #定义第二个虚拟主机
22
23
24 ServerName www.test2.com
25 DocumentRoot "/data/html/test2"
26 2014-04-22-v1.0第一版,后续更新。。。。。。由于本人技术有限,文中难免有错误或遗漏之处,请各位网友积极提出宝贵意见和建议,也希望成为各位技术爱好者的好朋友,谢谢。
页:
[1]