返回介绍

Hack-81 Apachectl 和 Httpd 技巧

发布于 2025-03-08 17:40:24 字数 5203 浏览 0 评论 0 收藏 0

Apachectl 和 Httpd 技巧

安装完 Apache2 之后,如果你想用 apachectlhttpd 来发挥他们最大的潜能,那你就不能仅仅使用 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.]

本书简介:

  • Linux 进阶技巧
  • 巧妙的命令组合
  • Bash 某些技巧
  • 一共一百零一个(包括充数的)
  • 最后有个奖励章(额外技巧)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文