一. 预备环境:
# 编译安装apache之前需要确认:apr(apache虚拟机),apr-util,apr-util-ldap的版本, 不同的版本所依赖的apr版本不同.
# 例:httpd-2.4.*需要依赖于apr-1.4.*以上的版本
# 如需下载相应的版本: http://apr.apache.org/
# 查看apr的版本
[root@caiya ~]# rpm -qa apr*
apr-1.3.9-5.el6_2.i686
apr-util-ldap-1.3.9-3.el6_0.1.i686
apr-util-1.3.9-3.el6_0.1.i686
[root@caiya ~]#
A. 安装组件
# 安装apr之前需要确认开发组件和开发库"Development tools" 和 "Development Libraries"是否已经安装.
# centos 5.X 的版本需要安装
[root@caiya software]# yum groupinstall "Development Libraries"
[root@caiya software]# yum groupinstall "Development tools"
# centos 6.X 的版本需要安装
[root@caiya software]# yum groupinstall "Server Platform Development"
[root@caiya software]# yum groupinstall "Development tools"
B. 安装apr, apr-util
# 下载apr
[root@caiya software]# wget http://mirrors.hust.edu.cn/apache//apr/apr-1.5.2.tar.gz
[root@caiya software]# tar zxvf apr-1.5.2.tar.gz
# 如果无法解压缩, 同步一下软件时间
[root@caiya software]# hwclock -s
# 安装apr
[root@caiya apr-1.5.2]# cd apr-1.5.2
[root@caiya apr-1.5.2]# ./configure --prefix=/usr/local/apr-1.5.2
[root@caiya apr-1.5.2]# make
[root@caiya apr-1.5.2]# make install
# 下载apr-util
[root@caiya software]# wget http://mirrors.hust.edu.cn/apache//apr/apr-util-1.5.4.tar.gz
[root@caiya software]# tar zxvf apr-util-1.5.4.tar.gz
[root@caiya software]# cd apr-util-1.5.4
# 需要注意指定apr的安装路径
[root@caiya apr-util-1.5.4]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr-1.5.2
[root@caiya apr-util-1.5.4]# make
[root@caiya apr-util-1.5.4]# make install
二. 至此环境已经安装完成, 可以安装apache(httpd)
# 下载httpd安装包(就近选择的下载地址, 搜狐镜像站点)
[root@caiya software]# wget http://mirrors.sohu.com/apache/httpd-2.4.17.tar.gz
[root@caiya software]# tar zxvf httpd-2.4.17.tar.gz
[root@caiya software]# cd httpd-2.4.17
[root@caiya httpd-2.4.17]# ./configure \
--prefix=/usr/local/apache-2.4 \
--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-1.5.2 \
--with-apr-util=/usr/local/apr-util \
# 常见报错一:
checking for pcre-config... false
configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/
# 解决办法
[root@caiya httpd-2.4.17]# yum install pcre-devel -y
# 常见报错二:
checking whether to enable mod_ssl...
configure: error: mod_ssl has been requested but can not be built due to prerequisite failures
# 解决办法
[root@caiya httpd-2.4.17]# yum install openssl-devel -y
# 配置通过后:
[root@caiya httpd-2.4.17]# make
[root@caiya httpd-2.4.17]# make install
# 注意: httpd受selinux控制, 如果selinux为运行状态, 有可能导致httpd无法启动
# 此时为关闭状态, 如果不是关闭状态, 修改selinux配置文件 vim /etc/selinux/config 修改为: SELINUX=disabled
[root@caiya local]# getenforce
Disabled
[root@caiya local]#
# 至此apache已经安装完毕
三. 配置apache
# 启动apache
# 编译安装的启动脚本在: /usr/local/apache-2.4/bin/apachectl
[root@caiya bin]# /usr/local/apache-2.4/bin/apachectl start
# 如果报如下错误:
AH00557: httpd: apr_sockaddr_info_get() failed for caiya.localdomain
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1.
Set the 'ServerName' directive globally to suppress this message
# 解决办法:
[root@caiya httpd]# vim /etc/httpd/httpd.conf
找到"#ServerName www.example.com:8" 去掉前面的注释
# 检测是否正常启动(当前为正常状态)
[root@caiya bin]# /usr/local/apache-2.4/bin/apachectl start
[root@caiya bin]# netstat -tnlp | grep 80
5:tcp 0 0 :::80 :::* LISTEN 41862/httpd
[root@caiya bin]# ps -ef | grep httpd
101:root 41862 1 0 19:51 ? 00:00:00 /usr/local/apache-2.4/bin/httpd -k start
102:daemon 41863 41862 0 19:51 ? 00:00:00 /usr/local/apache-2.4/bin/httpd -k start
103:daemon 41864 41862 0 19:51 ? 00:00:00 /usr/local/apache-2.4/bin/httpd -k start
104:daemon 41865 41862 0 19:51 ? 00:00:00 /usr/local/apache-2.4/bin/httpd -k start
106:root 41952 1738 0 19:51 pts/1 00:00:00 grep -n --color httpd
[root@caiya bin]#
四. httpd-2.4.x 的访问策略
# 单条策略
require ip ipaddre
require ip hostname
require ip username
require not ip ipaddre
require not ip hostname
require not ip username
# 允许所有主机访问:
require all granted
# 拒绝所有主机访问:
require all deny
五. 制作启动脚本
# 编译安装的httpd-2.4.x的启动脚本路径在编译目录: /httpd-2.4.17/build/rpm/httpd.init
# 当前环境的启动脚本路径在: /software/apache/httpd-2.4.17/build/rpm/httpd.init
# 复制 httpd.init 至 /etc/init.d/httpd
[root@caiya rpm]# cp /software/apache/httpd-2.4.17/build/rpm/httpd.init /etc/init.d/httpd
# 修改httpd脚本文件
[root@caiya rpm]# vim /etc/init.d/httpd
# 修改之前(每修改一项先做备份)
60 httpd=${HTTPD-/usr/sbin/httpd}
61 pidfile=${PIDFILE-/var/run/${prog}.pid}
62 lockfile=${LOCKFILE-/var/lock/subsys/${prog}}
63 RETVAL=0
64
65 # check for 1.3 configuration
66 check13 () {
67 CONFFILE=/etc/httpd/conf/httpd.conf
68 GONE="(ServerType|BindAddress|Port|AddModule|ClearModuleList|"
69 GONE="${GONE}AgentLog|RefererLog|RefererIgnore|FancyIndexing|"
70 GONE="${GONE}AccessConfig|ResourceConfig)"
71 if grep -Eiq "^[[:space:]]*($GONE)" $CONFFILE; then
72 echo
73 echo 1>&2 " Apache 1.3 configuration directives found"
74 echo 1>&2 " please read @docdir@/migration.html"
75 failure "Apache 1.3 config directives test"
76 echo
77 exit 1
78 fi
79 }
# 修改之后(红色为修改部分)
60 # httpd=${HTTPD-/usr/sbin/httpd}
61 httpd=${HTTPD-/usr/local/apache-2.4/bin/httpd}
62 # pidfile=${PIDFILE-/var/run/${prog}.pid}
63 pidfile=${PIDFILE-/usr/local/apache-2.4/logs/httpd.pid}
64 lockfile=${LOCKFILE-/var/lock/subsys/${prog}}
65 RETVAL=0
66
67 # check for 1.3 configuration
68 check13 () {
69 # CONFFILE=/etc/httpd/conf/httpd.conf
70 CONFFILE=/etc/httpd/httpd.conf
71 GONE="(ServerType|BindAddress|Port|AddModule|ClearModuleList|"
72 GONE="${GONE}AgentLog|RefererLog|RefererIgnore|FancyIndexing|"
73 GONE="${GONE}AccessConfig|ResourceConfig)"
74 if grep -Eiq "^[[:space:]]*($GONE)" $CONFFILE; then
75 echo
76 echo 1>&2 " Apache 1.3 configuration directives found"
77 echo 1>&2 " please read @docdir@/migration.html"
78 failure "Apache 1.3 config directives test"
79 echo
80 exit 1
81 fi
82 }
# 修改完成以后, 即可使用 service httpd {start|stop|status...}
[root@caiya /]# service httpd start
正在启动 httpd : [确定]
[root@caiya /]# service httpd status
httpd (pid 3864) 正在运行...
[root@caiya /]# service httpd stop
停止 httpd : [确定]
[root@caiya /]#
六. 添加自动
[root@caiya /]# chkconfig --add httpd
[root@caiya /]# chkconfig --list | grep httpd
8:httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@caiya /]# chkconfig httpd on
[root@caiya /]# chkconfig --list | grep httpd
8:httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@caiya /]#
==================== 扩展部分 ====================
# httpd的模块解释
选项解释:
# 指定安装路径
# --prefix=/usr/local/apache
# 指定配置文件的路径
# --sysconfdir=/etc/httpd
# 装载核心模块,支持动态共享模块,以模块的方式加载PHP, 如果不开启, 无法使用PHP
# --enable-so
# 支持URL重写
# --enable-rewrite
# 支持ssl的功能, 否则无法使用https
# --enable-ssl
# 启动压缩机会, 将文本信息压缩后回复给客户端, 浏览器自动解压, 很常用的一种压缩机制
# --enable-deflate
# 配置PHP的时候让PHP以FastCGI的模式工作, 必须要开启此项
# --enable-proxy-fcgi
转载于:https://blog.51cto.com/caiya/1911337