Linux源代码编译安装详解

1. 编译源代码步骤


configure、Makefile.in一般是项目管理器自动生成的,这依赖于两个开发工具:

  • autoconf:生成configure脚本文件;
    • autoconf需要结合一个配置文件来和其它命令生成configure脚本;这个配置文件告诉autoconf当前代码有哪些特性、指定哪些安装路径等,而后才能生成configure脚本;
  • automake:生成Makefile.in模板文件;
    • automake也要结合一个文件,这个指明有多少程序文件、它们的依赖关系是什么、以及使用哪一种版本的编译器等,而后才能生成Makefile.in;
Note:在安装之前查看INSTALL和README文件查看安装方法,因为不同程序包安装步骤不一样,有的程序包不需要configure,直接make后执行make install;而有的程序包不需要执行make install,只有一个二进制文件直接复制即可完成;

安装开发组件
想要进行编译,提前安装开发组件

 6:Development Tools、Server Platform Development
CentOS 7:Development Tools、Development and Creative Workstation

(1) ./configure(也可能是config文件)
--help:获取支持使用的选项;

在执行make命令之前,用户指定程序包的编译参数、启用特性、安装路径等等;
configure脚本会收集当前系统上的开发环境中所依赖各组件的版本、特性等,并检查所依赖的环境是否能满足,如果检查到所依赖的程序包不存在立即报错,无法执行下一步。最后会根据用户指定信息和Makefile.in文件生成一个makefile文件;

##通用的几个选项
--prefix=          :指定安装的路径
--sysconfdir=      :指定配置文件目录
--enable-feature   :启用某个特性
--disable-fecture  :禁用特性
--with-function    :启用某功能
--without-function :禁用某功能

(2) make

每个源代码都有专用的makefile,在make时依据这个配置文件,调用指定的预处理器做处理、调用指定的编译器做处理、编译文件的顺序等操作等;

(3) make install

其实是一些脚本,实现把构建出的应用程序,分别分配到用户指定的目录中;如创建出的二进制文件放到指定的二进制目录、库文件放到指定的库目录等等,使用install拷贝命令也能实现;

2. 安装后的配置

(1) 将二进制程序目录添加至PATH文件变量中,否则只能使用绝对路径才能运行程序

编辑文件/etc/profile.d/NAME.sh  
export PATH=/PATH/TO/BIN:$PATH

(2) 导出库文件路径

编辑/etc/ld.so.conf.d/NAME.conf,添加新的库文件所在的目录至此文件中;    
例如:/usr/local/apache2/lib   

ldconfig -v:重新加载库文件路径,并查看;
ldconfig -p:查看已加载所有库文件路径;

(3) 导出头文件

/usr/include/目录下,创建软链接; 
ln -s  target_dir  soft_link        ##可以导出一个目录为所有文件链接,也可以为每一个头文件导出一个链接,然后把创建的链接输出到/usr/include/目录下,或者直接复制头文件至/usr/include/目录;

(4) 导出man手册

Centos7是在/etc/man_db.conf的40到50行之间,添加一个MANPATH参数(Centos6可能是/etc/man.config);    
MANPATH_MAP     /usr/local/apache2/bin  /usr/local/apache2/man  

3. 编译安装实验

3.1. Centos7编译安装apache 2.2.27

系统版本与软件版本

[root@GNOME-C7 ~]# cat /etc/-release
CentOS Linux release 7.4.1708 (Core)
[root@GNOME-C7 ~]# uname -r
3.10.0-693.el7.x86_64
------------------------------------------------------------------
apr-1.6.3
apr-util-1.6.1
httpd-2.2.27

提前安装开发环境
[root@GNOME-C7 ~]# yum groupinstall -y "Development tools" "Development and Creative Workstation"

3.1.1. 安装apr-1.6.3

[root@GNOME-C7 ~]# cd apr-1.6.3/
root@GNOME-C7 apr-1.6.3]# ./configure -prefix=/usr/local/apr-1.6.3
[root@GNOME-C7 apr-1.6.3]# make
[root@GNOME-C7 apr-1.6.3]# make install

3.1.2. 安装apr-util-1.6.1

[root@GNOME-C7 apr-1.6.3]# cd ../apr-util-1.6.1/
[root@GNOME-C7 apr-util-1.6.1]# ./configure -prefix=/usr/local/apr-unil-1.6.1 --with-apr=/usr/local/apr-1.6.3

[root@GNOME-C7 apr-util-1.6.1]# make
遇到报错:
xml/apr_xml.c:35:19: fatal error: expat.h: No such file or directory
#include <expat.h>
                   ^
compilation terminated.
make[1]: *** [xml/apr_xml.lo] Error 1
make[1]: Leaving directory `/root/apr-util-1.6.1'
make: *** [all-recursive] Error 1
解决方法:yum install -y expat-devel

[root@GNOME-C7 apr-util-1.6.1]# make install

3.1.3. 安装httpd-2.2.27

编译安装

[root@GNOME-C7 apr-util-1.6.1]# cd ../httpd-2.2.27/
[root@GNOME-C7 httpd-2.2.27]# ./configure --sysconfdir=/etc/httpd --prefix=/usr/local/apache2  --with-apr=/usr/local/apr-1.6.3 --with-apr-util=/usr/local/apr-unil-1.6.3 --with-mpm=prefork --with-included-apr --enable-mods-shared=most --enable-ssl --enable-suexec --enable-so
遇到报错:configure: error: ...No recognized SSL/TLS toolkit detected
解决方法:yum install -y openssl-devel

[root@GNOME-C7 httpd-2.2.27]# make
[root@GNOME-C7 httpd-2.2.27]# make install

编译第二次,从而得到httpd.worker        ##这种方法不确定是否可以
[root@GNOME-C7 httpd-2.2.27]# cd ../
[root@GNOME-C7 ~]# rm -rf httpd-2.2.27/
[root@GNOME-C7 ~]# tar -xf httpd-2.2.27.tar.bz2
[root@GNOME-C7 ~]# cd ./httpd-2.2.27/
[root@GNOME-C7 httpd-2.2.27]# ./configure --sysconfdir=/etc/httpd --prefix=/usr/local/apache2  --with-apr=/usr/local/apr-1.6.3 --with-apr-util=/usr/local/apr-unil-1.6.3 --with-mpm=worker --with-included-apr --enable-mods-shared=most --enable-ssl --enable-suexec --enable-so
[root@GNOME-C7 httpd-2.2.27]# make       ##不用执行make install否则就会覆盖当前apache的文件了
[root@GNOME-C7 httpd-2.2.27]# cp ./httpd /usr/local/apache2/bin/httpd.worker


本次使用的编译参数
--prefix=/PATH:指明安装路径;
--sysconfdir=/PATH:指明配置文件路径;不指明,默认在安装路径下;
--enable-so:表示启用DSO动态模块加载机制;
--enable-ssl:启用支持ssl功能;
--enable-mods-shared=most:表示动态的编译进来大多数的模块;如果使用--enable-mods-shared=all,则指动态的编译所有的模块。
--with-apr=/PATH:指明apr程序包的路径;不使用=指明路径会在默认路径查找;
--with-apr-util=/PATH:指明apr-util程序包的路径;
--with-mpm=prefork:指明默认使用的MPM模块;


说明:
静态编译:将module直接在安装的过程中放到apache中,当apache使用module的时候,就可以直接使用了。
动态编译:将module引入到apache之中,在使用的时候才会真正的去加载(通过LoadModule命令)
--with.....:表示依赖的包,不指明路径,编译安装时会到默认路径下找;
--without.....:不依赖的包;
--enable.....:启用特性;
--disable.....:禁用特性;


可能用到的其他参数
[root@Centos7 httpd-2.2.29]# ./configure --help        ##查看有哪些支持的选项
部分内容如下:
    Fine tuning of the installation directories:
      --bindir=DIR            user executables [EPREFIX/bin]:所有用户执行的命令放在哪个目录;
      --sbindir=DIR           system admin executables [EPREFIX/sbin]:管理员执行的命令放在哪个目录;
      --libexecdir=DIR        program executables [EPREFIX/libexec]:库执行的程序放在哪个目录;
      --sysconfdir=DIR        read-only single-machine data [PREFIX/etc]:配置文件放在哪个目录;
      --sharedstatedir=DIR    modifiable architecture-independent data [PREFIX/com]:共享数据目录放在哪个目录;
      --localstatedir=DIR     modifiable single-machine data [PREFIX/var]:本地状态目录放在哪个目录;
      --libdir=DIR            object code libraries [EPREFIX/lib]:库文件放在哪个目录;
      --includedir=DIR        C header files [PREFIX/include]:头文件放在哪个目录;

安装后的产生的目录

]# ll /usr/local/apache2/
显内容示:
drwxr-xr-x  2 root root  4096 Apr 14 08:51 bin 程序路径
drwxr-xr-x  2 root root  4096 Apr 14 08:51 build 编译安装的相关文件
drwxr-xr-x  2 root root  4096 Apr 14 08:51 cgi-bin cgi格式的程序文件
drwxr-xr-x  4 root root  4096 Apr 14 08:51 conf 配置文件
drwxr-xr-x  3 root root  4096 Apr 14 08:51 error 错误页面
drwxr-xr-x  2 root root  4096 Apr 14 08:30 htdocs 网页文件存放路径
drwxr-xr-x  3 root root  4096 Apr 14 08:51 icons 图标
drwxr-xr-x  2 root root  4096 Apr 14 08:51 include 头文件,任何支持二次开发的程序都提供头文件
drwxr-xr-x  2 root root  4096 Apr 14 08:53 logs 日志
drwxr-xr-x  4 root root  4096 Apr 14 08:51 man 帮助手册
drwxr-xr-x 14 root root 12288 Jul 16  2014 manual 官方文档
drwxr-xr-x  2 root root  4096 Apr 14 08:51 modules 模块

3.1.4. 导出二进制程序目录至PATH变量

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

3.1.5. 导出man手册

[root@centos7 ~]# vim /etc/man_db.conf
MANPATH_MAP /usr/local/apache2/bin    /usr/local/apache2/man
[root@GNOME-C7 ~]# manpath
/usr/local/share/man:/usr/share/man/overrides:/usr/share/man:/usr/local/apache2/man

3.1.6. 导出头文件

[root@GNOME-C7 ~]# ln -sv /usr/local/apache2/include /usr/include/httpd
‘/usr/include/httpd’ -> ‘/usr/local/apache2/include’

3.1.7. 导出库文件

[root@GNOME-C7 ~]# vim /etc/ld.so.conf.d/httpd.conf
/usr/local/apache2/lib
[root@GNOME-C7 ~]# ldconfig -v
[root@GNOME-C7 ~]# ldconfig -p | grep apache
    libaprutil-1.so.0 (libc6,x86-64) => /usr/local/apache2/lib/libaprutil-1.so.0
    libaprutil-1.so (libc6,x86-64) => /usr/local/apache2/lib/libaprutil-1.so
    libapr-1.so.0 (libc6,x86-64) => /usr/local/apache2/lib/libapr-1.so.0
    libapr-1.so (libc6,x86-64) => /usr/local/apache2/lib/libapr-1.so

3.1.8. 创建服务管理脚本

这两个脚本是从其他机器上复制的,然后改了一下文件与目录的路径

[root@GNOME-C7 ~]# cat /etc/sysconfig/httpd
# Configuration file for the httpd service.

#
# The default processing model (MPM) is the process-based
# 'prefork' model.  A thread-based model, 'worker', is also
# available, but does not work with some modules (such as PHP).
# The service must be stopped before changing this variable.
#
HTTPD=/usr/local/apache2/bin/httpd.worker        ##表示启动的httpd服务mpm为worker

#
# To pass additional options (for instance, -D definitions) to the
# httpd binary at startup, set OPTIONS here.
#
#OPTIONS=

#
# By default, the httpd process is started in the C locale; to
# change the locale in which the server runs, the HTTPD_LANG
# variable can be set.
#
#HTTPD_LANG=C

#
# By default, the httpd process will create the file
# /var/run/httpd/httpd.pid in which it records its process
# identification number when it starts.  If an alternate location is
# specified in httpd.conf (via the PidFile directive), the new
# location needs to be reported in the PIDFILE.
#
#PIDFILE=/var/run/httpd/httpd.pid



[root@GNOME-C7 ~]# cat /etc/rc.d/init.d/httpd
#!/bin/bash
#
# httpd        Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: The Apache HTTP Server is an efficient and extensible  \
#           server implementing the current HTTP standards.
# processname: httpd
# config: /etc/httpd/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /usr/local/apache2/logs/httpd.pid
#
### BEGIN INIT INFO
# Provides: httpd
# Required-Start: $local_fs $remote_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Should-Start: distcache
# Short-Description: start and stop Apache HTTP Server
# Description: The Apache HTTP Server is an extensible server
#  implementing the current HTTP standards.
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/httpd ]; then
        . /etc/sysconfig/httpd
fi

# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache2/bin/apachectl
httpd=${HTTPD-/usr/local/apache2/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/usr/local/apache2/logs/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}

# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  So we just do it the way init scripts
# are expected to behave here.
start() {
        echo -n $"Starting $prog: "
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}

# When stopping httpd, a delay (of default 10 second) is required
# before SIGKILLing the httpd parent; this gives enough time for the
# httpd parent to SIGKILL any errant children.
stop() {
    status -p ${pidfile} $httpd > /dev/null
    if [[ $? = 0 ]]; then
        echo -n $"Stopping $prog: "
        killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd
    else
        echo -n $"Stopping $prog: "
        success
    fi
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}

reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
        RETVAL=6
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        # Force LSB behaviour from killproc
        LSB=1 killproc -p ${pidfile} $httpd -HUP
        RETVAL=$?
        if [ $RETVAL -eq 7 ]; then
            failure $"httpd shutdown"
        fi
    fi
    echo
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
        status -p ${pidfile} $httpd
    RETVAL=$?
    ;;
  restart)
    stop
    start
    ;;
  condrestart|try-restart)
    if status -p ${pidfile} $httpd >&/dev/null; then
        stop
        start
    fi
    ;;
  force-reload|reload)
        reload
    ;;
  graceful|help|configtest|fullstatus)
    $apachectl $@
    RETVAL=$?
    ;;
  *)
    echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"
    RETVAL=2
esac

exit $RETVAL

3.1.9. 使用chkconfig管理

[root@GNOME-C7 ~]# chmod 755 /etc/init.d/httpd 
[root@GNOME-C7 ~]# chkconfig --add httpd
[root@GNOME-C7 ~]# chkconfig --level 3 httpd on
[root@GNOME-C7 ~]# chkconfig --list | grep httpd
httpd              0:off    1:off    2:off    3:on    4:off    5:off    6:off

3.1.10. 启动服务

[root@GNOME-C7 ~]# service httpd start
Starting httpd (via systemctl):                            [  OK  ]
[root@GNOME-C7 ~]# ss -ant | grep 80
LISTEN     0      128          *:80                       *:*  
[root@GNOME-C7 ~]# ps aux | grep http[d]        ##现在运行httpd的用户是daemon,可以在配置文件中更改
root     114229  0.0  0.0  97508  2536 ?        Ss   18:34   0:00 /usr/local/apache2/bin/httpd
daemon   114231  0.0  0.0 386420  4416 ?        Sl   18:34   0:00 /usr/local/apache2/bin/httpd
daemon   114232  0.0  0.0 386420  4416 ?        Sl   18:34   0:00 /usr/local/apache2/bin/httpd
daemon   114233  0.0  0.0 386420  4420 ?        Sl   18:34   0:00 /usr/local/apache2/bin/httpd
daemon   114234  0.0  0.0 386420  4420 ?        Sl   18:34   0:00 /usr/local/apache2/bin/httpd

3.1.11. 访问测试

3.2. Centos7编译安装apache 2.4.6

系统版本与软件版本

[root@GNOME-C7 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@GNOME-C7 ~]# uname -r
3.10.0-693.el7.x86_64
------------------------------------------------------------------
apr-1.6.3
apr-util-1.6.1
httpd-2.4.6

提前安装开发环境
[root@GNOME-C7 ~]# yum groupinstall -y "Development tools" "Development and Creative Workstation"

3.2.1. 安装apr-1.6.3

[root@GNOME-C7 ~]# cd apr-1.6.3/
root@GNOME-C7 apr-1.6.3]# ./configure -prefix=/usr/local/apr-1.6.3
[root@GNOME-C7 apr-1.6.3]# make
[root@GNOME-C7 apr-1.6.3]# make install

3.2.2. 安装apr-util-1.6.1

[root@GNOME-C7 apr-1.6.3]# cd ../apr-util-1.6.1/
[root@GNOME-C7 apr-util-1.6.1]# ./configure -prefix=/usr/local/apr-unil-1.6.1 --with-apr=/usr/local/apr-1.6.3

[root@GNOME-C7 apr-util-1.6.1]# make
遇到报错:
xml/apr_xml.c:35:19: fatal error: expat.h: No such file or directory
#include <expat.h>
                   ^
compilation terminated.
make[1]: *** [xml/apr_xml.lo] Error 1
make[1]: Leaving directory `/root/apr-util-1.6.1'
make: *** [all-recursive] Error 1
解决方法:yum install -y expat-devel

[root@GNOME-C7 apr-util-1.6.1]# make install

3.2.3. 安装httpd-2.4.6

[root@GNOME-C7 apr-util-1.6.1]# cd ../httpd-2.4.6/
[root@GNOME-C7 apr-util-1.6.1]# yum install -y openssl-devel    ##如果想要直接编译成功ssl模块,需要提前安装该openssl-devel,否则编译过程中虽然不报错(因为不是是动态编译),但是编译结束后会发现没有ssl模块;
[root@GNOME-C7 httpd-2.4.6]# ./configure  --prefix=/usr/local/apache2 --with-apr=/usr/local/apr-1.6.3 --with-apr-util=/usr/local/apr-unil-1.6.1 --with-included-apr --sysconfdir=/etc/httpd --enable-so --enable-mpms-shared=all  --enable-mods-shared=all --with-mpm=prefork
遇到报错:configure: error: Bundled APR requested but not found at ./srclib/. Download and unpack the corresponding apr and apr-util packages to ./srclib/.
解决方法:cp -r ../apr-1.6.3 ./srclib/apr ; cp -r ../apr-util-1.6.1 ./srclib/apr-util
遇到报错:configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/
解决方法:yum -y install pcre-devel

[root@GNOME-C7 httpd-2.4.6]# make
[root@GNOME-C7 httpd-2.4.6]# make install

本次使用的编译参数
--enable-so:表示启用DSO动态模块加载机制;
--enable-mpms-shared=安装所有支持的MPM模块
--enable-mods-shared=all:动态的编译所有的模块
--with-mpm=prefork:指定默认MPM

3.2.4. 导出二进制程序目录至PATH变量

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

3.2.5. 导出man手册

[root@centos7 ~]# vim /etc/man_db.conf
MANPATH_MAP /usr/local/apache2/bin    /usr/local/apache2/man
[root@GNOME-C7 ~]# manpath
/usr/local/share/man:/usr/share/man/overrides:/usr/share/man:/usr/local/apache2/man

3.2.6. 导出头文件

[root@GNOME-C7 ~]# ln -sv /usr/local/apache2/include /usr/include/httpd
‘/usr/include/httpd’ -> ‘/usr/local/apache2/include’

3.2.7. 导出库文件

[root@GNOME-C7 ~]# vim /etc/ld.so.conf.d/httpd.conf
/usr/local/apache2/lib
[root@GNOME-C7 ~]# ldconfig -v
[root@GNOME-C7 ~]# ldconfig -p | grep apache
    libaprutil-1.so.0 (libc6,x86-64) => /usr/local/apache2/lib/libaprutil-1.so.0
    libaprutil-1.so (libc6,x86-64) => /usr/local/apache2/lib/libaprutil-1.so
    libapr-1.so.0 (libc6,x86-64) => /usr/local/apache2/lib/libapr-1.so.0
    libapr-1.so (libc6,x86-64) => /usr/local/apache2/lib/libapr-1.so

3.2.8. 创建服务管理脚本

这两个脚本是从其他机器上复制的,然后改了一下文件与目录的路径

[root@GNOME-C7 ~]#  cat /etc/sysconfig/httpd
# Configuration file for the httpd service.

#
# The default processing model (MPM) is the process-based
# 'prefork' model.  A thread-based model, 'worker', is also
# available, but does not work with some modules (such as PHP).
# The service must be stopped before changing this variable.
#
HTTPD=/usr/local/apache2/bin/httpd

#
# To pass additional options (for instance, -D definitions) to the
# httpd binary at startup, set OPTIONS here.
#
#OPTIONS=

#
# By default, the httpd process is started in the C locale; to
# change the locale in which the server runs, the HTTPD_LANG
# variable can be set.
#
#HTTPD_LANG=C



[root@GNOME-C7 ~]# cat /etc/rc.d/init.d/httpd
#!/bin/bash
#
# httpd        Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: The Apache HTTP Server is an efficient and extensible  \
#           server implementing the current HTTP standards.
# processname: httpd
# config: /etc/httpd/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /usr/local/apache2/logs/httpd.pid
#
### BEGIN INIT INFO
# Provides: httpd
# Required-Start: $local_fs $remote_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Should-Start: distcache
# Short-Description: start and stop Apache HTTP Server
# Description: The Apache HTTP Server is an extensible server
#  implementing the current HTTP standards.
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/httpd ]; then
        . /etc/sysconfig/httpd
fi

# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache2/bin/apachectl
httpd=${HTTPD-/usr/local/apache2/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/usr/local/apache2/logs/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}

# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  So we just do it the way init scripts
# are expected to behave here.
start() {
        echo -n $"Starting $prog: "
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}

# When stopping httpd, a delay (of default 10 second) is required
# before SIGKILLing the httpd parent; this gives enough time for the
# httpd parent to SIGKILL any errant children.
stop() {
    status -p ${pidfile} $httpd > /dev/null
    if [[ $? = 0 ]]; then
        echo -n $"Stopping $prog: "
        killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd
    else
        echo -n $"Stopping $prog: "
        success
    fi
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}

reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
        RETVAL=6
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        # Force LSB behaviour from killproc
        LSB=1 killproc -p ${pidfile} $httpd -HUP
        RETVAL=$?
        if [ $RETVAL -eq 7 ]; then
            failure $"httpd shutdown"
        fi
    fi
    echo
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
        status -p ${pidfile} $httpd
    RETVAL=$?
    ;;
  restart)
    stop
    start
    ;;
  condrestart|try-restart)
    if status -p ${pidfile} $httpd >&/dev/null; then
        stop
        start
    fi
    ;;
  force-reload|reload)
        reload
    ;;
  graceful|help|configtest|fullstatus)
    $apachectl $@
    RETVAL=$?
    ;;
  *)
    echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"
    RETVAL=2
esac

exit $RETVAL

3.2.9. 使用chkconfig管理

[root@GNOME-C7 ~]# chmod 755 /etc/init.d/httpd 
[root@GNOME-C7 ~]# chkconfig --add httpd
[root@GNOME-C7 ~]# chkconfig --level 3 httpd on
[root@GNOME-C7 ~]# chkconfig --list | grep httpd
httpd              0:off    1:off    2:off    3:on    4:off    5:off    6:off

3.2.10. 启动服务

[root@GNOME-C7 ~]# service httpd start
Starting httpd (via systemctl):                            [  OK  ]
[root@GNOME-C7 ~]# ss -ant | grep 80
LISTEN     0      128          *:80                       *:*  
[root@GNOME-C7 ~]# ps aux | grep http[d]        ##现在运行httpd的用户是daemon,可以在配置文件中更改
root     114229  0.0  0.0  97508  2536 ?        Ss   18:34   0:00 /usr/local/apache2/bin/httpd
daemon   114231  0.0  0.0 386420  4416 ?        Sl   18:34   0:00 /usr/local/apache2/bin/httpd
daemon   114232  0.0  0.0 386420  4416 ?        Sl   18:34   0:00 /usr/local/apache2/bin/httpd
daemon   114233  0.0  0.0 386420  4420 ?        Sl   18:34   0:00 /usr/local/apache2/bin/httpd
daemon   114234  0.0  0.0 386420  4420 ?        Sl   18:34   0:00 /usr/local/apache2/bin/httpd

3.2.11. 访问测试

猜你喜欢

转载自www.linuxidc.com/Linux/2018-12/155637.htm