CentOS 6编译安装Apache 2.4

时间:2017-10-10 01:55来源:blog.csdn.net 作者:独孤柯灵的博客 举报 点击:

httpd-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.安装httpd需要的程序包组为 “Development tools” 和”Server Platform Development”,用yum安装即可

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

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

[root@localhost ~]# wget http://mirrors.shuosc.org/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 http://mirrors.shuosc.org/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

三 编译安装apache2.4  先创建apache用户和组

[root@localhost ~]# groupadd -r apache
[root@localhost ~]# useradd -r -g apache apache

编译安装apache2.4

[root@localhost ~]# wget http://mirrors.shuosc.org/apache/httpd/httpd-2.4.28.tar.gz
[root@localhost ~]# tar xf httpd-2.4.28.tar.gz -C /usr/local/src/
[root@localhost ~]# cd /usr/local/src/httpd-2.4.28/
[root@localhost httpd-2.4.28]# ./configure --prefix=/usr/local/apache2 --sysconf=/etc/httpd24 \
> --with-included-apr --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre \
> --enable-so --enable-ssl --enable-cgi --enable-rewrite --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork

其中,上述选项的含义如下:  –prefix=/usr/local/apache2 安装路径  –sysconf=/etc/httpd24 配置文件路径  –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的安装路径  –enable-so 允许运行时加载DSO模块  –enable-ssl 如果不加载将无法使用使用https  –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

注:如果编译是出现如下错误  configure: error: Bundled APR requested but not found at ./srclib/. Download and unpack the corresponding apr and apr-util packages to ./srclib/.  只需要将apr和apr-util源码下载,解压放到httpd-2.4.28/srclib里面,并去除版本号

[root@localhost httpd-2.4.28]# cp -rf /usr/local/src/apr-1.6.2 /usr/local/src/httpd-2.4.28/srclib/apr
[root@localhost httpd-2.4.28]# cp -rf /usr/local/src/apr-util-1.6.0 /usr/local/src/httpd-2.4.28/srclib/apr-util

然后重新执行即可

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

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

2.导出库文件路径

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

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

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

4.导出帮助手册

[root@localhost ~]# vim /etc/man.config   
MANPATH /usr/local/apache2/man  
[root@localhost ~]# man -M /usr/local/apache2/man httpd

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”这样的报错,  解决办法如下

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

重启apache服务后就没这个报错了  6.创建apache启动脚本

[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/conf/httpd.conf  
# config: /etc/sysconfig/httpd  
# pidfile: /var/run/httpd/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/sysconfig/httpd ]; then  
        . /etc/sysconfig/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=""  
# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server  
# with the thread-based "worker" MPM; BE WARNED that some modules may not  
# work correctly with a thread-based MPM; notably PHP will refuse to start.  
# Path to the apachectl script, server binary, and short-form for messages.  
apachectl=/usr/local/apache2/bin/apachectl  
httpd=httpd=${HTTPD-/usr/local/apache2/bin/httpd}  
prog=httpd  
pidfile=${PIDFILE-/var/run/httpd/httpd.pid}  
lockfile=${LOCKFILE-/var/lock/subsys/httpd}  
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  

给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 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
------分隔线----------------------------
发表评论
为了和诣的生活,我关闭了评论页面,请大家到QQ群里交流吧:348944156,也欢迎关注本站微信公众号:centoscn
推荐内容