linux下httpd服务阶段实验
实验需求
配置基于域名的虚拟主机,必须用以下域名访问
www.163.com
www.sina.com
要求这两个虚拟站点分别存放日志文件
只有访问www.163.com/news时,要求输入账户news密码123后能看到/usr/html目录下test.htm网页内容
只有163.com有news别名,sina.com没有news别名
配置只允许网段中一台主机能访问www.sina.com
配置仅不允许能访问sina的那台主机访问www.163.com
服务器端
1.安装所需的软件包
1 | [root@rhel5 ~]# yum install httpd -y |
2.添加虚拟主机的配置文件
1 2 3 4 | [root@rhel5 ~]# cd /etc/httpd/conf.d/ [root@rhel5 conf.d]# vim vhost.conf [root@rhel5 conf.d]# vim vhost.conf [root@rhel5 conf.d]# cat vhost.conf |
NameVirtualHost 10.0.1.8:80 #指定主机ip和监听端口
DocumentRoot /var/www/html/yum #虚拟主机的文件家目录
ServerName www.yum.com #虚拟主机的域名
ErrorLog logs/yum.com-error_log #虚拟主机的日志文件
CustomLog logs/yum.com-access_log common
DocumentRoot /var/www/html/163
ServerName www.163.com
alias /news /usr/html #设置别名
AuthName "please input password "
AuthType Basic
AuthUserFile /etc/httpd/.htpasswd
require user news
Order deny,allow #访问控制
Deny from 10.0.1.238
ErrorLog logs/163.com-error_log
CustomLog logs/163.com-access_log common
DocumentRoot /var/www/html/sina
ServerName www.sina.com
Order allow,deny
Allow from 10.0.1.238
ErrorLog logs/sina.com-error_log
CustomLog logs/sina.com-access_log common
3.添加所需要的目录和文件夹
#创建各个虚拟主机的根目录
1 2 3 | [root@rhel5 named]# cd /var/www/html/ [root@rhel5 html]# mkdir yum 163 sina [root@rhel5 conf.d]# mkdir /usr/html |
#添加标志性文件
1 2 3 4 | [root@rhel5 html]# echo "this is a yum" >yum/index.html [root@rhel5 html]# echo "this is a 163" >163/index.html [root@rhel5 html]# echo "this is a sina" >sina/index.html [root@rhel5 conf.d]# echo "this is a 163 news" >/usr/html/index.html |
#添加news目录用户认知
1 | [root@rhel5 conf.d]# htpasswd -c -b /etc/httpd/.htpasswd news 123 Adding password for user /etc/httpd/.htpasswd |
#挂载光盘镜像到www.163.com的家目录
1 | [root@rhel5 conf.d]# mount /dev/cdrom /var/www/html/yum |
4.修改dns主配置文件(这里使用我上次配置好的dns服务器,新添加3个域就行了)
1 2 | [root@rhel5 conf.d]# vim /var/named/chroot/etc/named.conf [root@rhel5 conf.d]# tail -13 /var/named/chroot/etc/named.conf |
zone "yum.com" IN{
type master;
file "yum.com.zone";
};
zone "163.com" IN{
type master;
file "163.com.zone";
};
zone "sina.com" IN{
type master;
file "sina.com.zone";
};
5.添加dns区域(zone)配置文件
1 2 3 4 | [root@rhel5 conf.d]# cd /var/named/chroot/var/named/ [root@rhel5 named]# cp -a sw.com.zone yum.com.zone [root@rhel5 named]# cp -a sw.com.zone 163.com.zone [root@rhel5 named]# cp -a sw.com.zone sina.com.zone |
#yum.com配置文件
1 2 3 4 5 6 7 8 9 10 11 12 | [root@rhel5 named]# vim yum.com.zone [root@rhel5 named]# cat yum.com.zone $TTL 86400 @ IN SOA www.yum.com. root ( 2013080803 ; Serial 28800 ; Refresh 14400 ; Retry 3600000 ; Expire 86400 ) ; Minimum @ IN NS www.yum.com. @ IN A 10.0.1.8 www IN A 10.0.1.8 |
#163.com配置文件
1 2 3 4 5 6 7 8 9 10 11 12 | [root@rhel5 named]# vim 163.com.zone [root@rhel5 named]# cat 163.com.zone $TTL 86400 @ IN SOA www.163.com. root ( 2013080803 ; Serial 28800 ; Refresh 14400 ; Retry 3600000 ; Expire 86400 ) ; Minimum @ IN NS www.163.com. @ IN A 10.0.1.8 www IN A 10.0.1.8 |
#sina.com配置文件
[root@rhel5 named]# vim sina.com.zone
[root@rhel5 named]# cat sina.com.zone
1 2 3 4 5 6 7 8 9 10 | $TTL 86400 @ IN SOA www.sina.com. root ( 2013080803 ; Serial 28800 ; Refresh 14400 ; Retry 3600000 ; Expire 86400 ) ; Minimum @ IN NS www.sina.com. @ IN A 10.0.1.8 www IN A 10.0.1.8 |
6.重启服务
#重启dns服务
1 2 3 | [root@rhel5 named]# service named restart 停止 named: [确定] 启动 named: [确定] |
#重启httpd服务
1 2 3 | [root@rhel5 html]# service httpd restart 停止 httpd: [失败] 启动 httpd: [确定] |
客户端
#修改默认dns服务器
1 2 3 | [root@localhost ~]# vim /etc/resolv.conf [root@localhost ~]# cat /etc/resolv.conf nameserver 10.0.1.8 |
#使用elinks命令简单测试是否能查看相应域名中的内容
1 2 3 4 5 6 | [root@localhost ~]# elinks --dump www.sina.com this is a sina [root@localhost ~]# elinks --dump www.163.com this is a 163 [root@localhost ~]# elinks --dump www.yum.com this is a yum |