Httpd编译安装教程


写在前面

本文主要说明httpd的编译安装过程。
源码安装是比较麻烦的一种安装方法,但却有其不可替代性。
因为此前在网上查找的其他相关文章或多或少存在一些问题,此处将个人实际成功的步骤记录下来,方便自己与他人的查询。

注:安装步骤中的知识补充放在文末。


操作环境

物理机:Windows 10 Enterprise
虚拟机:CentOS 7 64位


具体操作流程

首先安装开发者套件:

1
2
# yum groups mark convert
# yum groupinstall "Development Tools" -y // 整套安装一组开发者工具

下载源码包:

1
2
3
4
# cd /usr/local/src/
# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.6.5.tar.gz
# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
# wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.37.tar.gz

备注:三个源码包都放在/usr/local/src/


安装apr-1.6.5

1
2
3
4
5
# cd /usr/local/src/
# cp -r apr-1.6.5 /usr/local/src/httpd-2.4.37/srclib/apr
# cd apr-1.6.5
# ./configure --prefix=/usr/local/apr
# make && make install

安装apr-util-1.6.1

1
2
3
4
5
# cd /usr/local/src
# cp -r apr-util-1.6.1 /usr/local/src/httpd-2.4.37/srclib/apr-util
# cd /usr/local/src/apr-util-1.6.1
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
# make && make install

pcre的编译安装

1
2
3
4
5
# wget https://ftp.pcre.org/pub/pcre/pcre-8.00.tar.gz
# tar -zxvf pcre-8.10.tar.gz
# cd pcre-8.10
# ./configure
# make && make install


安装httpd-2.4.37

1
2
# ./configure --with-included-apr --prefix=/usr/local/apache2.4 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-so --enable-mods-shared=most
# make && make install

编写启动脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# vim httpd
#!/bin/bash
# chkconfig: 12345 80 90
function start_http()
{
/usr/local/apache2.4/bin/apachectl start
}
function stop_http()
{
/usr/local/apache2.4/bin/apachectl stop
}
case "$1" in
start)
start_http
;;
stop)
stop_http
;;
restart)
stop_http
start_http
;;
*)
echo "Usage : start | stop | restart"
;;
esac

加入所编写的httpd系统服务:

1
2
# chmod a+x httpd
# cp -arf httpd /etc/init.d/

启动自己编写的服务:

1
2
# systemctl daemon-reload
# systemctl start httpd

设置开机自启动:# chkconfig --add httpd
查看配置文件路径:# /usr/local/apache2.4/conf/httpd.conf
查看httpd服务运行状态# systemctl status httpd


补充知识

参数释义:

–enable-mods // 让apache核心装载DSO,但不实际编译任何动态模块,未加参数,为静态编译
–enable-ModuleName=share // 将模块编译成动态的
–enable-ModuleName=most // 动态地编译进来大多数的模块
–enable-ModuleName=all // 动态地编译所有的模块

动态编译:使用模块时才将需要使用的模块Load进来
静态编译:先将所需模块Load进去,使用时直接调用


install与groupinstall区别:

yum install 安装单个软件
yum grouplist 查看这个软件的所有软件包,每次安装前可以先查看yum grouplist有哪些软件,然后再去安装yum groupinstall
yum groupinstall 看装多个软件,安装这个软件的所有依赖包,yum groupinstall安装的时候必须加双引号

示例代码:

yum -y groupinstall Desktop // -y参数同意所有软件安装操作
yum groupremove // 卸载所有软件包


-prefix选项:

使用./configure --prefix=/supersparrow
安装完成将自动生成目录supersparrow,则该软件任何的文档都被复制到这个目录。指定此安装目录是为了以后的维护方便,否则该软件所需的软件会被复制到不同的系统目录下,难以维护。

用了-prefix选项的另一个好处是卸载软件或移植软件。当某个安装的软件不再需要时,只须简单的删除该安装目录,就能够把软件卸载得干干净净;移植软件只需拷贝整个目录到另外一个机器即可(相同的操作系统)。

您的支持是我前进的动力!