29.9. 配置/etc/httpd/conf/httpd.conf文件

httpd.conf文件是 Apache Web 服务器的主要配置文件。 有许多选项,阅读 Apache 附带的文档以获取有关不同设置和参数的更多信息非常重要。 以下配置示例是 Apache 的最小工作配置文件,并支持 SSL。 此外,重要的是要注意,我们仅注释与安全和优化相关的参数,并将所有其他参数留给您自行研究。

编辑httpd.conf文件,vi/etc/httpd/conf/httpd.conf并添加/更改

      ### Section 1: Global Environment
      #
      ServerType standalone
      ServerRoot "/etc/httpd"
      PidFile /var/run/httpd.pid
      ResourceConfig /dev/null
      AccessConfig /dev/null
      Timeout 300
      KeepAlive On
      MaxKeepAliveRequests 0
      KeepAliveTimeout 15
      MinSpareServers 16
      MaxSpareServers 64
      StartServers 16
      MaxClients 512
      MaxRequestsPerChild 100000

      ### Section 2: 'Main' server configuration
      #
      Port 80

      <IfDefine SSL>
      Listen 80
      Listen 443
      </IfDefine>

      User www
      Group www
      ServerAdmin admin@openna.com
      ServerName www.openna.com
      DocumentRoot "/home/httpd/ona"

      <Directory />
      Options None
      AllowOverride None
      Order deny,allow
      Deny from all
      </Directory>

      <Directory "/home/httpd/ona">
      Options None
      AllowOverride None
      Order allow,deny
      Allow from all
      </Directory>

      <Files .pl>
      Options None
      AllowOverride None
      Order deny,allow
      Deny from all
      </Files>

      <IfModule mod_dir.c>
      DirectoryIndex index.htm index.html index.php index.php3 default.html index.cgi
      </IfModule>

      #<IfModule mod_include.c>
      #Include conf/mmap.conf
      #</IfModule>

      UseCanonicalName On

      <IfModule mod_mime.c>
      TypesConfig /etc/httpd/conf/mime.types
      </IfModule>

      DefaultType text/plain
      HostnameLookups Off

      ErrorLog /var/log/httpd/error_log
      LogLevel warn
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
      SetEnvIf Request_URI \.gif$ gif-image
      CustomLog /var/log/httpd/access_log combined env=!gif-image
      ServerSignature Off

      <IfModule mod_alias.c>
      ScriptAlias /cgi-bin/  "/home/httpd/cgi-bin/"
      <Directory "/home/httpd/cgi-bin">
      AllowOverride None
      Options None
      Order allow,deny
      Allow from all
      </Directory>
      </IfModuleGT;

      <IfModule mod_mime.c>
      AddEncoding x-compress Z
      AddEncoding x-gzip gz tgz

      AddType application/x-tar .tgz
      </IfModule>

      ErrorDocument 500 "The server made a boo boo.
      ErrorDocument 404 http://192.168.1.1/error.htm
      ErrorDocument 403 "Access Forbidden -- Go away.

      <IfModule mod_setenvif.c>
      BrowserMatch "Mozilla/2" nokeepalive
      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
      </IfModule>

      ### Section 3: Virtual Hosts
      #
      <IfDefine SSL>
      AddType application/x-x509-ca-cert .crt
      AddType application/x-pkcs7-crl    .crl
      </IfDefine>

      <IfModule mod_ssl.c>
      SSLPassPhraseDialog     builtin
      SSLSessionCache         dbm:/var/run/ssl_scache
      SSLSessionCacheTimeout  300

      SSLMutex  file:/var/run/ssl_mutex

      SSLRandomSeed startup builtin
      SSLRandomSeed connect builtin

      SSLLog      /var/log/httpd/ssl_engine_log
      SSLLogLevel warn
      </IfModule>

      <IfDefine SSL>
      <VirtualHost _default_:443>

      DocumentRoot "/home/httpd/ona"
      ServerName www.openna.com
      ServerAdmin admin@openna.com
      ErrorLog /var/log/httpd/error_log

      SSLEngine on
      SSLCipherSuite ALL:!ADH:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL

      SSLCertificateFile      /etc/ssl/certs/server.crt
      SSLCertificateKeyFile   /etc/ssl/private/server.key
      SSLCACertificatePath    /etc/ssl/certs
      SSLCACertificateFile    /etc/ssl/certs/ca.crt
      SSLCARevocationPath     /etc/ssl/crl
      SSLVerifyClient none
      SSLVerifyDepth  10

      SSLOptions +ExportCertData +StrictRequire
      SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
      SetEnvIf Request_URI \.gif$ gif-image
      CustomLog /var/log/httpd/ssl_request_log \
      "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b" env=!gif-image
      </VirtualHost>
      </IfDefine>
    

这告诉httpd.conf文件为此特定配置设置自身,使用

ServerType standalone

选项ServerType指定 Apache 应如何在系统上运行。 您可以从超级服务器 inetd 运行它,也可以作为独立守护程序运行。 强烈建议以独立类型运行 Apache,以获得更好的性能和速度。

ServerRoot "/etc/httpd"

选项ServerRoot指定 Apache 服务器的配置文件所在的目录。 它允许 Apache 知道在启动时在哪里可以找到其配置文件。

PidFile /var/run/httpd.pid

选项PidFile指定服务器将在启动时记录守护程序的进程 ID 的位置。 仅当您在独立模式下配置 Apache 时,此选项才是必需的。

ResourceConfig /dev/null

选项ResourceConfig指定旧的srm.conf文件,Apache 在完成读取httpd.conf文件后读取。 当您将位置设置为/dev/null,Apache 允许您在此文件中包含此文件的内容httpd.conf文件中,这样,您就只有一个文件来处理所有配置参数,以简化操作。

AccessConfig /dev/null

选项AccessConfig指定旧的access.conf文件,Apache 在完成读取srm.conf文件后读取。 当您将位置设置为/dev/null,Apache 允许您在此文件中包含此文件的内容httpd.conf文件中,这样,您就只有一个文件来处理所有配置参数,以简化操作。

Timeout 300

选项Timeout指定 Apache 将等待 GET、POST、PUT 请求和传输确认的时间量。 您可以安全地将此选项保留为默认值。

KeepAlive On

选项KeepAlive,如果设置为On,指定在此 Web 服务器上启用持久连接。 为了获得更好的性能,建议将此选项设置为On,并允许每个连接多个请求。

MaxKeepAliveRequests 0

选项MaxKeepAliveRequests指定当KeepAlive上面的选项设置为On。当此选项的值设置为0时,服务器上允许无限制的请求。 为了服务器性能,建议允许无限制的请求。

KeepAliveTimeout 15

选项KeepAliveTimeout指定 Apache 在关闭连接之前等待后续请求的时间(以秒为单位)。15秒是服务器性能的良好平均值。

MinSpareServers 16

选项MinSpareServers指定 Apache 的空闲子服务器进程的最小数量,这些进程不处理请求。 这是关于 Apache Web 服务器性能的重要调整参数。 对于高负载操作,建议值为16,这是互联网上各种基准测试推荐的值。

MaxSpareServers 64

选项MaxSpareServers指定 Apache 的空闲子服务器进程的最大数量,这些进程不处理请求。 这也是关于 Apache Web 服务器性能的重要调整参数。 对于高负载操作,值为64,这是互联网上各种基准测试推荐的值。

StartServers 16

选项StartServers指定 Apache 在启动时将创建的子服务器进程的数量。 这再次是关于 Apache Web 服务器性能的重要调整参数。 对于高负载操作,值为16,这是互联网上各种基准测试推荐的值。

MaxClients 512

选项MaxClients指定 Apache 可以支持的并发请求数。 这也是关于 Apache Web 服务器性能的重要调整参数。 对于高负载操作,值为512,这是互联网上各种基准测试推荐的值。

MaxRequestsPerChild 100000

选项MaxRequestsPerChild指定单个子服务器进程将处理的请求数。 这也是关于 Apache Web 服务器性能的重要调整参数。

User www

选项User指定 Apache 服务器将以其身份运行的 UID。 创建一个对系统具有最小访问权限的新用户非常重要,该用户的功能仅用于运行 Web 服务器守护程序。

Group www

选项Group指定 Apache 服务器将以其身份运行的 GID。 创建一个对系统具有最小访问权限的新组非常重要,该组的功能仅用于运行 Web 服务器守护程序。

DirectoryIndex index.htm index.html index.php index.php3 default.html index.cgi

选项DirectoryIndex指定 Apache 用作预先编写的 HTML 目录索引的文件。 换句话说,如果 Apache 找不到要显示的默认索引页面,它将尝试此参数中的下一个条目(如果可用)。 为了提高 Web 服务器的性能,建议首先列出您网站最常用的默认索引页面。

Include conf/mmap.conf

选项Include指定可以从服务器配置文件中包含的其他文件的位置httpd.conf。 在我们的例子中,我们包含mmap.conf文件,位于/etc/httpd/conf目录。 此文件mmap.conf将文件映射到内存中以加快服务速度。 有关更多信息,请参阅关于 优化 Apache 的章节。

HostnameLookups Off

选项HostnameLookups,如果设置为Off,指定禁用 DNS 查找。 建议将此选项设置为Off,以便节省网络流量时间并提高 Apache Web 服务器的性能。