apache服务器是目前因特网上最流行的web服务器之一,它的开源性,使得软件可以不断的更新和升级,这样安全性能也在不断的提高,下面我们就来体验一下red hat enterprise linux 下apache的搭建与配置。

 1、apcahe服务安装的前期准备

 red hat 下 apache服务httpd 默认情况下是没有被安装的,如果你在安装的时候没有定制软件的话。你可以通过以下命令来查询你的机子上是否已经安装了httpd服务。

# rpm -q httpd 

如果给你返回没有安装此软件的信息,那也没事,安装httpd服务很简单。如果你有安装光碟或者镜像文件,你可以直接使用。在你挂载cdrom 或iso后,你可以去 /media/cdrom/Server/ 下寻找httpd相关的软件包。相关的软件包有:httpd、httpd-manual、httpd-tools等。当然你也直接去apache的官网上下载软件包。

另外有一点需要我们注意,安装httpd服务需要50MB的临时磁盘空间,安装后的apache服务需要占用10MB左右的磁盘空间

安装事例:

#rpm -ivh httpd-2.2.15-5.el6.i686.rpm

然后执行以下的命令启动httpd服务:

#service httpd start

最后我们在客户端浏览器中输入服务器的IP地址,如果可以进行访问,恭喜你安装成功了。

再补充两个命令:

#service httpd restart /*重启服务*/

#service httpd stop  /*停止服务*/

2、让apache服务器开机自启动:

图形化界面中单击“系统”—“管理”—“服务”,然后选择httpd服务,如果是红色的点将其点绿。

文字化界面中,我们可以 利用chkconfig命令来完成.

# chkconfig --level 2345 httpd on 

如果是要关闭:

#chkconfig --level 2345 http off 

3、apache服务的基本配置

apache的主配置文件是:/etc/httpd/conf/httpd.conf 。注意:httpd.conf的配置语句除了选项的参数值以外,所有选项命令均不区分大小写。

httpd.conf由3大部分构成:Global Environment(apache全局配置)、Main Server Configuration(主服务器配置)、Virtual Hosts(虚拟主机配置)。

首先介绍全局配置文件(### Section 1: Global Environment)

 

# (available at <URL:http://httpd.apache.org/docs/2.2/mod/mpm_common.html#lockfile>);

# you will save yourself a lot of trouble.

# Do NOT add a slash at the end of the directory path.

ServerRoot "/etc/httpd"    //apache的根目录:/etc/httpd ,该目录下包括配置文件、记录文件、模块文件等。

# PidFile: The file in which the server should record its process

# identification number when it starts.  Note the PIDFILE variable in

# /etc/sysconfig/httpd must be set appropriately if this location is

# changed.

PidFile run/httpd.pid    // 在 /var/run/httpd.pid 保存着apache父进程的ID

 

# Timeout: The number of seconds before receives and sends time out.

Timeout 60    //超出时间控制,若客户端超过60s还没有连接上Server,或者Server超过60s还没有传送信息给客户端,则强制断线。

# KeepAlive: Whether or not to allow persistent connections (more than

# one request per connection). Set to "Off" to deactivate.

KeepAlive Off   //表示不允许客户端同时提出多个请求,on即可允许。

 

# MaxKeepAliveRequests: The maximum number of requests to allow

# during a persistent connection. Set to 0 to allow an unlimited amount.

# We recommend you leave this number high, for maximum performance.

MaxKeepAliveRequests 100   //表示每次联系允许的最大请求数目,数字越大,效率越高,0表示不受限制。

# KeepAliveTimeout: Number of seconds to wait for the next request from the

# same client on the same connection.

KeepAliveTimeout 15    //表示用户端的请求如果15s没有发出,则断线。

 

## Server-Pool Size Regulation (MPM specific)

 

<IfModule prefork.c>

StartServers       8      //启动时打开的httpd进程数目为8

MinSpareServers    5      //至少会有5个闲置的httpd进程来监听用户的请求

MaxSpareServers   20      //最多的闲置的httpd进程数为20 。如果网站的访问量很大,可将这个数目设置大一些。

ServerLimit      256      //客户端链接服务器的最大生存时间。ttl值

MaxClients       256      //表示限制客户端的同时最大连接数为256.一旦达到这个数目,客户端就会备拒绝访问。

MaxRequestsPerChild  4000  //表示限制每个httpd进程可以完成的最大任务数。

</IfModule>

# Change this to Listen on specific IP addresses as shown below to 
# prevent Apache from glomming onto all bound IP addresses (0.0.0.0)
#
#Listen 12.34.56.78:80
Listen 80  //设置监听端口
 
# Dynamic Shared Object (DSO) Support  
/*****/                  //加载dso模块,就像是windows中的dll(动态链接库)

 

 

# ExtendedStatus controls whether Apache will generate "full" status
# information (ExtendedStatus On) or just basic information (ExtendedStatus
# Off) when the "server-status" handler is called. The default is Off.
ExtendedStatus On      //用于检测apache的状态信息。
# User/Group: The name (or #number) of the user/group to run httpd as.
#  . On SCO (ODT 3) use "User nouser" and "Group nogroup".
#  . On HPUX you may not be able to use shared memory as nobody, and the
#    suggested workaround is to create a user www and use that user.
#  NOTE that some kernels refuse to setgid(Group) or semctl(IPC_SET)
#  when the value of (unsigned)Group is above 60000; 
#  don't use Group #-1 on these systems!
#
User apache     
Group apache      //设置apache工作时使用的用户和组。
 
然后介绍主服务器配置(### Section 2: 'Main' server configuration)
 
# ServerAdmin: Your address, where problems with the server should be
# e-mailed.  This address appears on some server-generated pages, such
# as error documents.  e.g. admin@your-domain.com
#
ServerAdmin root@localhost  // 当服务器发生问题的时候,apache会自动向这个邮箱发送相关邮件。
#
# If your host doesn't have a registered DNS name, enter its IP address here.
# You will have to access it by its address anyway, and this will make 
# redirections work in a sensible way.
#
#ServerName www.example.com:80  //设定apache服务器用于标识自身的名字和端口号。一般情况下,apache可以通过DNS服务器获得自身的名字,但服务器没有正式的
DNS名字时,需要我们设定。如果这个设置出错,将导致服务器不能正常启动。
 
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html"  //指定了主机中网页文件的根目录。
 
##
<Directory />
    Options FollowSymLinks
    AllowOverride None          //设置apache根目录的访问权限和访问方式。
</Directory>                  
 
<Directory "/var/www/html">
 Options Indexes FollowSymLinks  
 AllowOverride None 
 Order allow,deny               //设置主服务器网页文件存放目录的访问权限,
 Allow from all
</Directory>
 
<IfModule mod_userdir.c>   
    UserDir disabled
    #UserDir public_html    //表示设置用户是否可以在自己的目录下建立public_html目录在存放自己的网页文件,如果把userDir前面的#去掉,则表示用户可以通过http://ip地址:端口/~用户名 来访问其中的内容。
</IfModule>
 
DirectoryIndex index.html index.html.var   //设置预设首页,默认是:index.html。设置以后,用户通过“http://服务器IP地址:端口/”访问的就是"index.html"。
 
AccessFileName .htapacheace  //设置apache目录访问权限的控制文件,这里我们设的是.htapacheace。
 
<Files ~ "^\.ht">
    Order allow,deny
    Deny from all   //防止用户看到以.ht开头的文件,保护.htaccess\.htpasswd的内容。防止他人盗取相关用户名和密码。
    Satisfy All
</Files>
 
# TypesConfig describes where the mime.types file (or equivalent) is
# to be found.
TypesConfig /etc/mime.types   //指定存放MIME文件类型的文件,用户也可以编辑该文件。
 
DefaultType text/plain   //如果apache遇到不认识的文件类型的时候,自动将该文件当成文本文件
 
HostnameLookups Off     //如果设置On,则每次都会向DNS服务器要求解析该IP,这样会花费额外的服务器资源,并且降低服务器的相应速度,所以一般设置为Off。
 
ErrorLog logs/error_log   //如果服务器发生错误,所有的日志文件都会存到log
s/error_log 下。
 
CustomLog logs/access_log combined //设置存取文件记录采用combined模式。
 
ServerSignature On     // 当设置为On时,出错的时候服务器所产生的网页会显示Apache 的版本号、主机、连接端口等信息;如果设置为E-Mail,则会有“mailto:”的超链接
 
Alias /icons/ "/var/www/icons/"
 
<Directory "/var/www/icons">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order allow,deny      //定义一个图标虚拟目录,并设置访问权限。
    Allow from all
</Directory>
 
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
 
<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Order allow,deny     //设置脚本的文件目录。
    Allow from all
</Directory>
 
IndexOptions FancyIndexing VersionSort NameWidth=* HTMLTable Charset=UTF-8
//采用更好看的带有格式的文件列表方式 ,字符编码为utf-8
 
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/*
............................                  //文件列表各种文件类型对应的图标显示。
DefaultIcon /icons/unknown.gif
 
ReadmeName README.html
HeaderName HEADER.html    //显示文件清单时,README.html的内容就会显示在页面的最下端,HEADER.html的内容就会显示在页面的最上端。
 
IndexIgnore .??* *~ *# HEADER* README* RCS CVS *,v *,t  //忽略这些类型的文件。
 
# DefaultLanguage nl   //设置页面的默认语言,这一行通常是被注释掉的。
 
AddLanguage ca .ca
AddLanguage cs .cz .cs
AddLanguage da .dk
..........
AddLanguage zh-CN .zh-cn     //设置页面语言
AddLanguage zh-TW .zh-tw
 
LanguagePriority en ca cs da de el eo es et fr he hr it ja ko ltz nl nn no pl pt pt-BR ru sv zh-CN zh-TW  //设置页面语言的优先级。
 
AddDefaultCharset UTF-8   //设置默认的字符编码格式。
 
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz   //增加MIME的类型。
 
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml   //使用动态页面
 
#ErrorDocument 500 "The server made a boo boo."
#ErrorDocument 404 /missing.html
#ErrorDocument 404 "/cgi-bin/missing_handler.pl"
#ErrorDocument 402 http://www.example.com/subscription_info.html  //设置各种错误类型的显示方式。
 
BrowserMatch "Mozilla/2" nokeepalive   //如过浏览器符合这钟类型,则不提供keepalive的支持。
BrowserMatch "MSIE 4\.0b2;" nokeepalive downgrade-1.0 force-response-1.0
BrowserMatch "RealPlayer 4\.0" force-response-1.0
BrowserMatch "Java/1\.0" force-response-1.0
BrowserMatch "JDK/1\.0" force-response-1.0  //如果浏览器符合这四种方式,则采用“HTTP/1.0”回应。
 
最后是虚拟主机的配置(Section 3: Virtual Hosts)
<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 //  CustomLog 指令用来对服务器的请求进行日志记录。可以使用环境变量根据请求的特征来自由地组织日志。
</VirtualHost>
 
关于apache的更高级的配置,下面的blog将进行详解。