Centos7上编译安装Httpd2.4

Centos7上编译安装HTTPD2.4

在Centos7上使用yum安装httpd是最简便的方式。但是由于Centos7自带的htpd版本固定,有时我们可能需要安装更新版本的httpd,或者需要开启httpd默认未开启的功能模块,此时编译httpd源码的安装方式将是我们必须掌握的技能。

1 准备源码包

从官网下载httpd源码包,并将其街道指定路径:

[root@Centos7 R1 ~]#ls /app/packages/
httpd-2.4.28.tar.gz
[root@Centos7 R1 ~]#cd /app/source/
[root@Centos7 R1 source]#tar xf ../packages/httpd-2.4.28.tar.gz
[root@Centos7 R1 source]#ls
httpd-2.4.28

2 安装编译需要的相关组件

编译源码文件需要依赖特定的编译工具,同时,编译Httpd也需要依赖特定的相关工具,我们在此一并安装:

yum -y -q groupinstall "Development tools"
yum -y -q install apr-devel apr-util-devel openssl-devel pcre-devel mod_ssl expat-devel

3 创建apache账户

httpd在运行过程中,需要使用一个同医德账户管运行相关的进程,我们定义该账户为apache,并予以创建:

[root@Centos7 R1 httpd]#useradd -r -d /app/httpd/htdocs/ -s /sbin/nologin apache
[root@Centos7 R1 httpd]#cat /etc/passwd |grep apache
apache:x:304:304::/app/httpd/htdocs:/sbin/nologin

我们设定apache账户的家目录为/app/httpd/htdocs是因为,在后续编译完成后,该目录将被用于存储网页文件,也即DocumentRoot,apache账户需要有该目录的读权限。

4 编译安装Httpd

编译安装Httpd需要用到源码包自带的configure工具,进入解压后的目录后,我们可以使用./configure --help查看相关的帮助

[root@Centos7 R1 httpd-2.4.28]#./configure --help
`configure' configures this package to adapt to many kinds of systems.
Usage: ./configure [OPTION]... [VAR=VALUE]...
To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.
Defaults for the options are specified in brackets.
Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [/usr/local/apache2]
  --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX
                          [PREFIX]
Fine tuning of the installation directories:
  --bindir=DIR            user executables [EPREFIX/bin]
  --sysconfdir=DIR        read-only single-machine data [PREFIX/etc]
  --mandir=DIR            man documentation [DATAROOTDIR/man]
  --docdir=DIR            documentation root [DATAROOTDIR/doc/PACKAGE]
  --htmldir=DIR           html documentation [DOCDIR]
…… ……

编译Httpd需要用到以下两条命令:

[root@Centos7 R1 httpd-2.4.28]#./configure --prefix=/app/httpd --enable-so --enable-ssl --enable-cgi \  
--enable-rewrite --with-zlib --with-pcre --enable-modules=most --enable-mpms-shared=all --with-mpm=worker
…… ……
configure: summary of build options:

    Server Version: 2.4.28
    Install prefix: /app/httpd
    C compiler:     gcc -std=gnu99
    CFLAGS:           -pthread
    LDFLAGS:         
    LIBS:           
    CPPFLAGS:        -DLINUX -D_REENTRANT -D_GNU_SOURCE
    C preprocessor: gcc -E
[root@Centos7 R1 httpd-2.4.28]make && make install
Making all in srclib
make[1]: Entering directory `/app/source/httpd-2.4.28/srclib'
make[1]: Leaving directory `/app/source/httpd-2.4.28/srclib'
Making all in os
make[1]: Entering directory `/app/source/httpd-2.4.28/os'
Making all in unix
make[2]: Entering directory `/app/source/httpd-2.4.28/os/unix'
make[3]: Entering directory `/app/source/httpd-2.4.28/os/unix'
…… ……
Installing configuration files
[PRESERVING EXISTING HTDOCS SUBDIR: /app/httpd/htdocs]
[PRESERVING EXISTING ERROR SUBDIR: /app/httpd/error]
[PRESERVING EXISTING ICONS SUBDIR: /app/httpd/icons]
[PRESERVING EXISTING CGI SUBDIR: /app/httpd/cgi-bin]
Installing header files
Installing build system files
Installing man pages and online manual
make[1]: Leaving directory `/app/source/httpd-2.4.28'
[root@Centos7 R1 httpd-2.4.28]#cd /app/httpd/
[root@Centos7 R1 httpd]#ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs  man  manual  modules

5 修改Httpd的配置文件

编译完成的Httpd的配置文件位于/app/httpd/conf/httpd.conf,我们需要先做个简单的设置,复杂的设置后期可以参考文档自行设置:

[root@Centos7 R1 httpd]#sed -i.bak 's/#ServerName/ServerName/g' /app/httpd/conf/httpd.conf
[root@Centos7 R1 httpd]#sed -i 's/User daemon/User apache/' /app/httpd/conf/httpd.conf
[root@Centos7 R1 httpd]#sed -i 's/Group daemon/Group apache/' /app/httpd/conf/httpd.conf

第一条命令用于修改Httpd的ServerName,否则每次启动Httpd服务都会报错,且启动很慢,尽管并不影响Httpd服务运行。此处显然只是将注释符号去掉就可以了。第二条命令和第三条命令指定了运行Httpd进程的默认账户信息。

6 将httpd服务加入开机启动

由于是编译安装,没有启动脚本,我们不能够使用systenctl工具对Httpd服务进行管理,也无法使用chkconfig命令对Httpd的开机启动行为进行控制。但是编译完成的Httpd文件中已经自带了控制Httpd服务运行的二进制程序apachectl,同样可使用start、stop、restart等参数命令对Httpd服务进行控制。因此,我们需要将该命令加入控制开机启动的文件/etc/rc.d/rc.local中:

[root@Centos7 R1 httpd]#echo "/app/httpd/bin/apachectl start" >> /etc/rc.d/rc.local
[root@Centos7 R1 httpd]#chmod +x /etc/rc.d/rc.local
[root@Centos7 R1 httpd]#tail -n1 /etc/rc.d/rc.local       
/app/httpd/bin/apachectl start
[root@Centos7 R1 httpd]#ll /etc/rc.d/rc.local
-rwxr-xr-x 1 root root 504 Oct 27 19:52 /etc/rc.d/rc.local

7 修改PATH变量

前面我们提到Httpd自带的命令工具apachectl,该命令位于目录/app/httpd/bin/,同时该目录下还有一些其他的命令工具,为了能够直接使用这些命令而不每次陈指定路径,我们需要将该目录加入到PATH变量:

[root@Centos7 R1 httpd]#echo "PATH=/app/httpd/bin:\$PATH" > /etc/profile.d/httpd.sh
[root@Centos7 R1 httpd]#cat /etc/profile.d/httpd.sh
PATH=/app/httpd/bin:$PATH

8 启动Httpd服务并查看端口信息

[root@Centos7 R1 httpd]#apachectl start
[root@Centos7 R1 httpd]#pstree  | grep httpd  
        |-httpd---3*[httpd---26*[{httpd}]]
        [root@Centos7 R1 httpd]#ss -ntl
        State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
        LISTEN     0      10     172.18.42.51:53                            *:*                  
        LISTEN     0      10     192.168.25.51:53                            *:*                  
        LISTEN     0      10        127.0.0.1:53                            *:*                  
        LISTEN     0      128               *:22                            *:*                  
        LISTEN     0      100       127.0.0.1:25                            *:*                  
        LISTEN     0      128       127.0.0.1:953                           *:*                  
        LISTEN     0      80               :::3306                         :::*                  
        LISTEN     0      128              :::80                           :::*                  
        LISTEN     0      10              ::1:53                           :::*                  
        LISTEN     0      128              :::22                           :::*                  
        LISTEN     0      100             ::1:25                           :::*                  
        LISTEN     0      128             ::1:953                          :::*                  

很显然Httpd服务启动成功,并且工作于worker模式,并对80端口进行监听。现在我们可以使用curl命令访问httpd服务的Web页面:

[root@Centos7 R1 httpd]#echo Congratulations'!' > /app/httpd/htdocs/index.html  
[root@Centos7 R1 httpd]#cat /app/httpd/htdocs/index.html
Congratulations!
[root@Centos7 R1 httpd]#curl -I http://127.0.0.1:80                           
HTTP/1.1 200 OK
Date: Fri, 27 Oct 2017 14:01:57 GMT
Server: Apache/2.4.28 (Unix)
Last-Modified: Fri, 27 Oct 2017 14:01:26 GMT
ETag: "11-55c87b98974a1"
Accept-Ranges: bytes
Content-Length: 17
Content-Type: text/html

[root@Centos7 R1 httpd]#curl http://127.0.0.1:80                              
Congratulations!

测试成功。至此Httpd编译安装完成。

你可能感兴趣的:(linux)