编译安装Apache

系统环境:centos 7.2

前提:

提供开发工具及开发环境

  • 开发工具:make, gcc等

  • 开发环境:开发库,头文件

  • glibc:标准库

方式:

通过“包组”提供开发组件

centos 6
[root@centos6 ~]# yum groupinstall "Development Tools"
[root@centos6 ~]# yum groupinstall "Development tools"

centos 7 
[root@centos6 ~]# yum groupinstall "Development Tools"



1、首先在方法网站http://www.apache.org/下载源码包

下载路径:http://apache.fayea.com/httpd/httpd-2.2.31.tar.gz

2、我们将下载好的文件放置在/root目录下

3、我们可以看到源码包的格式为.tar.gz 所有这里我们要将源码包进行解压缩和拆包

[root@centos7 ~]# tar xvzf httpd-2.2.31.tar.gz

4、执行完上述操作后,在当前目录即(/root)目录下,我们可以看到httpd-2.2.31目录,进入该目录

[root@centos7 ~]# cd httpd-2.2.31/

5、在该目录下,有众多的文件,我们首先关注的是 README 文件和 INSTALL 文件

README 文件主要简单的介绍了

[root@centos7 httpd-2.2.31]# less README

what is it? 
The latest version  
documentation   
installation    
licensing
Cryptographic Software Notice
Contacts
Acknowledgments

通过以上的说明可以使我们对Apache有一个简单的了解



INSTALL 文件主要说明了安装的方法

[root@centos7 httpd-2.2.31]# less INSTALL

$ ./configure --prefix=PREFIX
$ make
$ make install
$ PREFIX/bin/apachectl start

6、通过查看以上文件,下面我们可以进行Apache的编译安装

首先我们来介绍下configure脚本的参数说明

[root@centos7 httpd-2.2.31]# ./configure --help


Installation directories:指定默认的安装位置
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [/usr/local/apache2]


Fine tuning of the installation directories:默认文件安装位置
  --bindir=DIR            user executables [EPREFIX/bin]
  --sbindir=DIR           system admin executables [EPREFIX/sbin]
  --sysconfdir=DIR        read-only single-machine data [PREFIX/etc]
  ......
  ......


System types:交叉编译选项
  --build=BUILD     configure for building on BUILD [guessed]
  --host=HOST       cross-compile to build programs to run on HOST [BUILD]
  --target=TARGET   configure for building compilers for TARGET [HOST]


Optional Features:可选特性
  --disable-option-checking  ignore unrecognized --enable/--with options
  --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
  --enable-v4-mapped      Allow IPv6 sockets to handle IPv4 connections
  ......
  ......


Optional Packages:可选包
  --with-PACKAGE[=ARG]    use PACKAGE [ARG=yes]
  --without-PACKAGE       do not use PACKAGE (same as --with-PACKAGE=no)
  --with-included-apr     Use bundled copies of APR/APR-Util

6(1)这里我们指定几个简单的选项即可:

[root@centos7 httpd-2.2.31]# ./configure --prefix=/usr/local/http2 --sysconfdir=/etc/http2

//该步骤执行完毕后,会结合 Makefile.in 文件,在当前目录下生成 Makefile 文件
//出现error等信息,说明配置失败

6(2)执行make和make install命令(上一步执行成功后) 注意:要始终处于当前目录下,不要离开该目录

[root@centos7 httpd-2.2.31]# make
[root@centos7 httpd-2.2.31]# make install

7、启动

[root@centos7 httpd-2.2.31]# cd /usr/local/http2/bin
[root@centos7 bin]# ./apachectl start
[root@centos7 bin]# netstat -tapn | grep "80"
tcp6       0      0 :::80                   :::*                    LISTEN      14611/http

8、测试

centos 6.8系统下测试

[root@centos6 ~]# links 10.1.249.146
//10.1.249.146 为以上编译安装Apache软件的centos 7系统 的IP

ZOq3YW3.png

在Windows系统下测试

任意一款浏览器下输入:http://10.1.249.146/

YOlk46U.png

注意:如果启动成功但是却始终访问不了,则可能的原因是centos 7 的防火墙没有关闭,所以建议关闭防火墙后在做测试

[root@centos7 ~]# iptables -F



9、最后的配置

我们知道一个程序主要由4类文件组成

  • 二进制文件

  • 配置文件

  • 库文件

  • 帮助文档

    [root@centos7 ~]# cd /usr/local/http2/
    [root@centos7 http2]# ls
    bin  build  cgi-bin  error  htdocs  icons  include  lib  logs  man  manual  modules

以上4类文件应该放置在系统特定的目录下 或者通过修改系统相关的配置文件

(1)二进制文件 bin目录 添加其路径到PATH环境变量即可

 [root@centos7 http2]# vim /etc/profile.d/http.sh
 export PATH=/usr/local/http2/bin:$PATH

[root@centos7 http2]# echo $PATH
/usr/local/http2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

(2)配置文件我们在编译安装时已经通过 --sysconfdir=/etc/http2 选项指定了

(3)库文件 lib目录

[root@sixijie http2]# vim /etc/ld.so.conf.d/http2.conf
/usr/local/http2/lib

让系统重新生成库文件路径缓存

[root@sixijie apache2]# ldconfig -v

缓存文件:/etc/ld.so.cache

(4)帮助文档 man

[root@centos7 lib]# vim /etc/man_db.conf (centos 7)
添加一行内容:MANDATORY_MANPATH           /usr/local/http2/man

(5)对于 include 头文件 采用软链接的方式(/usr/include为系统头文件位置)

[root@centos7 include]# ln -s /usr/local/http2/include/ /usr/include/http
[root@centos7 include]# ll -d /usr/include/http
lrwxrwxrwx. 1 root root 25 Aug 23 20:46 /usr/include/http -> /usr/local/http2/include/
[root@centos7 include]# ll /usr/include/http/

至此安装配置完成