32121111 发表于 2016-11-30 09:13:00

LAMP-apache编译安装

apache编译安装笔记
系统环境:Centos6.664位
apache版本2.4.23
下载apache文件http://httpd.apache.org/download.cgi#apache24

一、准备工作:
先安装依赖组件pcre和openssl-devel
yum -y install pcre-devel
yum -y install openssl-devel

下载、安装apr和apr-util组件
apr下载地址:http://apr.apache.org/download.cgi
1.安装apr
tar xf apr-1.5.2.tar.bz2
cdapr-1.5.2   
./configure --prefix=/usr/local/apr   #指定安装路径为/usr/local/apr
make
make install         

2.安装apr-util
tar xf apr-util-1.5.4.tar.bz2
cd apr-util-1.5.4
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make
make install
#指定安装路径:--prefix=/usr/local/apr-util
#告知软件所依赖的apr的安装路径:--with-apr=/usr/local/apr


二、安装httpd
#tar xf httpd-2.4.23.tar.bz2
#cd httpd-2.4.23
#./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd--enable-so --enable-rewirte --enable-ssl --enable-cgi --enable-cgid --enable-modules=most --enable-mods-shared=most --enable-mpms-shared=all --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util--with-mpm=event
#make
#make install
安装完成!

# 各编译参数详解
--prefix:#安装路径
--sysconfdir:#指定配置文件路径
--enable-so:#DSO兼容,DSO=Dynamic Shared Object,动态共享对象,可实现模块动态生效
--enable-ssl:#支持SSL/TLS,可以实现https访问
--enable-cgi:#支持CGI脚本(默认对非线程的MPM模式开启)
--enable-rewrite:#启用Rewrite功能
--enable-deflate:#支持压缩
--with-z:#使用指定的zlib库,不指定路径会自动寻找
--with-pcre:#使用指定的PCRE库,不指定路径会自动寻找
--with-apr:#指定apr安装路径
--with-apr-util:#指定apr-util安装路径
--enable-modules:#支持动态启用的模块,可选参数有“all”,“most”,“few”,“reallyall”
--enable-mpms-shared:#支持动态加载的MPM模块,可选“all”
--with-mpm:#设置默认启用的MPM模式
httpd的MPM有:prefork,worker,event
httpd2.4编译安装的默认MPM为:event


三、启用之前需要配置:
启动httpd之前必须先关闭:selinux
使用:getenforce 查看状态
Enforcing
使用:setenforce 0 关闭
保持状态为Permissive即可:# getenforce
Permissive
使用vim /etc/selinux/config
将SELINUX=permissive

添加主机名称:
在配置文件:/etc/httpd/httpd.conf 里面添加
            ServerName localhost:80
在/usr/local/apache/目录下,使用bin/apachectl start启动
关闭防火墙:service iptables stort即可访问。


更改pid文件目录:
修改vim /etc/httpd/httpd.conf
添加:#PidFile "/var/run/httpd.pid"   先用#注释这条命令
使用root@localhost apache]# bin/apachectl stop   #关闭apachectl
使用:# netstat -tnlp   #验证80端口是否关闭
成功关闭之后进入vim /etc/httpd/httpd.conf把之前注释的命令启用
查看ls /var/run/ 文件夹是否有httpd.pid文件


启动,停止设置:
创建编辑文件:vim /etc/init.d/httpd
写入内容:
#/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   #判断该目录下如有httpd文档(配置文件)
      . /etc/sysconfig/httpd          #读到该目录下
fi

# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}   #如果该变量有值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/apache/bin/apachectl    #定义apachectl程序文件路径
httpd=${HTTPD-/usr/local/apache/bin/httpd}    #定义httpd程序文件路径
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}    #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


给该文件添加执行权限:chmod +x /etc/init.d/httpd
使用命令:service httpd restart即可重启


开机自动启动:
chkconfig --add httpd   #加入服务列表
chkconfig --list httpd   #查看默认状态
chkconfig --httpd on   #更改状态

启用httpd自带命令:
新建文件: vim/etc/profile.d/httpd.sh
写入内容:export PATH=$PATH:/usr/local/apache/bin
使用命令验证:echo $PATH


更改MPM模块:
编辑:vim/etc/httpd/httpd.conf
默认使用的是event模块:LoadModule mpm_event_module modules/mod_mpm_event.so
使用httpd -M   #查看默认使用的模块

打开/etc/httpd/extra/httpd-mpm.conf可配置mpm

页: [1]
查看完整版本: LAMP-apache编译安装