apache的相关配置

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

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

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

#rpm-qhttpd

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

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

安装事例:

#rpm-ivhhttpd-2.2.15-5.el6.i686.rpm

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

#servicehttpdstart

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

再补充两个命令:

#servicehttpdrestart/*重启服务*/

#servicehttpdstop/*停止服务*/

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

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

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

#chkconfig--level2345httpdon

如果是要关闭:

#chkconfig--level2345httpoff

3、apache服务的基本配置

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

httpd.conf由3大部分构成:GlobalEnvironment(apache全局配置)、MainServerConfiguration(主服务器配置)、VirtualHosts(虚拟主机配置)。

首先介绍全局配置文件(###Section1:GlobalEnvironment)

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

#youwillsaveyourselfalotoftrouble.

#DoNOTaddaslashattheendofthedirectorypath.

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

#PidFile:Thefileinwhichtheservershouldrecorditsprocess

#identificationnumberwhenitstarts.NotethePIDFILEvariablein

#/etc/sysconfig/httpdmustbesetappropriatelyifthislocationis

#changed.

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

#Timeout:Thenumberofsecondsbeforereceivesandsendstimeout.

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

#KeepAlive:Whetherornottoallowpersistentconnections(morethan

#onerequestperconnection).Setto"Off"todeactivate.

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

#MaxKeepAliveRequests:Themaximumnumberofrequeststoallow

#duringapersistentconnection.Setto0toallowanunlimitedamount.

#Werecommendyouleavethisnumberhigh,formaximumperformance.

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

#KeepAliveTimeout:Numberofsecondstowaitforthenextrequestfromthe

#sameclientonthesameconnection.

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

##Server-PoolSizeRegulation(MPMspecific)

<IfModuleprefork.c>

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

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

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

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

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

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

</IfModule>

#ChangethistoListenonspecificIPaddressesasshownbelowto

#preventApachefromglommingontoallboundIPaddresses(0.0.0.0)

#

#Listen12.34.56.78:80

Listen80//设置监听端口

#DynamicSharedObject(DSO)Support

/*****///加载dso模块,就像是windows中的dll(动态链接库)

#ExtendedStatuscontrolswhetherApachewillgenerate"full"status

#information(ExtendedStatusOn)orjustbasicinformation(ExtendedStatus

#Off)whenthe"server-status"handleriscalled.ThedefaultisOff.

ExtendedStatusOn//用于检测apache的状态信息。

#User/Group:Thename(or#number)oftheuser/grouptorunhttpdas.

#.OnSCO(ODT3)use"Usernouser"and"Groupnogroup".

#.OnHPUXyoumaynotbeabletousesharedmemoryasnobody,andthe

#suggestedworkaroundistocreateauserwwwandusethatuser.

#NOTEthatsomekernelsrefusetosetgid(Group)orsemctl(IPC_SET)

#whenthevalueof(unsigned)Groupisabove60000;

#don'tuseGroup#-1onthesesystems!

#

Userapache

Groupapache//设置apache工作时使用的用户和组。

然后介绍主服务器配置(###Section2:'Main'serverconfiguration)

#ServerAdmin:Youraddress,whereproblemswiththeservershouldbe

#e-mailed.Thisaddressappearsonsomeserver-generatedpages,such

#aserrordocuments.e.g.admin@your-domain.com

#

ServerAdminroot@localhost//当服务器发生问题的时候,apache会自动向这个邮箱发送相关邮件。

#

#Ifyourhostdoesn'thavearegisteredDNSname,enteritsIPaddresshere.

#Youwillhavetoaccessitbyitsaddressanyway,andthiswillmake

#redirectionsworkinasensibleway.

#

#ServerNamewww.example.com:80//设定apache服务器用于标识自身的名字和端口号。一般情况下,apache可以通过DNS服务器获得自身的名字,但服务器没有正式的

DNS名字时,需要我们设定。如果这个设置出错,将导致服务器不能正常启动。

#DocumentRoot:Thedirectoryoutofwhichyouwillserveyour

#documents.Bydefault,allrequestsaretakenfromthisdirectory,but

#symboliclinksandaliasesmaybeusedtopointtootherlocations.

#

DocumentRoot"/var/www/html"//指定了主机中网页文件的根目录。

##

<Directory/>

OptionsFollowSymLinks

AllowOverrideNone//设置apache根目录的访问权限和访问方式。

</Directory>

<Directory"/var/www/html">

OptionsIndexesFollowSymLinks

AllowOverrideNone

Orderallow,deny//设置主服务器网页文件存放目录的访问权限,

Allowfromall

</Directory>

<IfModulemod_userdir.c>

UserDirdisabled

#UserDirpublic_html//表示设置用户是否可以在自己的目录下建立public_html目录在存放自己的网页文件,如果把userDir前面的#去掉,则表示用户可以通过http://ip地址:端口/~用户名来访问其中的内容。

</IfModule>

DirectoryIndexindex.htmlindex.html.var//设置预设首页,默认是:index.html。设置以后,用户通过“http://服务器IP地址:端口/”访问的就是"index.html"。

AccessFileName.htapacheace//设置apache目录访问权限的控制文件,这里我们设的是.htapacheace。

<Files~"^\.ht">

Orderallow,deny

Denyfromall//防止用户看到以.ht开头的文件,保护.htaccess\.htpasswd的内容。防止他人盗取相关用户名和密码。

SatisfyAll

</Files>

#TypesConfigdescribeswherethemime.typesfile(orequivalent)is

#tobefound.

TypesConfig/etc/mime.types//指定存放MIME文件类型的文件,用户也可以编辑该文件。

DefaultTypetext/plain//如果apache遇到不认识的文件类型的时候,自动将该文件当成文本文件

HostnameLookupsOff//如果设置On,则每次都会向DNS服务器要求解析该IP,这样会花费额外的服务器资源,并且降低服务器的相应速度,所以一般设置为Off。

ErrorLoglogs/error_log//如果服务器发生错误,所有的日志文件都会存到log

s/error_log下。

CustomLoglogs/access_logcombined//设置存取文件记录采用combined模式。

ServerSignatureOn//当设置为On时,出错的时候服务器所产生的网页会显示Apache的版本号、主机、连接端口等信息;如果设置为E-Mail,则会有“mailto:”的超链接

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

<Directory"/var/www/icons">

OptionsIndexesMultiViewsFollowSymLinks

AllowOverrideNone

Orderallow,deny//定义一个图标虚拟目录,并设置访问权限。

Allowfromall

</Directory>

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

<Directory"/var/www/cgi-bin">

AllowOverrideNone

OptionsNone

Orderallow,deny//设置脚本的文件目录。

Allowfromall

</Directory>

IndexOptionsFancyIndexingVersionSortNameWidth=*HTMLTableCharset=UTF-8

//采用更好看的带有格式的文件列表方式,字符编码为utf-8

AddIconByEncoding(CMP,/icons/compressed.gif)x-compressx-gzip

AddIconByType(TXT,/icons/text.gif)text/*

AddIconByType(IMG,/icons/image2.gif)image/*

AddIconByType(SND,/icons/sound2.gif)audio/*

AddIconByType(VID,/icons/movie.gif)video/*

............................//文件列表各种文件类型对应的图标显示。

DefaultIcon/icons/unknown.gif

ReadmeNameREADME.html

HeaderNameHEADER.html//显示文件清单时,README.html的内容就会显示在页面的最下端,HEADER.html的内容就会显示在页面的最上端。

IndexIgnore.??**~*#HEADER*README*RCSCVS*,v*,t//忽略这些类型的文件。

#DefaultLanguagenl//设置页面的默认语言,这一行通常是被注释掉的。

AddLanguageca.ca

AddLanguagecs.cz.cs

AddLanguageda.dk

..........

AddLanguagezh-CN.zh-cn//设置页面语言

AddLanguagezh-TW.zh-tw

LanguagePriorityencacsdadeeleoesetfrhehritjakoltznlnnnoplptpt-BRrusvzh-CNzh-TW//设置页面语言的优先级。

AddDefaultCharsetUTF-8//设置默认的字符编码格式。

AddTypeapplication/x-compress.Z

AddTypeapplication/x-gzip.gz.tgz//增加MIME的类型。

AddTypetext/html.shtml

AddOutputFilterINCLUDES.shtml//使用动态页面

#ErrorDocument500"Theservermadeabooboo."

#ErrorDocument404/missing.html

#ErrorDocument404"/cgi-bin/missing_handler.pl"

#ErrorDocument402http://www.example.com/subscription_info.html//设置各种错误类型的显示方式。

BrowserMatch"Mozilla/2"nokeepalive//如过浏览器符合这钟类型,则不提供keepalive的支持。

BrowserMatch"MSIE4\.0b2;"nokeepalivedowngrade-1.0force-response-1.0

BrowserMatch"RealPlayer4\.0"force-response-1.0

BrowserMatch"Java/1\.0"force-response-1.0

BrowserMatch"JDK/1\.0"force-response-1.0//如果浏览器符合这四种方式,则采用“HTTP/1.0”回应。

最后是虚拟主机的配置(Section3:VirtualHosts)

<VirtualHost*:80>

ServerAdminwebmaster@dummy-host.example.com//配置虚拟主机管理员的电子邮件地址

DocumentRoot/www/docs/dummy-host.example.com//设置虚拟主机网页存放位置

ServerNamedummy-host.example.com//设置虚拟主机的名称

ErrorLoglogs/dummy-host.example.com-error_log//设置错误日志的存放位置

CustomLoglogs/dummy-host.example.com-access_logcommon//CustomLog指令用来对服务器的请求进行日志记录。可以使用环境变量根据请求的特征来自由地组织日志。

</VirtualHost>

关于apache的更高级的配置,下面的blog将进行详解。

有两种方法可以让Apache在系统启动时自动启动。

1.在/etc/rc.d/rc.local中增加启动apache的命令,例如:/usr/local/httpd/bin/apachectlstart

2.将apache注册为系统服务

首先将apachectl命令拷贝至/etc/rc.d/init.d目录下,改名为httpd

使用编辑器打开httpd文件,并在第一行#!/bin/sh下增加两行文字如下

#chkconfig:357030

#description:Apache

接着注册该服务

chkconfig--addhttpd

一切OK了,启动服务

servicehttpdstart

其中所增加的第二行中三个数字第一个表示在运行级别3和5下启动apache,第二、三是关于启动和停止的优先级配置,无关紧要。

相关推荐