Apachectl 和 Httpd 技巧

安装完Apache2之后, 如果你想用apachectlhttpd来发挥他们最大的潜能, 那你就不能仅仅使用start, stop, status了. 下面的九个例子向你介绍了如何高效的使用它们.

给apachectl设置一个不同的httpd.conf文件

一般来说, 当你需要设置一个不同的apache2指令时, 你需要修改原来的httpd.conf文件, 但是利用下面的方法, 你就可以新建一个, 而不是修改原来的, 以达到测试的目的.

  1. apachectl -f conf/httpd.conf.debug
  2. # or
  3. httpd -k start -f conf/httpd.conf.debug

看一下进程:

  1. ps -ef | grep http
  2. root 25080 1 0 23:26 00:00:00 /usr/sbin/httpd -f conf/httpd.conf.debug
  3. apache 25099 25080 0 23:28 00:00:00 /usr/sbin/httpd -f conf/httpd.conf.debug

如果这个conf/httpd.conf.debug文件没有问题的话, 你就可以把它复制为conf/httpd.conf, 然后重启apache:

  1. # cp httpd.conf.debug httpd.conf
  2. # apachectl stop
  3. # apachectl start

不修改httpd.conf文件就更换网页根目录

-c选项可以很简单的更改网页的根目录, 而不必修改配置文件.

  1. # httpd -k start -c "DocumentRoot /var/www/html_debug/"

提升日志记录的等级

-e选项可以更改apache2记录的日志等级

  1. # httpd -k start -e debug
  2. [Sun Aug 17 13:53:06 2008] [debug] mod_so.c(246): loaded module auth_basic_module
  3. [Sun Aug 17 13:53:06 2008] [debug] mod_so.c(246): loaded module auth_digest_module

可用的等级有:debug, info, notice, warn,
error, crit, alert, emerg
.

显示apache已编译的模块

  1. # httpd -l
  2. Compiled in modules:
  3. core.c
  4. prefork.c
  5. http_core.c
  6. mod_so.c

显示apache加载的模块(动态/静态)

上一个-l选项只显示了静态编译过的模块, 这个-M则会显示动态模块和共享模块.

  1. # httpd -M
  2. Loaded Modules:
  3. core_module (static)
  4. mpm_prefork_module (static)
  5. http_module (static)
  6. so_module (static)
  7. auth_basic_module (shared)
  8. auth_digest_module (shared)
  9. authn_file_module (shared)
  10. authn_alias_module (shared)
  11. Syntax OK

显示httpd.conf内所有可用的指令

这个-L的选项类似于httpd的帮助扩展, 他会显示httpd.conf中可用的变量及其参数,

  1. # httpd -L
  2. HostnameLookups (core.c)
  3. "on" to enable, "off" to disable reverse DNS lookups, or
  4. "double" to enable double-reverse DNS lookups
  5. Allowed in *.conf anywhere
  6. ServerLimit (prefork.c)
  7. Maximum value of MaxClients for this run of Apache
  8. Allowed in *.conf only outside <Directory>, <Files> or
  9. <Location>
  10. KeepAlive (http_core.c)
  11. Whether persistent connections should be On or Off
  12. Allowed in *.conf only outside <Directory>, <Files> or
  13. <Location>
  14. LoadModule (mod_so.c)
  15. a module name and the name of a shared object file to load
  16. it from
  17. Allowed in *.conf only outside <Directory>, <Files> or
  18. <Location>

检测修改过的httpd.conf是否有错误

当我们新更改过apache的配置文件后, 我们应该先检测一下里面是否有错误的语法或者配置不正确的参数, 用-t这个参数:

  1. # httpd -t -f conf/httpd.conf.debug
  2. httpd: Syntax error on line 148 of
  3. /etc/httpd/conf/httpd.conf.debug:
  4. Cannot load /etc/httpd/modules/mod_auth_basicso into
  5. server:
  6. /etc/httpd/modules/mod_auth_basicso: cannot open shared
  7. object file: No such file or directory
  8. Once you fix the issue, it will display Syntax OK.
  9. # httpd -t -f conf/httpd.conf.debug
  10. Syntax OK

显示httpd编译参数

  1. # httpd -V
  2. Server version: Apache/2.2.9 (Unix)
  3. Server built:
  4. Jul 14 2008 15:36:56
  5. Server's Module Magic Number: 20051115:15
  6. Server loaded:
  7. APR 1.2.12, APR-Util 1.2.12
  8. Compiled using: APR 1.2.12, APR-Util 1.2.12
  9. Architecture: 32-bit
  10. Server MPM: Prefork
  11. threaded:
  12. forked:
  13. no
  14. yes (variable process count)
  15. Server compiled with....
  16. -D APACHE_MPM_DIR="server/mpm/prefork"
  17. -D APR_HAS_SENDFILE
  18. -D HTTPD_ROOT="/etc/httpd"
  19. -D SUEXEC_BIN="/usr/sbin/suexec"
  20. -D DEFAULT_PIDLOG="logs/httpd.pid"
  21. -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
  22. -D DEFAULT_LOCKFILE="logs/accept.lock"
  23. -D DEFAULT_ERRORLOG="logs/error_log"
  24. -D AP_TYPES_CONFIG_FILE="conf/mime.types"
  25. -D SERVER_CONFIG_FILE="conf/httpd.conf"
  26. ... ...
  27. ... ...

或者用-v显示很少的信息:

  1. # httpd -v
  2. Server version: Apache/2.2.9 (Unix)
  3. Server built:
  4. Jul 14 2008 15:36:56

按需加载特定的模块

有时候你并不需要加载全部的模块, 你只需要加载某个特定的模块, 怎么做呢?

还是修改httpd.conf文件:

  1. <IfDefine load-ldap>
  2. LoadModule ldap_module modules/mod_ldap.so
  3. LoadModule authnz_ldap_module modules/mod_authnz_ldap.so
  4. </IfDefine>

当你测试ldap的时候你会想加载所有与ldap有关的模块, 所以:

  1. # httpd -k start -e debug -Dload-ldap -f
  2. /etc/httpd/conf/httpd.conf.debug
  3. [Sun Aug 17 14:14:58 2008] [debug] mod_so.c(246): loaded
  4. module ldap_module
  5. [Sun Aug 17 14:14:58 2008] [debug] mod_so.c(246): loaded
  6. module authnz_ldap_module
  7. [Note: Pass -Dload-ldap, to load the ldap modules into
  8. Apache]

注意-D参数哦~

  1. # apachectl start
  2. [Note: Start the Apache normally, if you don't want to
  3. load the ldap modules.]