Total Pageviews

Thursday 25 October 2012

编译安装Apache

编译Apache的相关知识

预备知识

Makefile的惯例

  1. make clean 清除当前目录下在 make 过程中产生的文件。它不能删除软件包的配置文件,也不能删除 build 时创建的那些文件。
  2. make distclean 类似于”clean”,但增加删除当前目录下的的配置文件、build 过程产生的文件。
  3. make install-strip 和”make install”类似,但是会对复制到安装目录下的可执行文件进行 strip 操作。

make的常用选项

  1. -jN , –jobs[=N],指定并行执行的命令数目。
  2. -n –just-print,–dry-run,–recon,只打印出所要执的命令,但并不实际执行命令。
  3. -s –silent,–quit,不显示所执行的命令。

编译Apache的要求

  1. 磁盘空间
  2. ANSI-C编译器及编译环境
  3. 确保准确的时间
  4. Perl 5 [可选]
  5. apr/apr-util >= 1.2
apr和apr-util包含在Apache httpd的发行源代码中,并且在绝大多数情况下使用都不会出现问题。当然,如果apr或apr-util的1.0或1.1版本已经安装在你的系统中了, 则必须将你的apr/apr-util升级到1.2版本,或者将httpd单独分开编译。要使用发行源代码中自带的apr/apr-util源代码进行安 装,你必须手动完成:
# 编译和安装 apr 1.2
cd srclib/apr
./configure –prefix=/usr/local/apr-httpd/
make
make install
# 编译和安装 apr-util 1.2
cd ../apr-util
./configure –prefix=/usr/local/apr-util-httpd/ –with-apr=/usr/local/apr-httpd/
make
make install
# 配置 httpd
cd ../../
./configure –with-apr=/usr/local/apr-httpd/ –with-apr-util=/usr/local/apr-util-httpd/

Apache的模块状态

通常我们认为Apache的模块分五类:多路处理模块(MPM),基本模块(Base),扩展模块(Extension),实验性模块(Experimental),第三方模块(External)。
关于各模块的状态详情可以查看手册。第三方模块不包含在发行版中,手册中只标识了四种状态:
M 多路处理模块 必须有且仅有一个MPM被静态编译到服务器中。
B 基本模块 默认包含,必须明确禁用。
E 扩展模块 默认不包含,必须明确启用。
X 试验模块 默认不包含,必须明确启用。

Apache的基本模块

Apache 2.2.14 默认被静态编译进httpd的模块:

核心模块

不可或缺
core.c
http_core.c
mod_so.c
prefork.c(Linux上默认是prefork.c)

认证相关模块

mod_authn_file.c
mod_authn_default.c
mod_authz_host.c
mod_authz_groupfile.c
mod_authz_user.c
mod_authz_default.c
mod_auth_basic.c

其它模块

mod_include.c
mod_filter.c
mod_log_config.c
mod_env.c
mod_setenvif.c
mod_version.c
mod_mime.c
mod_status.c
mod_autoindex.c
mod_asis.c
mod_cgi.c
mod_negotiation.c
mod_dir.c
mod_actions.c
mod_userdir.c
mod_alias.c

我的Apache的常用模块

最简单的认证模块组合

[B]mod_auth_basic.c 或
[E]mod_auth_digest.c 加 (2.2.14版时,已经由实验模块转为扩展模块)
[B]mod_authn_file.c 加
[B]mod_authz_user.c
注意!basic加密方式密码是明文传送的,不安全,建议使用digest方式的认证。

保护认证而加载的模块

[B]mod_authn_default.c
[B]mod_authz_default.c
出于安全考虑,强烈建议加载!

最常用的访问控制模块

[B]mod_authz_host.c
提供基与主机名、IP地址以及请求特征的访问控制。

其它常用模块

[B]mod_log_config.c
[B]mod_alias.c
[B]mod_dir.c
[B]mod_mime.c
[B]mod_setenvif.c
[E]mod_rewrite.c
[E]mod_deflate.c
[E]mod_expires.c
[E]mod_headers.c

我常用的编译指令

1. ./configure --prefix=/opt/httpd-2.2.14 --with-mpm=prefork --enable-mods-shared=all LDFLAGS='-s'

2. ./configure --prefix=/opt/httpd-2.2.14 --with-mpm=prefork --enable-mods-shared=all --enable-cache --enable-mem-cache --enable-ssl --enable-file-cache --enable-disk-cache --enable-proxy --enable-proxy-http LDFLAGS='-s'
3. ./configure --prefix=/opt/httpd-2.2.14 --with-mpm=prefork --enable-mods-shared=all --enable-dir=static --enable-authz-host=static --enable-auth-basic=static --enable-authn_file=static --enable-authz_user=static --enable-authn-default=static --enable-authz-default=static --enable-setenvif=static --enable-alias=static --enable-log-config=static --enable-deflate=static --enable-rewrite=static --enable-mime=static --enable-expires=static --enable-cache=static --enable-mem-cache=static --enable-headers=shared --enable-disk-cache=shared --enable-file-cache=shared LDFLAGS='-s'

注意:因为Apache的./configure生成的Makefile不支持make install-strip,所以使用 LDFLAGES=’-s’ 来编译被strip的代码.

参考:
http://httpd.apache.org/docs/2.2
http://httpd.apache.org/docs/2.2/install.html
http://httpd.apache.org/docs/2.2/dso.html
http://httpd.apache.org/docs/2.2/mod/
http://httpd.apache.org/docs/2.2/mod/module-dict.html
http://httpd.apache.org/docs/2.2/mod/quickreference.html
http://apache.jz123.cn/ 
---------------------------------------
 mod_authn_default 和 mod_authz_default

mod_authn_default
该模块是一个失败补救(fallback)模块,它在未正确配置认证模块(比如mod_auth_basic缺失mod_authz_user模块的,语法检查检查不出来)的情况下简单拒绝一切认证信息。起到保护数据的作用。
mod_authz_default
在未正确配置授权支持模块的情况下简单拒绝一切授权请求。同样起到保护数据的作用。
假如没有加载default模块,那么,错误的认证配置,会使服务端产生一个500状态码,默认情况下,会生成一个“The server encountered an internal error or misconfiguration and was unable to complete your request.”的提示,有助于管理员调试
假如加载了default模块,那么错误的认证配置,会使客户端始终不能通过认证,最终返回一个401状态码。
出于安全考虑,这两个模块在启用认证的情况下,一定要加载!
--------------------------------------------------------------------

Apache的认证模型

Apache的认证模块分成三个部分:认证类型模块,认证支持模块,认证授权模块。
认证类型模块(auth):
mod_auth_basic
mod_auth_digest
认证支持模块(authn):
mod_authn_alias
mod_authn_anon
mod_authn_file
mod_authn_dbd
mod_authn_dbm
mod_authn_default
mod_authnz_ldap
认证授权模块(authz):
mod_authnz_ldap
mod_authz_dbm
mod_authz_default
mod_authz_user
mod_authz_groupfile
mod_authz_owner
注意:
mod_authnz_ldap模块即包含认证功能也提供授权功能。mod_authn_alias本身并实现认证功能,但是允许其它认证支持模块以更灵活的方式进行配置。
mod_authz_host 模块提供基于主机名、IP地址、请求特征的访问控制,但并不属于认证支持系统。
一般常用的AuthType 是”Basic/Digest”,这个认证需要用到认证模块mod_auth_basic/mod_auth_digest,和认证支持模块mod_authn_file和认证授权模块mod_authz_user。
就是说,最常用的认证组合方式是:
mod_auth_basic/mod_auth_digest
    +
mod_authn_file
    +
mod_authz_user

即是说,一个使用认证以及访问控制(支持Order Allow,Deny ,Allow from all这样的指令)Apache系统,至少要用到下面四或者五个模块:
mod_auth_basic/mod_auth_digest
    +
mod_authn_file
    +
mod_authz_user
    +
mod_authz_host
不太熟悉Apache认证的管理员,为了优化Apache占用的内存,往往会注释掉一些用不到的模块,而又因为不明白认证模块之间的关系,而导致认 证总是不能成功,而apache(目前我使用的版本:2.2.3)的语法检查检查不出来mod_authz_user模块的缺失,故常常使管理员困惑。
重要提醒
Basic认证类型不加密来自用户浏览器的密码,因此不应该用于保护敏感数据。对于敏感的数据,应使用Digest方式来进行认证(比较老的浏览器不支持,不过时至今日,应该已经不是问题了).
参考: http://httpd.apache.org/docs/2.2/howto/auth.html 
------------------------------------------------------------------------
 快速了解Apache环境

当前使用的Apache的版本为2.2.3,支持以下使用方式:
apachectl -v
apachectl -V
apachectl -l
apachectl -L
apachectl -S
apachectl -M

apachectl -V
1. 查看Server version。
2. 查看编译时间。
3. APR 及 APR-Util 的版本。
4. 硬件架构/平台(64-bit/32-bit)
5. 查看 Server MPM(Prefort/Worker)
6. 是否支持线程(threaded)
7. 定位 HTTPD_ROOT
8. 定位配置文件
9. ……
apachectl -l
静态编译进httpd程序的模块。比如:
Compiled in modules:
core.c
prefork.c
http_core.c
mod_so.c
这个命令也可以用来查看 Server MPM,一般情况下,要么是prefork.c,要么是worker.c。
apachectl -L
查看目前Apache支持的指令,其中不包括加载模块后模块支持的指令。
apachectl -L | grep ^[A-Z]
apachectl -S / apachectl -t -D DUMP_VHOSTS
查看Apache下配置的虚拟主机情况。注间这里的“default server”是有特殊作用的。有时候我们需要专门统过调整Vhost配置段的位置或者vhosts配置文件被加载的顺序来调整、设立“default server”。
apachectl -M / apachectl -t -D DUMP_MODULES
查看apache目前配置里加载的模块.