CentOS 6.5 编译安装Apache2.4
一、 httpd 2.4的新特
1) MPM支持运行时装载
--enable-mpms-shared=all --with-mpm=prefork|worker|event
2) 支持event MPM
3) 异步读写支持
4) 支持每模块及每目录分别使用不同的日志级别
5) 支持per-request(即支持<If>, <ElseIf>, and <Else>条件判断)
6) 增强版的表达式分析器;
7) 支持毫秒级keepalive timeout;
8) 基于FQDN(域名)的虚拟主机不再需要NameVirtualHost;
9) 支持用户使用自定义变量;
新增一些模块:mod_proxy_fcgi, mod_ratelimit, mod_request, mod_remoteip
修改了一些配置机制:不再支持使用order, allow, deny来实现基于IP的访问控制;
二、 系统环境
CentOS6.4 x86_64位 采用最小化安装,系统经过了基本优化 1G内存,1核cpu
selinux为关闭状态,iptables关闭状态
三、编译准备
1. 安装相关依赖工具
# yum install -y gcc gcc-c++ wget expat-devel zlib-devel zlib openssl openssl-devel libxml2-devel
2. 下载相关软件包
# wget http://www-us.apache.org/dist//httpd/httpd-2.4.37.tar.gz
# wget -c http://www-us.apache.org/dist//apr/apr-1.6.5.tar.gz
# wget -c http://www-us.apache.org/dist//apr/apr-util-1.6.1.tar.gz
# wget https://ftp.pcre.org/pub/pcre/pcre-8.32.tar.gz
3. 编译安装依赖包
# cd /usr/local/src
安装apr:
# tar xf apr-1.6.5.tar.gz
# cd apr-1.6.5
# ./configure --prefix=/usr/local/apr
# make && make install
安装apr-util:
# tar xf apr-util-1.6.1.tar.gz
# cd apr-util-1.6.1
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
# make && make install
安装pcre:
# tar -xf pcre-8.32.tar.gz
# cd pcre-8.32
# ./configure --prefix=/usr/local/pcre
# make && make install
注意:如果你下载pcre2的话,apache2.4会出现编译不通过,建议不要使用pcre2.
四、编译安装httpd
# tar -xf httpd-2.4.37.tar.gz
# cd httpd-2.4.37
开始配置:
./configure \
--prefix=/usr/local/httpd24 \
--sysconfdir=/etc/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--enable-deflate \
--enable-expires \
--with-pcre=/usr/local/pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util \
--enable-mods-shared=most \
--enable-mpms-shared=all \
--with-mpm=prefork/event/worker注意:如果是复制粘贴配置,要注意每行结尾\之前只能有一个空格,之后不能有空格
参数含义:
./configure \
--prefix=/usr/local/httpd24 \ # 指定安装路径
--sysconfdir=/etc/httpd24 \ # 指定配置文件路径
--enable-so \ # 启用动态模块加载
--enable-ssl \ # 启用ssl加密传输
--enable-cgi \ # 允许使用cgi脚本
--enable-rewrite \ # 启用url重写功能
--with-zlib \ # 支持数据包压缩
--enable-deflate \ # 支持网页压缩
--enable-expires \ # 支持网页缓存
--with-pcre=/usr/local/pcre \ # pcre安装路径
--with-apr=/usr/local/apr \ # apr安装路径
--with-apr-util=/usr/local/apr-util \ # apr-util 安装路径
--enable-mods-shared=most \ # 动态的编译进来大多数的模块
--enable-mpms-shared=all \ # 动态编译MPM所有支持的模式
--with-mpm=prefork/event/worker # 默认加载使用的mpm编译并安装:
# make && make install
五、安装后配置
1. 将apache二进制程序目录加入环境变量PATH
# echo 'export PATH=/usr/local/http24/bin:$PATH' >>/etc/profile
# source /etc/profile
2. 链接方式输出头文件
# ln -sv /usr/local/httpd24/include /usr/include/apache
3. 添加man帮助手册
编辑man.config文件,找到 MANPATH /usr/X11R6/man 这一行,在下面添加如下一行
MANPATH /usr/local/httpd24/man
4.添加启动脚本
a. 系统自带脚本
# cp /usr/local/httpd24/bin/apachectl /etc/init.d/httpd
vi打开,添加两行
# chkconfig: 35 85 15
# description: The Apache HTTP Serverb. 相关启动脚本
#!/bin/bash
#
# httpd Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: The Apache HTTP Server is an efficient and extensible \
# server implementing the current HTTP standards.
# processname: httpd
# config: /etc/httpd/httpd.conf
# pidfile: /usr/local/httpd24/logs/httpd.pid
#
### BEGIN INIT INFO
# Provides: httpd
# Required-Start: $local_fs $remote_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Should-Start: distcache
# Short-Description: start and stop Apache HTTP Server
# Description: The Apache HTTP Server is an extensible server
# implementing the current HTTP standards.
### END INIT INFO
# Source function library.
. /etc/rc.d/init.d/functions
if [ -f /etc/httpd ]; then
. /etc/httpd
fi
# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}
# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""
# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/httpd24/bin/apachectl
httpd=/usr/local/httpd24/bin/httpd
prog=httpd
pidfile=${PIDFILE-/usr/local/httpd24/logs/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd24}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}
# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure. So we just do it the way init scripts
# are expected to behave here.
start() {
echo -n $"Starting $prog: "
LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
# When stopping httpd, a delay (of default 10 second) is required
# before SIGKILLing the httpd parent; this gives enough time for the
# httpd parent to SIGKILL any errant children.
stop() {
status -p ${pidfile} $httpd > /dev/null
if [[ $? = 0 ]]; then
echo -n $"Stopping $prog: "
killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd
else
echo -n $"Stopping $prog: "
success
fi
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
echo -n $"Reloading $prog: "
if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
RETVAL=6
echo $"not reloading due to configuration syntax error"
failure $"not reloading $httpd due to configuration syntax error"
else
# Force LSB behaviour from killproc
LSB=1 killproc -p ${pidfile} $httpd -HUP
RETVAL=$?
if [ $RETVAL -eq 7 ]; then
failure $"httpd shutdown"
fi
fi
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart|try-restart)
if status -p ${pidfile} $httpd >&/dev/null; then
stop
start
fi
;;
force-reload|reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"
RETVAL=2
esac
exit $RETVAL
c. 开机启动httpd服务
# chkconfig --add httpd
# chkconfig httpd on
# chkconfig --list httpd
5. 测试安装
打开浏览器,在浏览器输入centos主机的ip地址,或者本机curl,检测安装是否成功。
# curl localhost
<html><body><h1>It works!</h1></body></html>
六、可能遇到的问题
1. 启动报错
启动时会出现“AH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using localhost.localdomain. Set the ‘ServerName’ directive globally to suppress this message”这样的报错,只需要编辑http.conf文件,找到#ServerName www.example.com:80这一行,在下面添加 如下一行,然后重启apache
# vim /etc/httpd/httpd.conf
ServerName localhost:80
取消以下注释行,开启httpd状态监控
Include /etc/httpd24/extra/httpd-info.conf
2.
参数方面没问题,然后我开始一个个去掉参数:发现问题出现在--enable-cgi \这个上面,故这个有问题。
最后对调参数,参数放在倒数第二个再次环境测试!结果发现通过了!,其实问题出现在\后面有空格,去掉空格再编译
3.
这个提示是openssh版本太低,yum install -y openssl openssl-devel 解决
4.
缺少了xml相关的库,需要安装libxml2-devel包。直接安装并不能解决问题,因为httpd调用的apr-util已经安装好了,但是apr-util并没有libxml2-devel包支持。
安装libxml2-devel包
[root@localhost ~]# yum install -y libxml2-devel
删除apr-util安装目录,并重新编译安装
[root@localhost ~]# rm -rf /usr/local/apr-util
[root@localhost ~]# cd /usr/local/src/apr-util-1.6.1
# 这一步很重要,必须清除之前配置时的缓存
[root@localhost apr-util-1.6.1]# make clean
# 源码安装三步走
[root@localhost apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@localhost apr-util-1.6.1]# make
[root@localhost apr-util-1.6.1]# make install
重新编译安装httpd
# 同样要清理之前的缓存十分重要
[root@localhost src]# cd /usr/local/src/httpd-2.4.28
[root@localhost httpd-2.4.28]# make clean
[root@localhost httpd-2.4.28]# ./configure --prefix=/usr/local/apache2.4 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-so --enable-mods-shared=most
[root@localhost httpd-2.4.28]# make
# 这时make没报错了
[root@localhost httpd-2.4.28]# make install
注:此处参考http://blog.51cto.com/castiel/2051440