CentOS 6编译安装Apache 2.4

来源:互联网 发布:php require__DIR__ 编辑:程序博客网 时间:2024/05/16 19:45

apache 2.4增加如下新特性和新模块
新特性:
(1) MPM支持运行DSO机制;以模块形式按需加载;
(2) 支持event MPM;
(3) 支持异步读写;
(4) 支持每模块及每个目录分别使用各自的日志级别;
(5) 每请求配置;
(6) 增强版的表达式分析器;
(7) 支持毫秒级的keepalive timeout;
(8) 基于FQDN的虚拟主机不再需要NameVirtualHost指令;
(9) 支持用户自定义变量;
新模块:
(1) mod_proxy_fcgi
(2) mod_ratelimit
(3) mod_remoteip
修改了一些配置机制:
不再支持使用Order, Deny, Allow来做基于IP的访问控制;

一 部署环境
1.系统版本和内核

[root@localhost ~]# cat /etc/centos-release   CentOS release 6.9 (Final)  [root@localhost ~]# uname -r  2.6.32-696.el6.x86_64

2.编译安装apache 2.4时需要包组 “Development tools” 和”Server Platform Development”,用yum安装即可。编译安装apache 2.4时依赖于较高版本的apr,apr-util和pcre,这三个软件包可编译安装,而编译安装apr-util时需要expat开发库,用yum安装即可

[root@localhost ~]# yum groupinstall "Development tools" "Server Platform Development" -y[root@localhost ~]# yum install expat-devel -y

二 安装apache 2.4所依赖的软件包
apache 2.4依赖于更高版本的apr,apr-util和pcre,安装apache 2.4之前先编译安装这三个软件包
1.编译安装apr-1.6.2.tar.gz

[root@localhost ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.6.2.tar.gz[root@localhost ~]# tar xf apr-1.6.2.tar.gz -C /usr/local/src/[root@localhost ~]# cd /usr/local/src/apr-1.6.2/[root@localhost apr-1.6.2]# ./configure --prefix=/usr/local/apr[root@localhost apr-1.6.2]# make && make install

2.编译安装apr-util-1.6.0.tar.gz

[root@localhost ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.0.tar.gz[root@localhost ~]# tar xf apr-util-1.6.0.tar.gz -C /usr/local/src/[root@localhost ~]# cd /usr/local/src/apr-util-1.6.0/[root@localhost apr-util-1.6.0]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr[root@localhost apr-util-1.6.0]# make && make install

3.编译安装pcre-8.41.tar.gz

[root@localhost ~]# wget https://ftp.pcre.org/pub/pcre/pcre-8.41.tar.gz[root@localhost ~]# tar xf pcre-8.41.tar.gz -C /usr/local/src/[root@localhost ~]# cd /usr/local/src/pcre-8.41/[root@localhost pcre-8.41]# ./configure --prefix=/usr/local/pcre[root@localhost pcre-8.41]# make && make install

三 编译安装apache 2.4

[root@localhost ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.28.tar.gz[root@localhost ~]# tar xf httpd-2.4.28.tar.gz -C /usr/local/src/

把apr和apr-util的解压文件复制一份至httpd-2.4.28/srclib目录下,并去除版本号
否则编译时会出现如下错误
configure: error: Bundled APR requested but not found at ./srclib/. Download and unpack the corresponding apr and apr-util packages to ./srclib/.

[root@localhost ~]# cp -rf /usr/local/src/apr-1.6.2 /usr/local/src/httpd-2.4.28/srclib/apr[root@localhost ~]# cp -rf /usr/local/src/apr-util-1.6.0 /usr/local/src/httpd-2.4.28/srclib/apr-util
[root@localhost ~]# cd /usr/local/src/httpd-2.4.28/[root@localhost httpd-2.4.28]# ./configure --prefix=/usr/local/apache --sysconf=/etc/httpd \> --with-included-apr --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre --with-zlib\> --enable-so --enable-ssl --enable-cgi --enable-rewrite --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork

其中,上述选项的含义如下:
–prefix=/usr/local/apache 安装路径
–sysconf=/etc/httpd 配置文件路径
–with-included-apr 包含apr
–with-apr=/usr/local/apr 指定apr的安装路径
–with-apr-util=/usr/local/apr-util 指定apr-util的安装路径
–with-pcre=/usr/local/pcre 指定pcre的安装路径
–with-zlib 支持数据包压缩
–enable-so 允许运行时加载DSO模块
–enable-ssl 编译ssl模块
–enable-cgi 允许使用cgi脚本
–enable-rewrite 支持URL重写机制
–enable-modules=most 启用大多数常用的模块
–enable-mpms-shared=all 启用MPM所有支持的模式
–with-mpm=prefork 默认使用prefork模式

[root@localhost httpd-2.4.28]# make && make install

四 安装后的配置
1.导出二进制程序目录至PATH环境变量中

[root@localhost ~]# vim /etc/profile.d/apache.sh  export PATH=/usr/local/apache/bin:$PATH  [root@localhost ~]# source /etc/profile.d/apache.sh  [root@localhost ~]# echo $PATH  /usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin 

2.导出库文件路径

[root@localhost ~]# vim /etc/ld.so.conf.d/apache.conf  /usr/local/apache/lib  [root@localhost ~]# ldconfig 

3.导出头文件,可以基于链接的方式

[root@localhost ~]# ln -sv /usr/local/apache/include /usr/include/apache`/usr/include/apache' -> `/usr/local/apache/include'

4.导出帮助手册
编辑man.config文件,找到 MANPATH /usr/X11R6/man 这一行,在下面添加如下一行

[root@localhost ~]# vim /etc/man.config   MANPATH /usr/local/apache/man  

5.启动apache服务并测试

[root@localhost ~]# apachectl start  [root@localhost ~]# ss -tunl | grep 80  tcp    LISTEN    0    128        :::80        :::*   [root@localhost ~]# vim /etc/sysconfig/iptables  -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT  [root@localhost ~]# service iptables restart

打开浏览器,在浏览器输入centos主机的ip地址,网页上有如下显示

It works!

则表示apache服务已经正常启动
注:启动时会出现“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

[root@localhost ~]# vim /etc/httpd/httpd.conf  ServerName localhost:80  [root@localhost ~]# apachectl restart  

6.创建apache启动脚本
在/etc/init.d/目录下创建httpd文件,编辑httpd文件,内容如下

[root@localhost ~]# apachectl stop  [root@localhost ~]# vim /etc/init.d/httpd 
#!/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/apache/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/functionsif [ -f /etc/httpd ]; then        . /etc/httpdfi# 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/apache/bin/apachectlhttpd=/usr/local/apache/bin/httpdprog=httpdpidfile=${PIDFILE-/usr/local/apache/logs/httpd.pid}lockfile=${LOCKFILE-/var/lock/subsys/httpd}RETVAL=0STOP_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=2esacexit $RETVAL

给httpd文件添加执行权限

[root@localhost ~]# chmod +x /etc/init.d/httpd 

然后就可以用service 命令来启动和关闭httpd了

[root@localhost ~]# service httpd start  [root@localhost ~]# service httpd restart[root@localhost ~]# service httpd status[root@localhost ~]# service httpd reload[root@localhost ~]# service httpd stop  

7.加入开机自启动

[root@localhost ~]# chkconfig --add httpd  [root@localhost ~]# chkconfig httpd on  [root@localhost ~]# chkconfig --list httpd  httpd    0:off  1:off  2:on  3:on  4:on  5:on  6:off