<--目录-->

1)Apache主配置文件解析

2)基于rpmp安装httpd的配置



【Apache主配置文件解析】

httpd: Apache HTTP Server 

RHEL5的httpd服务


httpd基础网站服务(Apache是一个软件,提供httpd服务)

网站写完之后,需要有一个平台,将网站的东西放到服务器上面,这个服务器启动

服务,客户端通过浏览器,才能看到这样一个网站,我搭好平台,把网站给我,网

络没有问题,外面的用户就可以访问


技能要点

httpd基础网站服务

httpd访问控制

AWStats日志分析

虚拟Web主机


Apache项目简介

主要忧点

- 开放源代码、跨平台运用

- 支持多种网页编程语言

- 模块化设计、运行稳定、良好的安全性


主要软件包

- htttpd : Apache HTTP Server的服务程序包

- httpd-manual : 网页版手册文档

- httpd-devel : 开发工具包

- system-config-httpd : 图形化配置工具


#yum -y install httpd-manual httpd

.. ..

lnstalled:

 httpd-manualx86_64 0:2.2.3-74.e15

 httpd.x86_64 0:2.2.3-74.e15


主要目录和文件

- 服务目录 :/etc/httpd/

- 配置目录 :/etc/httpd/conf.d/

- 主配置文件 :/etc/httpd/conf/httpd.conf

- 网站根目录 : /var/www/html

- 模块目录 : /etc/httpd/modules/

- 访问日志 :/var/log/httpd/access_log

- 错误日志 : /var/log/httpd/error_log


服务与程序相关

- 系统服务脚本 : /etc/init.d/httpd

- 默认端口 : TCP 80

- 进程名 : httpd

- 进程所有者 : apache


httpd服务基本配置

基本部署思路

- 1.确定网站名称、IP地址

- 2.配置httpd,启动服务程序

- 3.部署网页文档

- 4.在客户机中访问Web站点

- 5.查看Web访问情况


部署网页文档

默认位置:/var/www/html

- 缺少首页文件时,会出现403禁止访问错误

- RHEL以欢迎面替代了403页面

#grep 403 /etc/httpd/conf.d/welcome.conf

 ErrorDocument 403 /error/noindex.html


#file /var/www/error/noindex.html

/var/www/error/noindex.html:HTML document text


访问默认的Web站点

直接启动httpd服务,即可访问默认站点

- http://服务器地址/


查阅httpd-manual提供的手册页

- httpd://服务器地址/manual/


查看Web访问情况

观察日志 access_log

#tail -2 /var/log/httpd/aceess_log


httpd.conf配置解析

- 全局设置:配置参数 值 

- 目录设置:

  <Directory 目录>.. ..</Directory>  (把写的东西放在中间,"/"表示结束)

- 访问位置设置:

  <Location URL>.. ..</Location> (把写的东西放在中间,"/"表示结束)

- 虚拟主机设置:

 <VirtualHost监听地址>.. ..</VirtualHost> (把写的东西放在中间,"/"表示结束)


httpd.conf配置解析

常用的全局设置参数

- ServerName:本站点的FQDN名称

- DocumentRoot:网页文档的根目录

- DirectoryIndex:默认索引页/首页文件

- ErrorLog:错误日志文件的位置

- CustomLog:访问日志文件的位置


- listen:监听服务的IP地址、端口号

- PidFile:保存httpd进程PID号的文件

- ServerRoot:服务目录

- ServerAdmin:管理员邮箱

- User:服务进程的属主,默认为apache

- Group:服务进程的属组,默认为apache


- Timeout:网络连接超时

- keepAlive:是否保持连接,可选On或off (建议打开,默认off)

- MaxkeepAliveRequests:每次连接最多处理的请求数

- keepAliveTimeout:保持连接的超时时限

- Include:可包含其他子配置文件



实验二

httpd访问控制

主要作用

- 控制对网站资源的访问

- 为特定的网站目录添加访问授权

常用的访问控制方式

- 客户机地址限制

- 用户授权


客户机地址限制

Order 配置项,定义控制顺序

- allow,deny 先允许后拒绝,默认拒绝所有

- deny,allow 先拒绝后允许,默认允许所有

Allow/Deny from 配置项,设置权限

- Deny from 地址1 地址2 .. ..

- Allow from 地址1 地址2 .. ..


例:

只允许192.168.4.0网段访问

<Directory /var/www/html>

Order allow,deny

Allow from 192.168.4.0/24

</Directory>



实验三

用户授权

为目录区段设置授权

- AuthName:认证领域名称,用于弹窗提示

- AuthType: 认证类型,一般使用basic

- AuthUserFile: 用户数据文件的路径

- Require:指定授权用户或组


Require valid-user   #合法用户都可以访问

或者, Require user 用户1 用户2 .. ..

或者, Require group 组1 组2 .. ..


http基本认证的用户数据文件

- htpasswd [-c] 账号文件 用户名

- 有选项 -c 表示新建,没有-c表示对该用户修改密码

htpasswd -c /etc/httpd/.vuser admin

New passwd: //设置密码

Re-type new passwd: //重复设置密码



实验四

目录别名

以网站根为参照的虚拟目录 

 - 实际对应的是根目录以外的其他文件夹

#grep Alias /etc/httpd/conf/httpd.conf

.. ..

Alias /icons/ "/var/www/icons"

ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

Alias /error/ "/var/www/error/"


当从浏览器访问 http://服务器地址/error/时

实际提供的是服务器本地/var/www/error/目录里的内容

而不是网站根目录 /var/www/html/下error/子目录里的内容



实验五

AWStats网站日志分析

1、PV(访问量)

即Page View,页面浏览量或点击量,用户每次刷新即被计算一次

   UV(独立访客)

即Unique Visitor,访问您网站的一台电脑客户端为一个访客

00:00-24:00内相同的客户端只被计算一次

   IP(独立IP):

即Internet Protocol,指独立IP数.00:00-24:00内相同IP地址被计算一次


AWStats系统

- 使用perl语言开发的日志分析工具

- 可统计页面访问量、访问者地区、文件点击数排名、搜索关健词、浏览器类型、所用系统...

- 下载站点:http:/awstats.sourceforge.net/


AWStats安装及使用

- 1、安装awstats软件包

- 2、为要统计的站点建立配置文件

- 3、修改站点统计配置文件

- 4、执行日志分析,并设置cron计划任务

- 5、查看分析结果



实验六

HTTP网站虚拟主机

1、虚拟主机概念

在同一台服务器上允许多个Web站点

每个站点并不是独立占用一台真正的计算机

2、虚拟主机的类型/区分方式

- 基于域名(FQDN)

- 基于IP地址

- 基于端口


基于域名的虚拟主机

案例环境

- 站点1:dns.wsyht.com 192.168.2.1

- 站点2:www.wsyht.com 192.168.2.1

区分访问效果

- http://dns1.wsyht.com 显示dns1

- http://www.wsyht.com 显示www



基于IP地址的虚拟主机

案例环境

- 站点1:dns.wsyht.com 192.168.2.2

- 站点2:www.syht.com 192.168.2.3

区分访问效果

- http://192.168.2.1 显示dns

- http://192.168.2.3 显示www


与域名方式相似,区别在httpd配置的差异

vi /etc/httpd/conf.d/virt.conf

.. .. //不需要NameVirtualHost

<VirtulHost 192.168.2.1> //虚拟站点1

.. ..

</Virtual>Host

<VirtualHost 192.168.2.2> //虚拟站点2

.. ..

</VirtualHost>



基于端口的虚拟主机

案例环境

- 站点1:dns.wsyht.com 192.168.2.1

- 站点2:dns.wsyht.com 192.168.2.2

区分访问效果

- httpd://dns.wsyht.com   显示dns

- httpd://www.wsyht.com81 显示www


与域名方式相似,区别在httpd配置差异

#vi /etc/httpd/conf.d/virt.conf

Listen 80 //监听必要的端口

Listen 81

.. .. //不需要NameVirtualHost

<VirtualHost 192.168.2.1:80> //虚拟站点1

.. ..

</VirtualHost>

<VirtualHost 192.168.2.1:81> //虚拟站点2

.. ..

</VirtualHost>



【基于rpm安装httpd的配置】

实验拓扑:

                                      Linux Client 192.168.2.2

--------RHEL5.9(vmnet1)--------(vmnet1)

       192.168.2.1                    Win7 Client  192.168.2.100



试验一:基本的HTTP服务器的配置

主机名设为:www.wsyht.com 192.168.2.1

默认首页包括:index.html、index.php

确认默认httpd是否支持php


一、前提条件在客户端配置host文件(host与DNS任选一个即可)

1)host文件(192.168.2.2)

# cat /etc/hosts

192.168.2.1 www.wsyht.com www

2)构建DNS(wsyht.com)服务器

1、安装软件包

#yum -y install bind bind-chroot caching-nameserver

2、修改主配置文件

#cd /var/named/chroot/etc/

#cp -p named.caching-nameserver.conf named.conf

#vim named.conf

...

 15 listen-on port 53 {192.168.2.1;};

...

 27 allow-query {any;};

 28 allow-query-cache {any;};

...

 37 match-clients {any;};

 38 match-destinations{any;};

#vi named.rfc1912.zones

...

 51 tye master;

 52 type master;

 53 file "wsyht.com.zone";

 54};

#named-checkconf named.conf

3、修改数据库文件

# cd /var/named/chroot/var/named/

#cp -p named.local wsyht.com.zone

#cat wsyht.com.zone

...

IN NS dns1.wsyht.com

dns1 IN A 192.168.2.1

www IN A 192.168.2.1

#named-checkconf wsyht.com wsyht.com.zone

4、启动服务

#service named restart

#chkconfig named on


二、搭建HTTPD

1、安装软件包

#yum -y install httpd

2、修改主配置文件

#cat /etc/httpd/conf/httpd.conf

...

 74 KeepAlive On

...

 265 ServerName www.wsyht.com:80

...

 391 DirectoryIndex index.html index.php

...


3、启动服务

#service httpd restart

#chkconfig httpd on 

新建测试页面

#vi /var/www/html/index.html

<html>

<head><title>This is a test Paqe!!!</title>

<body>

<h1>This is www.wsyht.com test Page!!!</h1>

</body>

</head>

</html>

:wq

#cat /var/www/html/index.php

<?php

phpinfo();

?>


4、测试

http://www.wsyht.com

http://www.wsyht.com/index.php



HTTP的访问控制

实验二:目录访问的客户机地址限制

只允许192.168.2.2访问www.wsyht.com

允许所有用户访问www.wsyht.com/authdir/index.html


1、修改主配置文件

#mkdir /var/www/html/authdir

#cat /var/www/html/authidr/index.html

http://www.wsyht.com/authdir/index.html

#vi /etc/httpd/conf/httpd.conf

...

 307 <Directory "/var/www/html">

...

 333 Order allow,deny

 334 # Allow from all

 335 Allow from 192.168.2.2

...

 337 <Directory "/var/www/html/authdir">

 338 Order deny,allow

 339 </Directory>

2、重启服务

#service httpd restart

3、在不同客户端测试

#tail /var/log/httpd/error_log //查看错误日志



实验三:为目录访问添加用户授权

客户端访问/var/www/html/authdir/需要输入用户名密码验证


1、修改主配置文件

...

 337 <Directory "/var/www/html/authdir">

 338 Order deny,allow

 339 AuthName "Please Input Password!!"

 340 AuthType Basic

 341 AuthUserFile "/etc/httpd/.vuser"

 342 Require valid-user

 343 </Directory>

...

2、创建账户密码

#htpasswd -c /etc/httpd/.vuser admin

New password:

Re-type new password:

Adding password for user admin

3、重启服务

#service httpd restart

4、客户端访问测试

http;//www.wsyht.com/authdir



实验四:HTTP目录别名设置

客户端访问http://www.wsyht.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 bbs.sina.com test Page!!!

2、修改主配置文件

#tail -1 /etc/httpd/conf/httpd.conf

  Alias /sina "/var/www/html/sina.com/bbs"

3、重启服务

#service httpd restrt

4、客户端测试

http://www.wsyht.com/sina



实验五:

部署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/ awastats

2、为站点建立配置文件(支持多个站点,每个站点独立配置)

#cd awstats/tools/

#./awstats_configure.pl

>/etc/httpd/conf/httpd.conf    //指定Web配置路径

?y //同意创建新的站点配置

>www.wsyht.com //指定网站名称

......自动重启httpd服务

> //回车,继续

> //回车,完成配置

3、修改主配置文件(指定正确的目标日志文件)

#vi /etc/awstats/awstats.www.wsyht.com.conf

LogFile="/var/log/httpd/access_log"    //要分析日志文件

DirData="/var/lib/awstats"           //默认工作目录

.. ..

根据默认的设置创建对应的工作目录 

#mkdir /var/lib/awstats

4、将日志文件导入AWStats,手动执行,更新所有站点

#./awstats_updateall.pl now

5、添加计划任务

- 每5分钟全部更新一次

#crontab -e 

*/5 * * * *  /usr/local/awstats/tools/awstats_updateall.pl now

6、重启服务

#service crond restart

#chkconfig crond on

7、验证

http://www.wsyht.com/awstats/awstats.pl?config=www.wsyht.com

8、简化访问地址,自动跳转到结果页面,通过html代码实现网页跳转功能

#cat /var/www/html/awstats.html

<html>

<head><meta http-equiv=refresh content="0;

url=http://www.wsyht.com/awstats/awstats.pl?

config=www.wsyht.com">

</head>

<body></body>

</html>

8、再次验证

http://www.wsyht.com/awstats.html


实验六:

查看默认HTTP使用的进程管理方式

更改默认进程管理方式为worker模式

[root@localhost sbin]# httpd -l

Compiled in modules:

  core.c

  prefork.c                //用的是prefork模式

  http_core.c

  mod_so.c


#cd /usr/sbin/             //进入相应文件夹更改进程管理方式

#mv httpd httpd.prefork

#mv httpd.worker httpd

#service httpd restart


再次验证查看

[root@localhost sbin]# httpd -l

Compiled in modules:

  core.c

  worker.c   //用的是worker模式

  http_core.c

  mod_so.c



实验七:基于域名的虚拟主机

 当用户访问www.baidu.com的时候访问baidu网站

 当用户访问www.google.com的时候访问goole网站

 baidu和google的ip地址一样

 确保上面www.wsyht.com访问还能访问

前提条件(客户端配置host)

windows:c:\windows\System32\drivers\etc\hosts

192.168.2.1 www.wsyht.com www

192.168.2.1 www.baidu.com www

192.168.2.1 www.google.com www

1、建立网站存放路径

#mkdir -p /data/web/{baidu,google}

#cat /data/web/baidu/index.html

www.baidu.com

#cat /data/web/google/index.html

www.google.com

2、修改主配置文件

#cat /etc/httpd/conf.d/virt.conf

NameVirtualHost *:80

<VirtualHost *:80>

    DocumentRoot /var/www/html

    ServerName www.wsyht.com

    ErrorLog logs/wsyht.com-error_log

    CustomLog logs/wsyht.com-access_log common

</VirtualHost>

<VirtualHost *:80>

    DocumentRoot /data/web/baidu

    ServerName www.baidu.com

    ErrorLog logs/baidu.com-error_log

    CustomLog logs/baidu.com-access_log common

</VirtualHost>

<VirtualHost *:80>

    DocumentRoot /data/web/google

    ServerName www.google.com

    ErrorLog logs/google.com-error_log

    CustomLog logs/google.com-access_log common

</VirtualHost>

3、重启服务

#service httpd restart

4、客户端测试

http://www.wsyht.com

http://www.baidu.com

http://www.google.com



实验八:基于IP的虚拟主机

 www.wsyht.com  192.168.2.1

 www.baidu.com  192.168.2.10

 www.google.com  192.168.2.20

前提条件,设置IP

#cd /etc/sysconfig/network-scripts/

#cp ifcfg-eth0 ifcfg-eth0:0

#cp ifcfg-eth0 ifcfg-eth0:1

# cat ifcfg-eth0:0

DEVICE=eth0:0

BOOTPROTO=on

IPADDR=192.168.2.10

NETMASK=255.255.255.0

ONBOOT=yes

#cat ifcfg-eth0:1

DEVICE=eth0:1

BOOTPROTO=on

IPADDR=192.168.2.20

NETMASK=255.255.255.0

ONBOOT=yes

重启网络

#service network restart

客户端修改host文件

192.168.2.1  www.wsyht.com www

192.168.2.10  www.baidu.com www

192.168.2.20  www.google.com www

1、建立网站存放路径

过程略...见基于域名的虚拟主机配置

2、修改主配置文件

#cat /etc/httpd/conf.d/virt.conf

<VirtualHost 192.168.2.1:80>

    DocumentRoot /var/www/html

    ServerName www.wsyht.com

    ErrorLog logs/wsyht.com-error_log

    CustomLog logs/wsyht.com-access_log common

</VirtualHost>

<VirtualHost 192.168.2.10:80>

    DocumentRoot /data/web/baidu

    ServerName www.baidu.com

    ErrorLog logs/baidu.com-error_log

    CustomLog logs/baidu.com-access_log common

</VirtualHost>

<VirtualHost 192.168.2.20:80>

    DocumentRoot /data/web/google

    ServerName www.google.com

    ErrorLog logs/google.com-error_log

    CustomLog logs/google.com-access_log common

</VirtualHost>

3、重启服务

#service httpd restart

4、客户端测试

192.168.2.1

192.168.2.10

192.168.2.20



实验九:基于端口的虚拟主机

 www.wsyht.com:80  wsyht.com的网站

 www.baidu.com:8080  baidu.com的网站

 www.google.com:8081  google.com的网站

前提条件客户端修改hosts文件

192.168.2.1  www.wsyht.com www

1、建立网站存放路径

过程略...见基于域名的虚拟主机配置

2、修改主配置文件

#cat /etc/httpd/conf.d/virt.conf

<VirtualHost *:80>

    DocumentRoot /var/www/html

    ServerName www.wsyht.com

    ErrorLog logs/wsyht.com-error_log

    CustomLog logs/wsyht.com-access_log common

</VirtualHost>

<VirtualHost *:8080>

    DocumentRoot /data/web/baidu

    ServerName www.baidu.com

    ErrorLog logs/baidu.com-error_log

    CustomLog logs/baidu.com-access_log common

</VirtualHost>

<VirtualHost *:8081>

    DocumentRoot /data/web/google

    ServerName www.google.com

    ErrorLog logs/google.com-error_log

    CustomLog logs/google.com-access_log common

</VirtualHost>

3、添加主配置文件的监听端口

#cat /etc/httpd/conf/httpd.conf

...

 Listen 80

 Listen 8080

 Listen 8081

..

4、重启服务

#service httpd restart

5、客户端测试

www.wsyht.com

www.wsyht.com8080

www.wsyht.com8081