在Linux中程序的安装最早就是通过编译源码来安装程序包的,之前并没有rpm和yum来方便的安装,当然rpm和yum为我们提供了很多方便的功能,但是如果我们需要安装的一个程序包没有制作好的rpm包怎么办呢?只有源代码,所以还是得知道如何用最原始的方法去安装一个源代码包,这里呢就以httpd为例说一说如何用源代码来编译安装。

在说安装一个程序包之前,需要了解下程序包是由什么组成的,只有知道了它的组成那么安装的时候才能知道需要做些什么。通常来说一个程序包括了四个部分,如下:

    1. 二进制的可执行程序:这个是必须包含的就像在windows安装完成以后,会生成一个exe可执行文件,通过执行这个文件来执行程序。一般情况下,在系统中执行一条命令,如果没有添加绝对路径的话,系统默认会在PATH定义的变量里面去寻找,如果找不到就无法执行,在CentOS当中,默认定义了PATH为/bin,/sbin/,/usr/bin,/usr/sbin,/usr/local/bin,/usr/local/sbin以及/root/bin。如果说新安装的程序中包含的可执行程序,没有在这些路径下的话,怎么办呢?这是我们需要解决的第一个问题。

    2. 库文件:执行的程序需要依赖系统提供的库文件,当然程序本身也会有一些库文件。如果程序在执行的过程当中,找不到相依赖的库。肯定也是无法执行的。默认系统定义的库文件存放的路径是在/lib,/usr/lib,/usr/local/lib这些路径下,如果是64位系统的话,额外会有64位应用的库/lib64,/usr/lib64以及/usr/local/lib64. 安装完成以后这是我们需要解决的第二个问题。

    3. 配置文件:Linux的哲学思想一切皆文件,配置文件都是以文件程序来存储的,如果没有相应的配置文件,怎么去修改程序的行为呢?或者调整参数,调优等。所以这个文件也是必需的。事实上,在程序安装的过程当中,已经知道了自己相应的配置文件在什么地方,所以这个我们不需要关心。但是我们需要关心的是在安装的过程中配置文件的存放路径,一般都是存放在/etc目录下的。这是第三个需要考虑的问题,此问题可选。

    4. 帮助文件:在Linux系统当中会有一些帮助文件,一般是存放在/usr/share/doc下的,使用man文件可以查看,如果想通过man来直接查看的话,也是需要指定新安装文件的路径的。这是第四个问题,需要在安装时考虑的,可选的,如果不指定的话,对程序的运行无影响,但是无法通过man文件来查询相应的帮助文档。

实验环境:

   操作系统:CentOS 6.4

   程序包: httpd-2.4.6

编译环境的准备:

    既然是编译安装,那么如果没有相应的编译环境的话,源程序代码是无法被编译成功的,在CentOS6中需要提前安装两个开发包,Development tools和Server Platform Development。

    这里利用yum工具先来查询下是否已经安装,如果没有安装通过yum来安装。Yum要正常使用的话需要完成相应配置否则会报错,如里需要了解如何配置以及具体使用细节请参考(yum的具体用法可参考linux软件包管理之YUM)

[root@centos1 ~]# yum grouplist "Development tools"   #查询Development tools是否安装 
Loaded plugins: fastestmirror, security
Setting up Group Process
Loading mirror speeds from cached hostfile
Installed Groups:          #installed表示已经安装。
   Development tools
Done
[root@centos1 ~]# yum grouplist "Server Platform Development"
Loaded plugins: fastestmirror, security
Setting up Group Process
Loading mirror speeds from cached hostfile
Available Groups:           #Available表示并没有安装,需要安装
   Server Platform Development
Done
#如果没有安装可以使用如下命令来完成安装
[root@centos1 ~]# yum groupinstall "Server Platform Development"
[root@centos1 ~]# yum groupinstall "Development tools"

解压源代码:

    开发包安装完成以后,会帮我们解决好多编译时间遇到的问题。开始安装的过程

    一般情况下,源代码都是压缩后打包提供的,安装的话首先就需要将源代码解压。

[root@centos1 ~]# pwd   #查询当前的工作路径
/root
[root@centos1 ~]# ll http*   #查询下源包是否在当前路径下,看到有一个httpd-2.4.6的一个包
-rw-r--r-- 1 root root 4949897 Sep  6 00:52 httpd-2.4.6.tar.bz2
[root@centos1 ~]# tar xf ./httpd-2.4.6.tar.bz2 
#解压缩httpd包。如果需要看详细的工作过程,请添加v选项,即tar xvf httpd-2.4.6.tar.bz2
[root@centos1 ~]#

安装前的配置(configure):

    在安装前需要先对源程序包进行配置(configure), 此过程当中它会检查编译环境是否具备,比如依赖包是否已经安装,如果没有安装,需要先安装被依赖的包才能继续配置,同时还可以指定相应的安装路径,配置文件的存放路径等参数。最后会生成makefile文件,编译(make)时会根据这个配置文件来进行编译。

[root@centos1 ~]# cd ./httpd-2.4.6   
[root@centos1 httpd-2.4.6]# ./configure --prefix=/usr/local/httpd --sysconfdir=/etc/httpd
checking for chosen layout... Apache       #开始检验安装环境等
checking for working mkdir -p... yes
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
.......#余下省略,如果没有报错,configure完成
creating Makefile
....

    cd到解压缩后的目录,然后执行configure, 这里的两个参数,--prefix表示程序的安装路径,--sysconfigdir是指配置文件存放的路径。每个程序的参数指定方式可能会有所不同可以根据configure --help来查看具体的用法。

编译(make):

    confgure完成以后,会生成一个makefile,然后就可以进行编译了。

[root@centos1 httpd-2.4.6]# make 
......  #此处省略过程
make[4]: Leaving directory `/root/httpd-2.4.6/modules/mappers'
make[3]: Leaving directory `/root/httpd-2.4.6/modules/mappers'
make[2]: Leaving directory `/root/httpd-2.4.6/modules'
make[2]: Entering directory `/root/httpd-2.4.6/support'
make[2]: Leaving directory `/root/httpd-2.4.6/support'
make[1]: Leaving directory `/root/httpd-2.4.6'

    执行make命令就开始编译,如果程序较大可能需要时间很长,最后会显示一个make结果

安装(make install):

    编译完成以后就是安装了,安装很简单,就是把编译完成的文件,复制到相应的目录

[root@centos1 httpd-2.4.6]# make install
......   #过程省略
Installing configuration files  #安装配置文件
mkdir /etc/httpd
mkdir /etc/httpd/extra
mkdir /etc/httpd/original
mkdir /etc/httpd/original/extra
Installing HTML documents
......
Installing man pages and online manual  #最后安装帮助文件
mkdir /usr/local/httpd/man
mkdir /usr/local/httpd/man/man1
mkdir /usr/local/httpd/man/man8
mkdir /usr/local/httpd/manual
make[1]: Leaving directory `/root/httpd-2.4.6'   #安装完成
[root@centos1 httpd-2.4.6]#

检验安装的结果:

    安装完成了,然后去检查一下程序是否能正常工作

[root@centos1 httpd-2.4.6]# lsof -i:80    #查询下httpd是否已经运行,无显示说明没有运行
[root@centos1 httpd-2.4.6]# /usr/local/httpd/bin/httpd #启动httpd
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.92.22.188. Set the 'ServerName' directive globally to suppress this message
#运行httpd会自动找DNS去解释IP地址对应的域名,这里并没有,所以会报错。实际上已经正常运行。
[root@centos1 httpd-2.4.6]# lsof -i:80     #再次查询,httpd已经起来了,并且处于listen状态
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
httpd   2835   root    4u  IPv6 115671      0t0  TCP *:http (LISTEN)
httpd   2836 daemon    4u  IPv6 115671      0t0  TCP *:http (LISTEN)
httpd   2837 daemon    4u  IPv6 115671      0t0  TCP *:http (LISTEN)
httpd   2838 daemon    4u  IPv6 115671      0t0  TCP *:http (LISTEN)
[root@centos1 httpd-2.4.6]#

通过远程主机或者本机测试:

    在浏览器中输入提供服务的IP地址:

Linux程序包管理之编译安装httpd_Linux

    出现It works!说明已经正常运行,这里输入的是主机IP地址,如果域名已经被解析出来的话,也可以通过域名访问。

安装完成以后需要解决的问题:

    1. 二进制可执行程序的输出

        开篇说过,默认情况下,系统的查找路径并不包含安装的路径,所以在检验的时候,输入的是绝对路径,那么怎么解决呢?这就需要去更改PATH配置文件。

[root@centos1 httpd-2.4.6]# httpd   #直接执行httpd
-bash: httpd: command not found    #提示命令并没有找到
[root@centos1 httpd-2.4.6]# echo $PATH   #查看下PATH变量
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos1 httpd-2.4.6]#

    从输出结果可以看出来,PATH变量中不包含/usr/local/httpd/bin这个路径,所以不能直接运行httpd命令,可以直接在shell中去更改这个变量,但是这种修改一旦重新启动系统就会失效,那么该怎么办呢?直接修改profile配置文件,系统启动时会自动读取这个profile文件。这里我们在/etc/profile.d目录下创建一个名为httpd的配置文件,如下:

[root@centos1 httpd-2.4.6]# vim /etc/profile.d/httpd.sh 
PATH=/usr/local/httpd/bin:$PATH
export PATH
[root@centos1 httpd-2.4.6]# . /etc/profile.d/httpd.sh  #加载httpd的配置文件
[root@centos1 httpd-2.4.6]# echo $PATH  #查看加载的结果,/usr/local/httpd/bin已经被添加
/usr/local/httpd/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos1 httpd-2.4.6]# httpd
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.92.22.188. Set the 'ServerName' directive globally to suppress this message
#命令执行成功,和用绝对路径的效果是一样的。

    2. 库文件的输出

        如果只是想执行此程序,不需要给其它程序提供httpd的依赖的话,那么就不需要将库文件输出,如果需要就必须让别的程序找到,在Linux系统中默认是需要到/lib或者/lib64目录下去查找相应的库文件,一般程序安装完成以后,库文件都存放在相应安装路径的/lib目录下。如果需要系统找到的话就需要修改lib的配置文件,配置文件存放于/etc/ld.so.config/目录下,创建一个httpd对应的配置文件,如下:

[root@centos1 httpd-2.4.6]# vim /etc/ld.so.conf.d/httpd.conf
/usr/local/httpd/lib
[root@centos1 httpd-2.4.6]# ldconfig  #让系统重新生成库文件缓存

    3. 头文件的输出

        更新系统文件所需的头文件。默认头文件是存放在/usr/include目录下,只需要建立一个链接,让系统找到即可

[root@centos1 httpd-2.4.6]# ln -s /usr/local/httpd/include /usr/include/httpd

    4. 导出man帮助文件

        修改man配置文件,让系统自动找到httpd相关的帮助文件。添加一个httpd的帮助文件所在的路径,命令如下

[root@centos1 httpd]# echo "MANPATH /usr/local/httpd/man" >> /etc/man.config

    至此,编译安装的过程以及需要解决的问题都完成了。

安装时configure过程中可能遇到的问题:

    1. configure: error: APR not found.  Please read the documentation.

        安装时需要的APR依赖包没有安装,下载然后编译安装此程序。安装完成以后需要更改httpd的配置选项,需要添加--with-apr=APR的安装路径。安装过程如下:

[root@centos1 httpd-2.4.6]# wget http://archive.apache.org/dist/apr/apr-1.4.5.tar.gz #下载apr包
[root@centos1 httpd-2.4.6]# cd ./apr-1.4.5
[root@centos1 httpd-2.4.6]# ./configure --prefix=/usr/local/apr  
[root@centos1 httpd-2.4.6]# make && make install  #表示编译执行完以后即安装
[root@centos1 httpd-2.4.6]# ./configure --prefix=/usr/local/httpd --sysconfdir=/etc/httpd --with-apr=/usr/local/apr

    2. configure: error: APR-util not found.

        缺少apr-util依赖包,解决方法类似apr的问题,但在安装apr-util的过程中,如果提示ARP not found的话,需要添加--with-apr=apr安装路径。并且安装成apr-util后,httpd的配置路径需要添加--with-apr-util=apr-util的安装路径,过程如下:

[root@centos1 httpd-2.4.6]# wget wget http://archive.apache.org/dist/apr/apr-util-1.3.12.tar.gz  
[root@centos1 httpd-2.4.6]# cd ./apr-util-1.3.12
[root@centos1 httpd-2.4.6]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@centos1 httpd-2.4.6]# make && make install
[root@centos1 httpd-2.4.6]# ./configure --prefix=/usr/local/httpd --sysconfdir=/etc/httpd --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util

    3. configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/

        缺少pcre依赖包,可以到错误指定的页面下查找相应程序的下载地址,进行下载后编译安装,过程不再细述,类似于上两个,同样在安装完成以后,需要在httpd的配置选项上加--with-pcre=pcre安装路径。