Apache LINUX中搭建HTTP服务器
LINUX中搭建HTTP服务器在LINUX系统中安装Apache做为HTTP的服务程序,通过设置配置文件,可以实现很多功能,如访问控制,虚似主机,基于域名的虚拟主机,通过安装Awstats我们可以更方便的统计Http访问日志
实验拓扑:
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
# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
HWADDR=00:0c:29:5d:a8:80
IPADDR=192.168.10.253
NETMASK=255.255.255.0
2、配置主机名
# cat /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=yes
HOSTNAME=web01.tarena.com
3、修改hosts文件
# cat /etc/hosts
127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
192.168.10.253web01.tarena.com web01
# service network restart
# chkconfig network on
4、软件包的安装
# rpm -q httpd
package httpd is not installed
# yum -y install httpd
5、启动服务
# service httpd restart
# chkconfig httpd on
试验二:基本HTTP服务器的配置
Web服务器域名:www.tarena.com
默认首页包括:index.html、index.php
开启保持连接
确认默认httpd是否支持php
网站用老师提供的test_web.zip测试
服务器操作:
1、备份主配置文件
# cd /etc/httpd/conf
# cp httpd.conf httpd.conf.bak
2、修改主配置文件
# vim /etc/httpd/conf/httpd.conf
...
74 KeepAlive On
...
265 ServerName www.tarena.com:80
...
391 DirectoryIndex index.html index.php
...
3、启动服务
# service httpd restart
# chkconfig httpd on
# cd /root/Desktop/
# unzip test_web.zip
# mv jiajutiyan/* /var/www/html/
4、编写测试php页面
# cat /var/www/html/test.php
测试:
1、在客户端hosts文件指定
192.168.10.253 www.tarena.comwww
2、打开浏览器
http://www.tarena.com
http://www.tarena.com/test.php
.
3、新建authdir站点,只允许192.168.1.251访问www.tarena.com,允许所有人访问www.tarena.com/authdir
# mkdir /var/www/html/authdir
# echo "http://www.tarena.com/authdir/index.html" >/var/www/html/authdir/index.html
# vim /etc/httpd/conf/httpd.conf
...
337
338 Order allow,deny
339 Allow from all
340
# service httpd restart
在不同客户端测试
# tail /var/log/httpd/error_log
试验四:HTTP的用户授权
客户端访问http://www.tarena.com/authdir需要输入用户名密码验证
1、修改主配置文件
# vim /etc/httpd/conf/httpd.conf
...
337
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
...
2、创建账户密码
# htpasswd -c /etc/httpd/.vuser admin
New password:
Re-type new password:
Adding password for user admin
3、启动服务测试
# service httpd restart
http://www.tarena.com/authdir
实验五:HTTP目录别名
客户端访问http://www.tarena.com/sina时可以访问/var/www/html/sina.com/bbs下的网页
1、创建测试站点
# mkdir -p /var/www/html/sina.com/bbs
# cat /var/www/html/sina.com/bbs/index.html
This is a test Page!!!
This is bbs.sina.com test Page!!!
2、修改主配置文件
# tail -n 1 /etc/httpd/conf/httpd.conf
Alias /sina "/var/www/html/sina.com/bbs"
3、启动服务测试
# service httpd restart
http://www.tarena.com/sina
实验六:
查看默认HTTP使用进程管理方式
更改默认进程管理方式为worker模式
# httpd -l
Compiled in modules:
core.c
prefork.c
http_core.c
mod_so.c
# cd /usr/sbin/
# mv httpd httpd.prefork
# mv httpd.worker httpd
# service httpd restart
# httpd -l
Compiled in modules:
core.c
worker.c
http_core.c
mod_so.c
试验七:
部署Awstats统计Http访问日志
1、安装软件(软件在/usr/src下)
# cd /usr/src/
# tar -zxvf awstats-7.1.tar.gz -C /usr/local/
# cd /usr/local/
# mv awstats-7.1/ awstats
# cd awstats/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 //生成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、修改主配置文件
# vim /etc/awstats/awstats.www.tarena.com.conf
...
51 LogFile="/var/log/httpd/access_log"
# mkdir /var/lib/awstats
3、将日志文件导入Awstats
# ./awstats_updateall.pl now
# crontab -l
*/5 * * * * /usr/local/awstats/tools/awstats_updateall.pl now
# service crond restart
# chkconfig crond on
4、验证:
http://www.tarena.com/awstats/awstats.pl?config=www.tarena.com
补充:
通过html代码实现网页跳转功能
# cat /var/www/html/awstats.html
验证:
http://www.tarena.com/awstats.html
实验八:基于域名的虚拟主机
www.sina.com 192.168.10.253 sina网站
www.sohu.com 192.168.10.253 sohu网站
1、修改主配置文件
新建一个配置文件(虚似主机配置专用)
# vim /etc/httpd/conf.d/virt.conf
NameVirtualHost *:80
DocumentRoot /var/www/sina
ServerName www.sina.com
ErrorLog logs/www.sina.com-error_log
CustomLog logs/www.sina.com-access_log common
DocumentRoot /var/www/sohu
ServerName www.sohu.com
ErrorLog logs/www.sohu.com-error_log
CustomLog logs/www.sohu.com-access_log common
创建网站目录和文件
# mkdir /var/www/{sina,sohu}
# cat /var/www/sina/index.html
www.sina.com
# cat /var/www/sohu/index.html
www.sohu.com
# servicehttpd restart
验证:
先在客户端修改hosts文件
192.168.10.253 www.sina.com
192.168.10.253 www.sohu.com
实验九:基于端口的虚拟主机
192.168.10.253:8081 sina网站
192.168.10.253:8082 sohu网站
2、修改主配置文件
新建一个配置文件(虚似主机配置专用)
# vim /etc/httpd/conf.d/virt.conf
Listen 8081 //激活端口
Listen 8082
DocumentRoot /var/www/sina
ServerName 192.168.10.253
ErrorLog logs/www.sina.com-error_log
CustomLog logs/www.sina.com-access_log common
DocumentRoot /var/www/sohu
ServerName 192.168.10.253
ErrorLog logs/www.sohu.com-error_log
CustomLog logs/www.sohu.com-access_log common
2、启动服务
# servicehttpd restart
实验十:配置HTTP支持php
1、安装php
# yum -y install php
# cat /var/www/sina/test.php
# servicehttpd restart
测试:
http://192.168.10.253:8081/test.php
页:
[1]