您的位置:首页 > 理论基础 > 计算机网络

初学linux网络服务之HTTP服务实验

2014-06-16 22:33 531 查看
实验拓扑:
Linux Client
-----RHEL5.9(vmnet1)----------(vmnet1)
Win7 Client

实验一:查看默认HTTP配置
找到默认红帽欢迎页面
(/etc/httpd/conf/httpd.conf ---->Include ----> /etc/httpd/conf.d ----> welcome.conf ----> /var/www/error/noindex.html)
前提条件:
1、配置IP
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 //设置网卡参数
# Intel Corporation 82545EM Gigabit Ethernet Controller (Copper)
DEVICE=eth0
BOOTPROTO=static //静态手动分配地址
ONBOOT=yes
HWADDR=00:0c:29:5d:a8:80
IPADDR=192.168.10.253
NETMASK=255.255.255.0
2、配置主机名
[root@localhost ~]# cat /etc/sysconfig/network //查看主机名
NETWORKING=yes
NETWORKING_IPV6=yes
HOSTNAME=web01.tarena.com
3、修改hosts文件
[root@localhost ~]# cat /etc/hosts //修改host文件,进行地址转换
# 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.10.253 web01.tarena.com web01
[root@localhost ~]# service network restart //重启网络服务
[root@localhost ~]# chkconfig network on //设置网络服务开机自启
4、软件包的安装
[root@localhost ~]# rpm -q httpd //查询httpd包有无安装
package httpd is not installed
[root@localhost ~]# yum -y install httpd //使用yum库安装httpd
5、启动服务
[root@localhost ~]# service httpd restart //启动httpd服务
[root@localhost ~]# chkconfig httpd on //设置httpd服务开机自启动

试验二:基本HTTP服务器的配置
Web服务器域名:www.tarena.com
默认首页包括:index.html、index.php
开启保持连接
确认默认httpd是否支持php
网站用老师提供的test_web.zip测试

服务器操作:
1、备份主配置文件
[root@localhost ~]# cd /etc/httpd/conf //进入主配置文件目录
[root@localhost conf]# cp httpd.conf httpd.conf.bak //备份配置文件防止配置错误后可恢复
2、修改主配置文件
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf //编辑主配置文件
...
74 KeepAlive On //保持连接
...
265 ServerName www.tarena.com:80 //web服务器域名
...
391 DirectoryIndex index.html index.php //默认首页
...
3、启动服务
[root@localhost ~]# service httpd restart //重启服务
[root@localhost ~]# cd /root/Desktop/
[root@localhost Desktop]# unzip test_web.zip //解压测试文件
[root@localhost Desktop]# mv jiajutiyan/* /var/www/html/ //将测试网站移动到默认工作目录下
4、编写测试php页面
[root@localhost ~]# vim /var/www/html/test.php
<?php
phpinfo();
?>

测试:
1、在客户端hosts文件指定
192.168.10.253www.tarena.comwww
2、打开浏览器 http://www.tarena.com http://www.tarena.com/test.php

3、linux客户机验证
ip地址设置和host文件设置参考服务器配置,
[root@svr3 ~]# 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.10.253 www.tarena.com www

在浏览器中输入www.tarena.com 出现测试网站的主页说明设置成功
访问php显示文件内容,表示默认不支持php
4、win7客户机验证
1)手动设置IP和服务器在同一网段
2)设置hosts文件
C:\Windows\System32\drivers\etc\hosts //修改host文件
localhost name resolution is handled within DNS itself.
#127.0.0.1 localhost
#::1 localhost
192.168.10.253 www.tarena.com www

浏览器访问www.tarena.com 出现测试网站的主页说明设置成功
实验三: 设置只允许 IP地址为192.168.10.21的用户访问www.tarena.com
设置允许所有用户访问www.tarena.com/authdir/index.html
1、编辑主配置文件

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf //编辑httpd主配置文件
...
306 <Directory "/var/www/html"> //站点目录
...
333 Order allow,deny //定义控制顺序
334 # Allow from all //注释掉默认允许所有
335 Allow from 192.168.10.21 //添加只允许此IP访问
336 </Directory>
...

2、新建authdir站点
[root@web01 ~]# mkdir /var/www/html/authdir //新建目录authdir
[root@web01 ~]# echo "http://www.tarena.com/authdir/index.html" > /var/www/html/authdir/index.html //输出字符到authdir/index.html

[root@web01 ~]# vim /etc/httpd/conf/httpd.conf //编辑主配置文件
...
337 <Directory /var/www/html/authdir>
338 Order allow,deny //设置控制顺序
339 Allow from all //允许所有用户
340 </Directory>
[root@localhost ~]# service httpd restart //重启httpd服务
[root@localhost ~]# tail /var/log/httpd/error_log //如遇访问错误可在服务器查看日志

在不同客户端测试
1、在linux客户端测试
1)linux客户机IP地址192.168.10.20 无法访问www.tarena.com,
2)浏览器地址栏输入www.tarena.com/authdir/index.html,可以访问
2、在win7客户端测试(IP192.168.10.21)
1、可以访问www.tarena.com
2、可以访问www.tarena.com/authdir/index.html

试验四:HTTP的用户授权

客户端访问http://www.tarena.com/authdir需要输入用户名密码验证

1、修改主配置文件
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf //编辑主配置文件
...
337 <Directory "/var/www/html/authdir"> //目录
338 Order allow,deny //控制顺序
339 Allow from all //允许所有用户
340 AuthName "Please Input Password!!" //认证领域,弹窗提示
341 AuthType Basic //认证类型
342 AuthUserFile "/etc/httpd/.vuser" //用户数据文件路径
343 Require valid-user //指定授权用户
344 </Directory>
...
2、创建账户密码
[root@localhost ~]# htpasswd -c /etc/httpd/.vuser admin //创建访问用户
New password: //设置密码
Re-type new password: //验证密码
Adding password for user admin
3、启动服务测试
[root@localhost ~]# service httpd restart //重启服务 http://www.tarena.com/authdir 客户及测试:
两台不同客户机访问www.tarena.com/authdir 提示输入用户名密码,输入用户名密码正常访问,说明设置成功

实验五:HTTP目录别名

客户端访问http://www.tarena.com/sina时可以防问/var/www/html/sina.com/bbs下的网页
1、创建测试站点
[root@localhost ~]# mkdir -p /var/www/html/sina.com/bbs //创建站点
[root@localhost ~]# vim /var/www/html/sina.com/bbs/index.html//编辑网页
<html>
<head><title>This is a test Page!!!</title></head>
<body>
<h1>This is bbs.sina.com test Page!!!</h1>
</body>
</html>
2、修改主配置文件
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf //修改主配置文件
Alias /sina "/var/www/html/sina.com/bbs" //全局配置添加别名语句
3、启动服务测试
[root@ser1 ~]# service httpd restart //重启服务
win7客户机测试访问http://www.tarena.com/sina显示This is bbs.sina.com test Page!!! 设置成功
实验六:
查看默认HTTP使用进程管理方式
更改默认进程管理方式为worker模式
[root@localhost ~]# httpd -l //查看默认进程管理方式
Compiled in modules:
core.c
prefork.c //默认为prefork
http_core.c
mod_so.c
[root@localhost ~]# cd /usr/sbin/ //进入sbin目录下
[root@localhost sbin]# mv httpd httpd.prefork //更改http名称
[root@localhost sbin]# mv httpd.worker httpd //将httpd.worker改名为httpd
[root@localhost sbin]# service httpd restart //重启http服务
[root@localhost sbin]# httpd -l //查看进程管理方式
Compiled in modules:
core.c
worker.c //为worker模式 设置成功
http_core.c
mod_so.c

试验七:
部署Awstats统计Http访问日志
1、安装软件(软件已经拷贝到/usr/src下)
[root@localhost ~]# cd /usr/src/
[root@localhost src]# tar -zxvf awstats-7.1.tar.gz -C /usr/local/ //解压文件
[root@localhost src]# cd /usr/local/
[root@localhost local]# mv awstats-7.1/ awstats //将文件夹移动到local下并改名
[root@localhost local]# cd awstats/tools/
[root@localhost tools]# ./awstats_configure.pl //运行文件
...
Config file path ('none' to skip web server setup):
> /etc/httpd/conf/httpd.conf //输入apache的主配置文件
...
-----> Need to create a new config file ?
Do you want me to build a new AWStats config/profile
file (required if first install) [y/N] ? y //生成awstats的配置文件
...
Your web site, virtual server or profile name:
> www.tarena.com //输入你的web服务器名字
...
Default: /etc/awstats
Directory path to store config file(s) (Enter for default):
>
...
/usr/local/awstats/tools/awstats_updateall.pl now
Press ENTER to continue...
...
Press ENTER to finish...
2、修改主配置文件
[root@localhost tools]#vim/etc/awstats/awstats.www.tarena.com.conf //编辑awstats主配置文件
...
51 LogFile="/var/log/httpd/access_log" //修改日志文件路径
[root@localhost tools]# mkdir /var/lib/awstats //创建文件夹
3、将日志文件导入Awstats
[root@localhost tools]# ./awstats_updateall.pl now //运行文件导入日志
[root@localhost tools]# crontab -l //查看周期任务
*/5 * * * * /usr/local/awstats/tools/awstats_updateall.pl now//每5分钟运行一次
[root@localhost tools]# service crond restart //重启周期计划任务服务
[root@localhost tools]# chkconfig crond on
4、验证:
访问下列网址: http://www.tarena.com/awstats/awstats.pl?config=www.tarena.com可看到网站统计信息
补充:
通过html代码实现网页跳转功能
[root@localhost tools]# vim /var/www/html/awstats.html //编辑awstats.html
<html>
<head><meta http-equiv=refresh content="0; url=http://www.tarena.com/awstats/awstats.pl? config=www.tarena.com">
</head>
<body>
</body>
</html>
验证:
浏览器访问http://www.tarena.com/awstats.html 显示统计信息为设置成功

本文出自 “前行的笔记” 博客,请务必保留此出处http://mmdqx.blog.51cto.com/9041985/1427092
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: