实验环境:VMware Workstation Pro 14(试用版)
系统平台:
CentOS release 6.9 (Final)             内核  2.6.32-696.el6.x86_64

1.去官网下载源码包

http://httpd.apache.org

http://mirrors.shu.edu.cn/apache//httpd/httpd-2.4.29.tar.bz2
http://mirror.bit.edu.cn/apache//apr/apr-1.6.3.tar.bz2
http://mirror.bit.edu.cn/apache//apr/apr-util-1.6.1.tar.bz2
http://mirror.bit.edu.cn/apache//apr/apr-iconv-1.2.2.tar.bz2

2.解压包

# cd /app/sdb/httpd
# tar xvf httpd-2.4.29.tar.bz2
# tar xvf apr-1.6.3.tar.bz2 -C httpd-2.4.29/srclib/ && mv httpd-2.4.29/srclib/apr-1.6.3 httpd-2.4.29/srclib/apr
# tar xvf apr-util-1.6.1.tar.bz2 -C httpd-2.4.29/srclib/ && mv httpd-2.4.29/srclib/apr-util-1.6.1 httpd-2.4.29/srclib/apr-util
# tar xvf apr-iconv-1.2.2.tar.bz2 -C httpd-2.4.29/srclib/ && mv httpd-2.4.29/srclib/apr-iconv-1.2.2 httpd-2.4.29/srclib/apr-iconv
把apr相关的源码必须解压至httpd源码目录中的srclib目录,在编译httpd的时候,指定相关源码位置。确保目录名称没有版本号

3. 安装依赖包

# yum install gcc gcc++ pcre-devel expat-devel openssl-devel

4. 编译安装httpd

参数:
--no-create
该configure脚本运行正常,但不会创建输出文件。这在生成用于编译的makefile前检查测试结果很有用。

编译参数

# cd httpd-2.4.29

./configure --prefix=/usr/local/httpd2.4.29 \
--sysconfdir=/etc/httpd2.4 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-cgid \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-included-apr \
--with-apr-iconv=srclib/apr-iconv \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork \
--enable-include

编译安装

# make -j 8 && make install

5.添加环境变量

#ln -s httpd2.4.29/ httpd
PATH=/usr/local/cmake/bin:/usr/local/httpd/bin:$PATH

#httpd -v
Server version: Apache/2.4.29 (Unix)

6.创建用于启动httpd的账号和组

# getent group apache > /dev/null || groupadd -g 48 -r apache
# getent passwd apache > /dev/null || useradd -r -u 48 -g apache -s /sbin/nologin apache

7.修改apache目录所有者和组

#chown -R apache:apache /usr/local/httpd2.4.29/

8.修改启动httpd服务的用户和组

# vim /etc/httpd2.4/httpd.conf
User apache
Group apache

9.创建用于service服务的脚本

直接提取一个光盘内的rpm包中的启动脚本吧(或者网上某些源找到的)
rpm2cpio的输出结果会以./开头

# rpm2cpio /misc/cd/Packages/httpd-2.2.15-59.el6.centos.x86_64.rpm |cpio -id ./etc/rc.d/init.d/httpd

修改以下几项为编译后的目录,没有目录自行创建

#mkdir /var/run/httpd

apachectl=/usr/local/httpd/bin/apachectl
httpd=${HTTPD-/usr/local/httpd/bin/httpd}
prog=httpd

10.复制脚本至rc.d并添加启动服务

# cp etc/rc.d/init.d/httpd /etc/rc.d/init.d/
# chkconfig --add httpd
# chkconfig httpd on
# service httpd start
Starting httpd: httpd (pid 93324) already running
                                                           [  OK  ]

11.测试

#curl 192.168.5.128

It works!

#ps aux|grep httpd apache 93325 0.0 0.0 76464 2124 ? S 11:36 0:00 /usr/local/httpd2.4.29/bin/httpd -k start

关于配置文件

默认编译下,除了主配置文件之外,配置文件存放在 指定的配置文件路径中extra目录,这个需要注意了,

比如要启用虚拟主机,则先在主配置文件内把相关功能启用

# Virtual hosts
#Include /etc/httpd2.4/extra/httpd-vhosts.conf