一、httpd 主配置文档的介绍/etc/httpd/conf/httpd.conf

### Section 1: Global Environment 全局环境
ServerRoot "/etc/httpd"
主服务程序在这个目录下
PidFile run/httpd.pid
Pid 在主服务目录下的这个文件
Timeout 60
超时时间为60秒
KeepAlive Off
持久连接关闭
MaxKeepAliveRequests 100
最大连接数
KeepAliveTimeout 15
超时时间
prefork MPM   prefork  装载模块  二进制包默认安装启动这个模块
<IfModule prefork.c>
StartServers       8  这个模块启动时启动进程数
MinSpareServers    5  最小空闲进程
MaxSpareServers   20  最大空闲进程
ServerLimit      256   为MaxClients 生成多少个进程数
MaxClients       256  最大客户端并发数
MaxRequestsPerChild  4000 每个子进程在生命周期中处理多少个请求数
</IfModule>
worker MPM
<IfModule worker.c>
StartServers         4   启动时启动进程数
MaxClients         300    最大客户端并发数
MinSpareThreads     25    最小空闲线程数
MaxSpareThreads     75    最大空闲线程数
ThreadsPerChild     25     为MaxRequestsPerChild启动多少个线程数
MaxRequestsPerChild  0     最大客户端请求并发数,不做限定
</IfModule>

三种模式的优缺点

wps_clip_p_w_picpath-27623

参数是可以修改的,具体视具体情况而定

#Listen 12.34.56.78:80   监听  ip 加端口
Listen 80 监听端口
基于多端口的话在这里添加端口就行啦   不用加 ip 端口 默认监听本主机
我做实验的时候ip 加端口报错 以后看看
模块部分,安装的模块已经启用,支持动态装卸载,注释就关闭,不注释就开启
LoadModule auth_basic_module modules/mod_auth_basic.so   基本明文认证模块
LoadModule auth_digest_module modules/mod_auth_digest.so  密文认证模块
LoadModule authn_file_module modules/mod_authn_file.so    以什么文件保存密码
LoadModule authn_alias_module modules/mod_authn_alias.so  别名模块
安装的模块没有启用,要启用的话手动启用就可以啦
#LoadModule asis_module modules/mod_asis.so
#LoadModule authn_dbd_module modules/mod_authn_dbd.so
#LoadModule cern_meta_module modules/mod_cern_meta.so
#LoadModule cgid_module modules/mod_cgid.so

用命令   httpd  -D  DUMP-MODULES 可以查看启用模块信息

以那个普通用户和组来运行此进程 这里以 apache 用户来运行为了安全考虑

User apache

Group apache

第二段   主要配置,默认是执行这一段配置代码

### Section 2: 'Main' server configuration    主要配置
ServerAdmin   www@dingchao.com   邮箱地址,当网页出问题时会显示邮箱地址,和
apache的版本信息
#ServerName www.example.com:80  主机名,默认注释,默认他会获取你的主机名
Httpd  启动时报错说主机名不确定  就要改这里  或者改 /etc/hosts 和 /etc/sysconfig/network
DocumentRoot "/www/html"  指定根目录的网页文件放在哪里
站点路径访问控制  对根目录的访问控制
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
Options   -Indexes    -FollowSymLinks
Indexes : 当访问的路径下无默认的主页面时,以列表的形式显示所有资源,很危险,一定要
注释掉,做下载站点时比较有用
-FollowSymLinks
允许跟踪符号链接文件相同的资源
AllowOverride None  不允许任何人覆盖
支持在每个页面目录下创建.htaccess用于实现对此目录中资源访问时的访问控制功能。
DirectoryIndex index.html index.html.var
定义默认主页面 写在前面的优先级高
定义访问控制的
Order allow,deny
Allow from all
允许所有访问
例如
Order allow,deny
Deny from 172.16.100.17 拒绝这个ip地址访问
Allow from 172.16.0.0\16 允许这个网段的访问

wps_clip_p_w_picpath-10483

定义错误日志 默认存储在 /var/httpd/log/目录下

ErrorLog logs/error_log
#
# LogLevel: Control the number of messages logged to the error_log.
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
#
Debug 最详细信息
Info  信息
Notice 引起注意
Warn 警告
Error 错误
Crit 蓝色警报
Alert 橙色警报
Emerg 红色级别
指定日志级别
LogLevel warn
错误的日志都会被记录
#
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
#
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\"  \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
%h 客户端主机
%l  客户端ip
%u  用户名
%t  登录时间
"%r\" %>s %b \"%{Referer}i\"  从哪个连接跳转进来的
\"%{User-Agent}i\"" 浏览器是什么

Combined  日志格式

自定义日志

LogFormat "%h %l %u %t \"%r\" %>s %b" common
CustomLog logs/access_log  common
定义别名跳转连接
Alias /icons/ "/var/www/icons/"
<Directory "/var/www/icons">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

站点根目录:   

DocumentRoot var/www/html
http://www.magedu.com/p_w_picpaths/logo/new.gif
此文件位置:/www/html/p_w_picpaths/logo/new.gif
实现URL路径的映射,从而所访问的资源不再依赖于站点根目录;
Alias /URL/ "/path/to/somewhere/"
CGI 脚本定义  动态页面
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"   路径跳转
#
# "/var/www/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#
<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None   访问控制设置
    Order allow,deny
    Allow from all
   </Directory>
字符集图片设置
IndexOptions FancyIndexing VersionSort Name HTMLTable Charset=UTF-8
#
# AddIcon* directives tell the server which icon to show for different
# files or filename extensions.  These are only displayed for
# FancyIndexed directories.
#
AddIconByEncoding (CMP,/icons/compressed.gif) x-compress x-gzip
AddIconByType (TXT,/icons/text.gif) text/*
AddIconByType (IMG,/icons/p_w_picpath2.gif) p_w_picpath/*
AddIconByType (SND,/icons/sound2.gif) audio/*
AddIconByType (VID,/icons/movie.gif) video/*
AddIcon /icons/binary.gif .bin .exe
AddIcon /icons/binhex.gif .hqx

第三段 虚拟主机配置

### Section 3: Virtual Hosts
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.2/vhosts/>  请读帮助手册,这里下载
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# Use name-based virtual hosting.  监听虚拟主机 这里默认不用更改
#
#NameVirtualHost *:80
#
# NOTE: NameVirtualHost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.
#
#
# VirtualHost example:   给的一个配置示例文档
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
#<VirtualHost *:80>
#    ServerAdmin webmaster@dummy-host.example.com
#    DocumentRoot /www/docs/dummy-host.example.com
#    ServerName dummy-host.example.com
#    ErrorLog logs/dummy-host.example.com-error_log
#    CustomLog logs/dummy-host.example.com-access_log common
#</VirtualHost>   实例 配置
<VirtualHost 192.168.1.143:80> 
     ServerName www.dingchao.com
     DocumentRoot "/www/html1"
     ErrorLog logs/dummy-host.example.com-error_log
     CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>

四、实验

基于虚拟主机建设站点

1、安装 httpd 服务

Rpm包安装

基础开发组包安装

Desktop Platform Development
Development tools
Server Platform Development

Httpd包相关的软件

httpd-2.2.15-29.el6.centos.x86_64
apr-1.3.9-5.el6_2.x86_64
apr-util-1.3.9-3.el6_0.1.x86_64

2、IP 设置

wps_clip_p_w_picpath-31516

3、主配置文档的修改 /etc/httpd/conf/httpd.conf

网页放置位置

wps_clip_p_w_picpath-24423

网页安全考虑 注释掉

wps_clip_p_w_picpath-32292

监听端口

wps_clip_p_w_picpath-27461

4、虚拟主机配置   基于端口  基于IP

wps_clip_p_w_picpath-840

5、创建目录建设网页

wps_clip_p_w_picpath-10936

6、实验结果

这里可以安装一个文本浏览器可以直接在本机linux查看结果

elinks-0.12-0.21.pre5.el6_3.x86_64

默认80端口

wps_clip_p_w_picpath-31983

改为8080端口

wps_clip_p_w_picpath-10219

基于多 IP 主要就是加网卡

wps_clip_p_w_picpath-4227

访问控制实验

基础介绍

(1) 基于用户进行认证

<Directory "/var/www/html/admin">
Options none
AllowOverride AuthConfig
AuthType Basic
AuthName "Admin Area."
#AuthBasicProvider file
AuthUserFile /etc/httpd/conf/.htpasswd
Require valid-user
</Directory>
Require valid-user: 文件中所有用户均可访问
Require user USERNAME, ...

(2) 提供认证文件

htpasswd

-c: 如果此文件事先不存在,则创建;注意,只能在创建第一个用户时使用;

-m:以md5的格式编码存储用户的密码信息

-D:删除指定用户

(3) 组认证

<Directory "/var/www/html/admin">
     Options none
     AllowOverride AuthConfig
     AuthType Basic
     AuthName "Admin Area."
     #AuthBasicProvider file
     AuthUserFile /etc/httpd/conf/.htpasswd
     AuthGroupFile /etc/httpd/conf/.htgroup
     Require group GROUP_NAME
</Directory>

组文件:

组名:user1 user2 user3

1、在已有的基础上实验

2、创建网页  本实验不用在虚拟主机哪里配置,如有配置的话最好注释掉以免影响实验结果

wps_clip_p_w_picpath-28354

3、配置访问控制

wps_clip_p_w_picpath-19936

4、新建账户

wps_clip_p_w_picpath-15658

5、重启查看实验结果

wps_clip_p_w_picpath-6858

登录网站查看

wps_clip_p_w_picpath-19230

Sethandler 可以做一个监控本机的状态信息

status页面

httpd内嵌有handler,其中有一个handler用于输出当前httpd服务相关状态信息

handler: server-status

启用handler要使用SetHandler指令

handler: 当文件被调用时,apache内部表示形式;一般每种文件类型都有其隐式处理器

1、在 server-status处 做一个访问控制策略

wps_clip_p_w_picpath-15225

2、实验结果

wps_clip_p_w_picpath-3983