CentOS 源码编译安装Apache 2.4

安装约定

apache源码路径:/usr/local/src
apache安装路径:/usr/local/apache
apache配置文件路径:/usr/local/apache/conf/httpd.conf
apache虚拟主机路径:/usr/local/httpd/conf/vhosts

下载地址

http://httpd.apache.org/
http://apr.apache.org/

下载源代码包

# cd /usr/local/src/
# wget http://mirrors.cnnic.cn/apache//apr/apr-1.4.8.tar.gz
# wget http://mirror.bit.edu.cn/apache//apr/apr-util-1.5.2.tar.gz
# wget http://apache.fayea.com/apache-mirror//httpd/httpd-2.4.6.tar.gz

安装gcc、make等

# yum -y install gcc gcc-c++ make autoconf automake

安装编译apache所需的库

# yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel

添加apache用户和组

# groupadd -g 48 apache
# useradd -u 48 -g apache -c "Apache" -d /var/www -s /sbin/nologin apache

安装apr

# tar zxvf apr-1.4.8.tar.gz
# cd apr-1.4.8
# ./configure --prefix=/usr/local/apr
# make
# make install

安装apr-util

# cd ..
# tar zxvf apr-util-1.5.2.tar.gz
# cd apr-util-1.5.2
# ./configure --prefix=/usr/local/apr-util \
--with-apr=/usr/local/apr
# make
# make install

安装apache

# cd ..
# tar zxvf httpd-2.4.6.tar.gz
# cd httpd-2.4.6
# ./configure --prefix=/usr/local/apache \
--enable-so \
--enable-deflate \
--enable-expires \
--enable-headers \
--enable-ssl \
--enable-rewrite \
-with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util \
--with-mpm=prefork
# make
# make install

配置apache启动脚本

方法一:

# cp -f build/rpm/httpd.init /etc/init.d/httpd
# lm -fs /usr/local/apache /etc/httpd
# ln -fs /usr/local/apache/bin/httpd /usr/sbin/httpd
# ln -fs /usr/local/apache/bin/apachectl /usr/sbin/apachectl

方法二:

将如下代码复制粘贴到/etc/init.d/httpd

#!/bin/bash
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# httpd        Startup script for the Apache Web Server
#
# chkconfig: - 85 15
# description: The Apache HTTP Server is an efficient and extensible  \
#             server implementing the current HTTP standards.
# processname: httpd
# pidfile: /var/run/httpd.pid
# config: /etc/sysconfig/httpd
#
### 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

# What were we called? Multiple instances of the same daemon can be
# created by creating suitably named symlinks to this startup script
prog=$(basename $0 | sed -e 's/^[SK][0-9][0-9]//')

if [ -f /etc/sysconfig/${prog} ]; then
        . /etc/sysconfig/${prog}
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.

httpd=${HTTPD-/usr/local/apache/bin/httpd}
pidfile=${PIDFILE-/usr/local/apache/logs//${prog}.pid}
lockfile=${LOCKFILE-/var/lock/subsys/${prog}}
RETVAL=0

# check for 1.3 configuration
check13 () {
 CONFFILE=/usr/local/apache/conf/httpd.conf
 GONE="(ServerType|BindAddress|Port|AddModule|ClearModuleList|"
 GONE="${GONE}AgentLog|RefererLog|RefererIgnore|FancyIndexing|"
 GONE="${GONE}AccessConfig|ResourceConfig)"
 if grep -Eiq "^[[:space:]]*($GONE)" $CONFFILE; then
  echo
  echo 1>&2 " Apache 1.3 configuration directives found"
  echo 1>&2 " please read @docdir@/migration.html"
  failure "Apache 1.3 config directives test"
  echo
  exit 1
 fi
}

# 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: "
        check13 || exit 1
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}
stop() {
 echo -n $"Stopping $prog: "
 killproc -p ${pidfile} -d 10 $httpd
 RETVAL=$?
 echo
 [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
 echo -n $"Reloading $prog: "
 check13 || exit 1
 killproc -p ${pidfile} $httpd -HUP
 RETVAL=$?
 echo
}

# See how we were called.
case "$1" in
  start)
 start
 ;;
  stop)
 stop
 ;;
  status)
        if ! test -f ${pidfile}; then
            echo $prog is stopped
            RETVAL=3
        else  
            status -p ${pidfile} $httpd
            RETVAL=$?
        fi
        ;;
  restart)
 stop
 start
 ;;
  condrestart)
 if test -f ${pidfile} && status -p ${pidfile} $httpd >&/dev/null; then
  stop
  start
 fi
 ;;
  reload)
        reload
 ;;
  configtest)
        LANG=$HTTPD_LANG $httpd $OPTIONS -t
        RETVAL=$?
        ;;
  graceful)
        echo -n $"Gracefully restarting $prog: "
        LANG=$HTTPD_LANG $httpd $OPTIONS -k $@
        RETVAL=$?
        echo
        ;;
  *)
 echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|graceful|help|configtest}"
 exit 1
esac

exit $RETVAL

启动apache

# chmod +x /etc/init.d/httpd
# service httpd start

配置apache开机启动

# chkconfig httpd on

设置apache环境变量

# vim /etc/profile

在其文件末尾添加如下变量

export PATH=$PATH:/usr/local/apache/bin

或者用以下命令添加

# sed -i '/unset -f pathmunge/a\export PATH=$PATH:/usr/local/apache/bin' /etc/profile

运行如下命令使环境变量生效

# source /etc/profile

日志轮转配置

# ln -fs /usr/local/apache/logs /var/log/httpd

将如下内容粘贴到httpd中

/var/log/httpd/*log {
    missingok
    notifempty
    sharedscripts
    delaycompress
    postrotate
        /sbin/service httpd reload > /dev/null 2>/dev/null || true
    endscript
}

然后将该文件移动到/etc/logrotate.d目录下

重启crond定时计划任务

# service crond restart

这样子apache错误日志和访问日志就可以每天自动轮转。