Apachectl 和 Httpd 技巧
安装完Apache2之后, 如果你想用apachectl
和httpd
来发挥他们最大的潜能, 那你就不能仅仅使用start
, stop
, status
了. 下面的九个例子向你介绍了如何高效的使用它们.
给apachectl设置一个不同的httpd.conf
文件
一般来说, 当你需要设置一个不同的apache2指令时, 你需要修改原来的httpd.conf
文件, 但是利用下面的方法, 你就可以新建一个, 而不是修改原来的, 以达到测试的目的.
apachectl -f conf/httpd.conf.debug
# or
httpd -k start -f conf/httpd.conf.debug
看一下进程:
ps -ef | grep http
root 25080 1 0 23:26 00:00:00 /usr/sbin/httpd -f conf/httpd.conf.debug
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:
# cp httpd.conf.debug httpd.conf
# apachectl stop
# apachectl start
不修改httpd.conf
文件就更换网页根目录
用-c
选项可以很简单的更改网页的根目录, 而不必修改配置文件.
# httpd -k start -c "DocumentRoot /var/www/html_debug/"
提升日志记录的等级
用-e
选项可以更改apache2记录的日志等级
# httpd -k start -e debug
[Sun Aug 17 13:53:06 2008] [debug] mod_so.c(246): loaded module auth_basic_module
[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已编译的模块
# httpd -l
Compiled in modules:
core.c
prefork.c
http_core.c
mod_so.c
显示apache加载的模块(动态/静态)
上一个-l
选项只显示了静态编译过的模块, 这个-M
则会显示动态模块和共享模块.
# httpd -M
Loaded Modules:
core_module (static)
mpm_prefork_module (static)
http_module (static)
so_module (static)
auth_basic_module (shared)
auth_digest_module (shared)
authn_file_module (shared)
authn_alias_module (shared)
Syntax OK
显示httpd.conf
内所有可用的指令
这个-L
的选项类似于httpd的帮助扩展, 他会显示httpd.conf中可用的变量及其参数,
# httpd -L
HostnameLookups (core.c)
"on" to enable, "off" to disable reverse DNS lookups, or
"double" to enable double-reverse DNS lookups
Allowed in *.conf anywhere
ServerLimit (prefork.c)
Maximum value of MaxClients for this run of Apache
Allowed in *.conf only outside <Directory>, <Files> or
<Location>
KeepAlive (http_core.c)
Whether persistent connections should be On or Off
Allowed in *.conf only outside <Directory>, <Files> or
<Location>
LoadModule (mod_so.c)
a module name and the name of a shared object file to load
it from
Allowed in *.conf only outside <Directory>, <Files> or
<Location>
检测修改过的httpd.conf
是否有错误
当我们新更改过apache的配置文件后, 我们应该先检测一下里面是否有错误的语法或者配置不正确的参数, 用-t
这个参数:
# httpd -t -f conf/httpd.conf.debug
httpd: Syntax error on line 148 of
/etc/httpd/conf/httpd.conf.debug:
Cannot load /etc/httpd/modules/mod_auth_basicso into
server:
/etc/httpd/modules/mod_auth_basicso: cannot open shared
object file: No such file or directory
Once you fix the issue, it will display Syntax OK.
# httpd -t -f conf/httpd.conf.debug
Syntax OK
显示httpd
编译参数
# httpd -V
Server version: Apache/2.2.9 (Unix)
Server built:
Jul 14 2008 15:36:56
Server's Module Magic Number: 20051115:15
Server loaded:
APR 1.2.12, APR-Util 1.2.12
Compiled using: APR 1.2.12, APR-Util 1.2.12
Architecture: 32-bit
Server MPM: Prefork
threaded:
forked:
no
yes (variable process count)
Server compiled with....
-D APACHE_MPM_DIR="server/mpm/prefork"
-D APR_HAS_SENDFILE
-D HTTPD_ROOT="/etc/httpd"
-D SUEXEC_BIN="/usr/sbin/suexec"
-D DEFAULT_PIDLOG="logs/httpd.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_LOCKFILE="logs/accept.lock"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="conf/mime.types"
-D SERVER_CONFIG_FILE="conf/httpd.conf"
... ...
... ...
或者用-v
显示很少的信息:
# httpd -v
Server version: Apache/2.2.9 (Unix)
Server built:
Jul 14 2008 15:36:56
按需加载特定的模块
有时候你并不需要加载全部的模块, 你只需要加载某个特定的模块, 怎么做呢?
还是修改httpd.conf
文件:
<IfDefine load-ldap>
LoadModule ldap_module modules/mod_ldap.so
LoadModule authnz_ldap_module modules/mod_authnz_ldap.so
</IfDefine>
当你测试ldap的时候你会想加载所有与ldap有关的模块, 所以:
# httpd -k start -e debug -Dload-ldap -f
/etc/httpd/conf/httpd.conf.debug
[Sun Aug 17 14:14:58 2008] [debug] mod_so.c(246): loaded
module ldap_module
[Sun Aug 17 14:14:58 2008] [debug] mod_so.c(246): loaded
module authnz_ldap_module
[Note: Pass -Dload-ldap, to load the ldap modules into
Apache]
注意-D
参数哦~
# apachectl start
[Note: Start the Apache normally, if you don't want to
load the ldap modules.]