httpd.conf是Apache网络服务器软件中重要的一个配置文件,它存储着Apache中许多必不可少的配置信息。最常用的一点,就是我们向里面添加建站网站信息。

http方法:

GET:请求获取一个资源,需要服务器发送

HEAD:跟GET近似,但其不需要服务响应请求的资源,而返回响应首部

POST:基于HTML表单向服务器提交数据,服务器通常需要存储此数据;(位置:通常为关系型数据库)

PUT:与GET相反,向服务器发送资源;服务器通常需要存储此资源;(位置:通常为文件系统)

DELETE:删除URL指向的资源

OPTIONS:探测服务器端对请求的URL所支持使用的请求方法

TRACE:跟一次请求中间所经过的代理服务器、防火墙或网关等


http状态码:

1XX:信息性状态码

2XX:成功状态码

200:OK

201:CREATED

3XX: 重定向类的状态码

301: Moved Permanently, 永久重定向

302: Found, 临时重定向,会在响应报文中使用“Location: 新位置”;

304: Not Modified

4XX:客户端类错误

403:Forbidden

404: Not Found

405: Method Not Allowed

5XX:服务器类的错误

500:Internal Server Error, 服务器内部错误

502:Bad Gateway, 代理服务器从上游服务器收到一条伪响应;

503:Service Unavailable, 服务暂时不可用

<method>:请求方法

<request-URL>: 请求的资源,可以是相对路径,如/p_w_picpaths/log.jpg,也可以绝对路径,如http://www.magedu.com/p_w_picpaths.banner.jpg

<version>: http协议版本,格式HTTP/<major>.<minor>,例如HTTP/1.0, HTTP/1.1

<headers>:各种所可以使用的首部

<status>: 状态码

<reason-phrase>: 原因短语,指状态码的易读信息


注意:http协议是无状态,stateless

cookie:

Set-Cookie

Set-Cookie2

Cookie:



 

前两天学习了WEB服务感觉挺简单的,然而一直对于其主配置文件的各个部分的含义并不是很清楚,将其整理一下,和大家分享。

首先配置文件分为三部分使用命令:grep "/<Section/>" /etc/httpd/conf/httpd.conf -n

得到:

33:### Section 1: Global Environment

234:### Section 2: 'Main' server configuration

955:### Section 3: Virtual Hosts

其中三部分意义分别是全局配置文件,主配置文件,虚拟主机。其中需要说明一下主配置文件的选项在虚拟主机中都可以使用

ServerRoot "/etc/httpd"

用于指定Apache的运行目录,服务启动之后自动将目录改变为当前目录,在后面使用到的所有相对路径都是想对这个目录下

PidFile run/httpd.pid

记录httpd守护进程的pid号码,这是系统识别一个进程的方法,系统中httpd进程可以有多个,但这个PID对应的进程是其他的父进程

Timeout 120

服务器与客户端与服务器断开的时间

KeepAlive Off

是否持续连接(因为每次连接都得三次握手,如果是访问量不大,建议打开此项,如果网站访问量比较大关闭此项比较好)

MaxKeepAliveRequests 100

表示一个连接的最大请求数

KeepAliveTimeout 15

断开连接前的时间

<IfModule prefork.c>

StartServers 8

MinSpareServers 5

maxSpareServers 20

ServerLimit 256

MaxClients 256

MaxRequestsPerChild 4000

</IfModule>

以上这几行是系统默认的模块儿,表示为每个访问启动一个进程(即当有多个连接公用一个进程的时候,在同一时刻只能有一个获得服务)。

StartServer开始服务时启动8个进程,最小空闲5个进程,最多空闲20个进程。

MaxClient限制同一时刻客户端的最大连接请求数量超过的要进入等候队列。

MaxRequestsPerChild每个进程生存期内允许服务的最大请求数量,0表示永不结束

<IfModule worker.c>

ServerLimit 10

ThreadLimit 64

StartServers 2

MaxClients 250

MinSpareThreads 25

MaxSpareThreads 75

ThreadsPerChild 25

MaxRequestsPerChild 0

</IfModule>

以上几行是为Apache配置线程访问,即每对WEB服务访问启动一个线程,这样对内存占用率比较小。

ServerLimit服务器允许配置进程数的上限。

ThreadLimit每个子进程可能配置的线程上限

StartServers启动两个httpd进程,

MaxClients同时最多能发起250个访问,超过的要进入队列等待,其大小有ServerLimit和ThreadsPerChild的乘积决定

ThreadsPerChild每个子进程生存期间常驻执行线程数,子线程建立之后将不再增加

MaxRequestsPerChild每个进程启动的最大线程数,如达到限制数时进程将结束,如置为0则子线程永不结束

Listen 80

监听的端口,如有多块网卡,默认监听所有网卡

LoadModule .......

启动时加载的模块儿

Include conf.d/*.conf

加载的配置文件

User apache

Group apache

启动服务后转换的身份,在启动服务时通常以root身份,然后转换身份,这样增加系统安全

ServerName localhost

默认是不需要指定的,服务器通过名字解析过程来获得自己的名字,但如果解析有问题(如反向解析不正确),或者没有DNS名字,也可以在这里指定IP地址,当这项不正确的时候服务器不能正常启动。

DocumentRoot "/var/www/html"

网页文件存放的目录

<Directory />

Options FollowSymLinks

AllowOverride None

</Directory>

这是对根目录的一个权限的设置

<Directory "/var/www/html">

Options Indexes FollowSymLinks

AllowOverride None

Order allow,deny

Allow from all

</Directory>

同上 其中的含义是:

options中 Indexes表示当网页不存在的时候允许索引显示目录中的文件,FollowSymLinks是否允许访问符号链接文件。还有得选项有ExecCGI表是否使用CGI,SymLinksOwnerMatch表示当符号链接的文件和目标文件为同一用户拥有时才允许访问。等选项

Order 对页面的访问控制顺序 后面的一项是默认选项,如allow,deny则默认是deny

Allow from all 表示允许所有的用户,通过和上一项结合可以控制对网站的访问控制

<IfModule mod_userdir.c>

UserDir disable

</IfModule>

是否允许用户访问其家目录,默认是不允许

HostnameLookups Off

当打开此项功能时,在记录日志的时候同时记录主机名,这需要服务器来反向解析域名,增加了服务器的负载,通常不建议开启

#EnableMMAP off

是否允许内存映射:如果httpd在传送过程中需要读取一个文件的内容,它是否可以使用内存映射。如果为on表示如果操作系统支持的话,将使用内存映射。在一些多核处理器的系统上,这可能会降低性能,如果在挂载了NFS的DocumentRoot上如果开启此项功能,可能造成因为分段而造成httpd崩溃

ErrorLog logs/error_log

错误日志存放的位置

ErrorLog logs/error_log

错误日志存放的位置

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

以上几个定义了日志的格式,并用不同的代号表示


<IfModule mod_dav_fs.c>

# Location of the WebDAV lock database.

DAVLockDB /var/lib/dav/lockdb

</IfModule>

这是对mod_dav_fs.c模块儿的管理

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

对CGI模块儿的的别名,与Alias相似。

虚拟主机

   

NameVirtualHost *:80

这是虚拟主机的设置,如果启用虚拟主机的话,必须将前面的注释去掉,而且,第二部分的内容都可以出现在每个虚拟主机部分。

#<VirtualHost *:80>

# ServerAdmin webmaster@网址

# DocumentRoot /www/docs/网址

# ServerName 网址

# ErrorLog logs/网址-error_log

# CustomLog logs/网址-access_log common

#</VirtualHost>

以上为一个虚拟主机的示例,和第二部分的内容完全相同。

一、编译安装apache

 

1、解决依赖关系

 

httpd-2.4.9需要较新版本的apr和apr-util,因此需要事先对其进行升级。升级方式有两种,一种是通过源代码编译安装,一种是直接升级rpm包。这里选择使用编译源代码的方式进行,它们的下载路径为ftp://172.16.0.1/pub/Sources/new_lamp。

 

(1) 编译安装apr

 

# tar xf apr-1.5.0.tar.bz2

# cd apr-1.5.0

# ./configure --prefix=/usr/local/apr

# make && make install

 

(2) 编译安装apr-util

 

# tar xf apr-util-1.5.3.tar.bz2

# cd apr-util-1.5.3

# ./configure--prefix=/usr/local/apr-util --with-apr=/usr/local/apr

# make && make install

 

附:apache官方对APR的介绍:

 

The mission of the Apache PortableRuntime (APR) project is to create and maintain software libraries that providea predictable and consistent interface to underlying platform-specificimplementations. The primary goal is to provide an API to which softwaredevelopers may code and be assured of predictable if not identical behaviourregardless of the platform on which their software is built, relieving them ofthe need to code special-case conditions to work around or take advantage ofplatform-specific deficiencies or features.

 

(3) httpd-2.4.9编译过程也要依赖于pcre-devel软件包,需要事先安装。此软件包系统光盘自带,因此,找到并安装即可。

 

2、编译安装httpd-2.4.9

 

首先下载httpd-2.4.9到本地,下载路径为ftp://172.16.0.1/pub/Sources/new_lamp。而后执行如下命令进行编译安装过程:

 

# tar xf httpd-2.4.9.tar.bz2

# cd httpd-2.4.9

# ./configure--prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl--enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr--with-apr-util=/usr/local/apr-util --enable-modules=most--enable-mpms-shared=all --with-mpm=event

# make && make install

 

 

补充:

 

(1)构建MPM为静态模块

在全部平台中,MPM都可以构建为静态模块。在构建时选择一种MPM,链接到服务器中。如果要改变MPM,必须重新构建。为了使用指定的MPM,请在执行configure脚本 时,使用参数 --with-mpm=NAME。NAME是指定的MPM名称。编译完成后,可以使用 ./httpd -l 来确定选择的MPM。 此命令会列出编译到服务器程序中的所有模块,包括 MPM。

 

(2)构建 MPM 为动态模块

 

在Unix或类似平台中,MPM可以构建为动态模块,与其它动态模块一样在运行时加载。 构建 MPM 为动态模块允许通过修改LoadModule指令内容来改变MPM,而不用重新构建服务器程序。在执行configure脚本时,使用--enable-mpms-shared选项即可启用此特性。当给出的参数为all时,所有此平台支持的MPM模块都会被安装。还可以在参数中给出模块列表。默认MPM,可以自动选择或者在执行configure脚本时通过--with-mpm选项来指定,然后出现在生成的服务器配置文件中。编辑LoadModule指令内容可以选择不同的MPM

3、修改httpd的主配置文件,设置其Pid文件的路径

 

编辑/etc/httpd/httpd.conf,添加如下行即可:

PidFile "/var/run/httpd.pid"

 

4、提供SysV服务脚本/etc/rc.d/init.d/httpd,内容如下:

#!/bin/bash

#

# httpd        Startup script for the Apache HTTPServer

#

# chkconfig: - 85 15

# description: Apache is a World WideWeb server.  It is used to serve \

#       HTML files and CGI.

# processname: httpd

# config: /etc/httpd/conf/httpd.conf

# config: /etc/sysconfig/httpd

# pidfile: /var/run/httpd.pid

 

# Source function library.

. /etc/rc.d/init.d/functions

 

if [ -f /etc/sysconfig/httpd ]; then

       . /etc/sysconfig/httpd

fi

 

# Start httpd in the C locale bydefault.

HTTPD_LANG=${HTTPD_LANG-"C"}

 

# This will prevent initlog fromswallowing up a pass-phrase prompt if

# mod_ssl needs a pass-phrase from theuser.

INITLOG_ARGS=""

 

# Set HTTPD=/usr/sbin/httpd.worker in/etc/sysconfig/httpd to use a server

# with the thread-based"worker" MPM; BE WARNED that some modules may not

# work correctly with a thread-basedMPM; notably PHP will refuse to start.

 

# Path to the apachectl script, serverbinary, and short-form for messages.

apachectl=/usr/local/apache/bin/apachectl

httpd=${HTTPD-/usr/local/apache/bin/httpd}

prog=httpd

pidfile=${PIDFILE-/var/run/httpd.pid}

lockfile=${LOCKFILE-/var/lock/subsys/httpd}

RETVAL=0

 

start() {

       echo -n $"Starting $prog: "

       LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS

       RETVAL=$?

       echo

       [ $RETVAL = 0 ] && touch ${lockfile}

       return $RETVAL

}

 

stop() {

 echo -n $"Stopping $prog: "

 killproc -p ${pidfile} -d 10 $httpd

 RETVAL=$?

 echo

 [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}

}

reload() {

   echo -n $"Reloading $prog: "

   if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then

       RETVAL=$?

       echo $"not reloading due to configuration syntax error"

       failure $"not reloading $httpd due to configuration syntaxerror"

   else

       killproc -p ${pidfile} $httpd -HUP

       RETVAL=$?

   fi

   echo

}

 

# See how we were called.

case "$1" in

 start)

 start

 ;;

 stop)

 stop

 ;;

 status)

       status -p ${pidfile} $httpd

 RETVAL=$?

 ;;

 restart)

 stop

 start

 ;;

 condrestart)

 if [ -f ${pidfile} ] ; then

   stop

   start

 fi

 ;;

 reload)

       reload

 ;;

 graceful|help|configtest|fullstatus)

 $apachectl $@

 RETVAL=$?

 ;;

 *)

 echo $"Usage: $prog{start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"

 exit 1

esac

 

exit $RETVAL

 

而后为此脚本赋予执行权限:

# chmod +x /etc/rc.d/init.d/httpd

 

加入服务列表:

# chkconfig --add httpd

 然后修改它的man手册

 

wKioL1Pq-A7RIheYAADgOJ6-U_I460.jpg

把man手册编译到    /etc/man.conf下

 Vim 、/etc/man.conf

wKioL1Pq-FPBB9V2AAEiXIiXaqY962.jpg

然后在我们的浏览器上键入

wKioL1Pq-GqBmdCiAAEixtCjGa4847.jpg

wKioL1Pq-IiRHbFkAAFGeYyBvNg692.jpg

 Bin 配置文件

Error错误信息

Include 头文件

Log 日志文件

我们也可以到httpd.apache.org上去下载httpp的最新版本

当你想要使用支持其他的语言是 可以使用manual

我们先查看一下/usr/local/apache

 

wKiom1Pq94zB0VyjAAGICr-ca2g029.jpg

修改

 Vim   /etc/httpd24/httpd.conf

wKiom1Pq-CmCjU4ZAAG8rSRt5hM528.jpg

     wKioL1Pq-XrwgLugAACqBND6o60968.jpg

出现这个问题的解决办法

vim/etc/httpd24/extra/httpd-manual.conf

这个文件的最后两项注释掉



      

    

wKiom1Pq99zwHdIzAAE0AHJyAR4637.jpg

在执行Service httpd24restart就可以了

:我们想要查看httpd24再那个模型下工作

                    可以使用 ps aux | grephttpd24


 

如果我们想要换一个mpm时:就在主配置文件里启用相应的mpm:MPM:

     prefork:多进程模型

     worker:多线程

     event:多线程,每个线程响应用户请求