zoukankan      html  css  js  c++  java
  • 编译httpd细节

    apache自己的站点提供了基金会下所有的(包括所有版本)资源,包括httpd。

    地址:http://archive.apache.org/dist/
    httpd下载地址:http://archive.apache.org/dist/httpd

    清华大学有一个httpd归档的镜像站点,里面提供最新测试版和最新稳定版的下载,还提供一些依赖包或模块的下载。

    地址:http://mirrors.tuna.tsinghua.edu.cn/apache/httpd/
    apache基金会下所有资源地址:http://mirrors.tuna.tsinghua.edu.cn/apache/

    httpd同样使用"./configure"、"make && make install"的编译流程编译。但是它有一些依赖包需要提前装好。官方上指定的依赖环境有:apr、apr-util、pcre、pcre-devle,此外还需要expat-devel包。其中pcre、pcre-devel和expat.devel可以直接使用yum安装,apr和apr-util需要编译安装。下载地址可以从站点 http://mirrors.tuna.tsinghua.edu.cn/apache/ap/ 找到。

    [root@localhost ~]# ls
    anaconda-ks.cfg  apr-1.6.3.tar.gz  apr-util-1.6.1.tar.gz
    [root@localhost ~]# tar xf apr-1.6.3.tar.gz 
    [root@localhost ~]# tar xf apr-util-1.6.1.tar.gz 
    [root@localhost ~]# cd apr-1.6.3
    [root@localhost apr-1.6.3]# ./configure --prefix=/usr/local/apr 
    [root@localhost apr-1.6.3]# make && make install
    [root@localhost apr-1.6.3]# yum -y install pcre pcre-devel expat-devel gcc*
    [root@localhost apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
    [root@localhost apr-util-1.6.1]# make && make install

    httpd编译选项                  

    httpd的编译选项非常多。以下是截取./configure -h中的一部分,使用"......"表示省略了一堆信息。

    [root@localhost ~]# tar xf httpd-2.4.29.tar.gz 
    [root@localhost ~]# cd httpd-2.4.29
    [root@localhost httpd-2.4.29]# ./configure -h
    `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.
    
    Configuration:
      -h, --help              display this help and exit
          --help=short        display options specific to this package
          --help=recursive    display the short help of all the included packages
      -V, --version           display version information and exit
      -q, --quiet, --silent   do not print `checking ...' messages
          --cache-file=FILE   cache test results in FILE [disabled]
      -C, --config-cache      alias for `--cache-file=config.cache'
      -n, --no-create         do not create output files
          --srcdir=DIR        find the sources in DIR [configure dir or `..']
    
    Installation directories:
      --prefix=PREFIX         install architecture-independent files in PREFIX
                              [/usr/local/apache2]
      --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX
                              [PREFIX]
    
    By default, `make install' will install all the files in
    `/usr/local/apache2/bin', `/usr/local/apache2/lib' etc.  You can specify
    an installation prefix other than `/usr/local/apache2' using `--prefix',
    for instance `--prefix=$HOME'.
    
    For better control, use the options below.
    
    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]
      --oldincludedir=DIR     C header files for non-gcc [/usr/include]
      --datarootdir=DIR       read-only arch.-independent data root [PREFIX/share]
      --datadir=DIR           read-only architecture-independent data [DATAROOTDIR]
      --infodir=DIR           info documentation [DATAROOTDIR/info]
      --localedir=DIR         locale-dependent data [DATAROOTDIR/locale]
      --mandir=DIR            man documentation [DATAROOTDIR/man]
      --docdir=DIR            documentation root [DATAROOTDIR/doc/PACKAGE]
      --htmldir=DIR           html documentation [DOCDIR]
      --dvidir=DIR            dvi documentation [DOCDIR]
      --pdfdir=DIR            pdf documentation [DOCDIR]
      --psdir=DIR             ps documentation [DOCDIR]
    
    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-layout=LAYOUT
      --enable-dtrace         Enable DTrace probes
      --enable-hook-probes    Enable APR hook probes
      --enable-exception-hook Enable fatal exception hook
      --enable-load-all-modules
                              Load all modules
      --enable-maintainer-mode
                              Turn on debugging and compile time warnings and load
                              all compiled modules
      --enable-debugger-mode  Turn on debugging and compile time warnings and turn
                              off optimization
      --enable-pie            Build httpd as a Position Independent Executable
      --enable-modules=MODULE-LIST
                              Space-separated list of modules to enable | "all" |
                              "most" | "few" | "none" | "reallyall"
      --enable-mods-shared=MODULE-LIST
                              Space-separated list of shared modules to enable |
                              "all" | "most" | "few" | "reallyall"
      --enable-mods-static=MODULE-LIST
                              Space-separated list of static modules to enable |
                              "all" | "most" | "few" | "reallyall"
      --disable-authn-file    file-based authentication control
      --enable-authn-dbm      DBM-based authentication control
      --enable-authn-anon     anonymous user authentication control
      --enable-authn-dbd      SQL-based authentication control
      --enable-authn-socache  Cached authentication control
      --disable-authn-core    core authentication module
      --disable-authz-host    host-based authorization control
      --disable-authz-groupfile
                              'require group' authorization control
      --disable-authz-user    'require user' authorization control
      --enable-authz-dbm      DBM-based authorization control
      --enable-authz-owner    'require file-owner' authorization control
      --enable-authz-dbd      SQL based authorization and Login/Session support
      --disable-authz-core    core authorization provider vector module
      --enable-authnz-ldap    LDAP based authentication
      --enable-authnz-fcgi    FastCGI authorizer-based authentication and
                              authorization
      --disable-access-compat mod_access compatibility
      --disable-auth-basic    basic authentication
      --enable-auth-form      form authentication
      --enable-auth-digest    RFC2617 Digest authentication
      --enable-allowmethods   restrict allowed HTTP methods
      --enable-isapi          isapi extension support
      --enable-file-cache     File cache
      --enable-cache          dynamic file caching. At least one storage
                              management module (e.g. mod_cache_disk) is also
                              necessary.
      --enable-cache-disk     disk caching module
      --enable-cache-socache  shared object caching module
      --enable-socache-shmcb  shmcb small object cache provider
      --enable-socache-dbm    dbm small object cache provider
      --enable-socache-memcache
                              memcache small object cache provider
      --enable-socache-dc     distcache small object cache provider
      --enable-so             DSO capability. This module will be automatically
                              enabled unless you build all modules statically.
      --enable-watchdog       Watchdog module
      --enable-macro          Define and use macros in configuration files
      --enable-dbd            Apache DBD Framework
      --enable-bucketeer      buckets manipulation filter. Useful only for
                              developers and testing purposes.
      --enable-dumpio         I/O dump filter
      --enable-echo           ECHO server
      --enable-example-hooks  Example hook callback handler module
      --enable-case-filter    Example uppercase conversion filter
      --enable-case-filter-in Example uppercase conversion input filter
      --enable-example-ipc    Example of shared memory and mutex usage
      --enable-buffer         Filter Buffering
      --enable-data           RFC2397 data encoder
      --enable-ratelimit      Output Bandwidth Limiting
      --disable-reqtimeout    Limit time waiting for request from client
      --enable-ext-filter     external filter module
      --enable-request        Request Body Filtering
      --enable-include        Server Side Includes
      --disable-filter        Smart Filtering
      --enable-reflector      Reflect request through the output filter stack
      --enable-substitute     response content rewrite-like filtering
      --enable-sed            filter request and/or response bodies through sed
      --disable-charset-lite  character set translation. Enabled by default only
                              on EBCDIC systems.
      --enable-charset-lite   character set translation. Enabled by default only
                              on EBCDIC systems.
      --enable-deflate        Deflate transfer encoding support
      --enable-xml2enc        i18n support for markup filters
      --enable-proxy-html     Fix HTML Links in a Reverse Proxy
      --enable-brotli         Brotli compression support
      --enable-http           HTTP protocol handling. The http module is a basic
                              one that enables the server to function as an HTTP
                              server. It is only useful to disable it if you want
                              to use another protocol module instead. Don't
                              disable this module unless you are really sure what
                              you are doing. Note: This module will always be
                              linked statically.
      --disable-mime          mapping of file-extension to MIME. Disabling this
                              module is normally not recommended.
      --enable-ldap           LDAP caching and connection pooling services
      --disable-log-config    logging configuration. You won't be able to log
                              requests to the server without this module.
      --enable-log-debug      configurable debug logging
      --enable-log-forensic   forensic logging
      --enable-logio          input and output logging
      --enable-lua            Apache Lua Framework
      --enable-luajit         Enable LuaJit Support
      --disable-env           clearing/setting of ENV vars
      --enable-mime-magic     automagically determining MIME type
      --enable-cern-meta      CERN-type meta files
      --enable-expires        Expires header control
      --disable-headers       HTTP header control
      --enable-ident          RFC 1413 identity check
      --enable-usertrack      user-session tracking
      --enable-unique-id      per-request unique ids
      --disable-setenvif      basing ENV vars on headers
      --disable-version       determining httpd version in config files
      --enable-remoteip       translate header contents to an apparent client
                              remote_ip
      --enable-proxy          Apache proxy module
      --enable-proxy-connect  Apache proxy CONNECT module. Requires
                              --enable-proxy.
      --enable-proxy-ftp      Apache proxy FTP module. Requires --enable-proxy.
      --enable-proxy-http     Apache proxy HTTP module. Requires --enable-proxy.
      --enable-proxy-fcgi     Apache proxy FastCGI module. Requires
                              --enable-proxy.
      --enable-proxy-scgi     Apache proxy SCGI module. Requires --enable-proxy.
      --enable-proxy-fdpass   Apache proxy to Unix Daemon Socket module. Requires
                              --enable-proxy.
      --enable-proxy-wstunnel Apache proxy Websocket Tunnel module. Requires
                              --enable-proxy.
      --enable-proxy-ajp      Apache proxy AJP module. Requires --enable-proxy.
      --enable-proxy-balancer Apache proxy BALANCER module. Requires
                              --enable-proxy.
      --enable-proxy-express  mass reverse-proxy module. Requires --enable-proxy.
      --enable-proxy-hcheck   reverse-proxy health-check module. Requires
                              --enable-proxy and --enable-watchdog.
      --enable-session        session module
      --enable-session-cookie session cookie module
      --enable-session-crypto session crypto module
      --enable-session-dbd    session dbd module
      --enable-slotmem-shm    slotmem provider that uses shared memory
      --enable-slotmem-plain  slotmem provider that uses plain memory
      --enable-ssl            SSL/TLS support (mod_ssl)
      --enable-ssl-staticlib-deps
                              link mod_ssl with dependencies of OpenSSL's static
                              libraries (as indicated by "pkg-config --static").
                              Must be specified in addition to --enable-ssl.
      --enable-optional-hook-export
                              example optional hook exporter
      --enable-optional-hook-import
                              example optional hook importer
      --enable-optional-fn-import
                              example optional function importer
      --enable-optional-fn-export
                              example optional function exporter
      --enable-dialup         rate limits static files to dialup modem speeds
      --enable-static-support Build a statically linked version of the support
                              binaries
      --enable-static-htpasswd
                              Build a statically linked version of htpasswd
      --enable-static-htdigest
                              Build a statically linked version of htdigest
      --enable-static-rotatelogs
                              Build a statically linked version of rotatelogs
      --enable-static-logresolve
                              Build a statically linked version of logresolve
      --enable-static-htdbm   Build a statically linked version of htdbm
      --enable-static-ab      Build a statically linked version of ab
      --enable-static-checkgid
                              Build a statically linked version of checkgid
      --enable-static-htcacheclean
                              Build a statically linked version of htcacheclean
      --enable-static-httxt2dbm
                              Build a statically linked version of httxt2dbm
      --enable-static-fcgistarter
                              Build a statically linked version of fcgistarter
      --enable-http2          HTTP/2 protocol handling in addition to HTTP
                              protocol handling. Implemented by mod_http2. This
                              module requires a libnghttp2 installation. See
                              --with-nghttp2 on how to manage non-standard
                              locations. This module is usually linked shared and
                              requires loading.
      --enable-nghttp2-staticlib-deps
                              link mod_http2 with dependencies of libnghttp2's
                              static libraries (as indicated by "pkg-config
                              --static"). Must be specified in addition to
                              --enable-http2.
      --enable-proxy-http2    HTTP/2 proxy module. This module requires a
                              libnghttp2 installation. See --with-nghttp2 on how
                              to manage non-standard locations. Also requires
                              --enable-proxy.
      --enable-lbmethod-byrequests
                              Apache proxy Load balancing by request counting
      --enable-lbmethod-bytraffic
                              Apache proxy Load balancing by traffic counting
      --enable-lbmethod-bybusyness
                              Apache proxy Load balancing by busyness
      --enable-lbmethod-heartbeat
                              Apache proxy Load balancing from Heartbeats
      --enable-mpms-shared=MPM-LIST
                              Space-separated list of MPM modules to enable for
                              dynamic loading. MPM-LIST=list | "all"
      --enable-unixd          unix specific support
      --enable-privileges     Per-virtualhost Unix UserIDs and enhanced security
                              for Solaris
      --enable-heartbeat      Generates Heartbeats
      --enable-heartmonitor   Collects Heartbeats
      --enable-dav            WebDAV protocol handling. --enable-dav also enables
                              mod_dav_fs
      --disable-status        process/thread monitoring
      --disable-autoindex     directory listing
      --enable-asis           as-is filetypes
      --enable-info           server information
      --enable-suexec         set uid and gid for spawned processes
      --enable-cgid           CGI scripts. Enabled by default with threaded MPMs
      --enable-cgi            CGI scripts. Enabled by default with non-threaded
                              MPMs
      --enable-dav-fs         DAV provider for the filesystem. --enable-dav also
                              enables mod_dav_fs.
      --enable-dav-lock       DAV provider for generic locking
      --enable-vhost-alias    mass virtual hosting module
      --enable-negotiation    content negotiation
      --disable-dir           directory request handling
      --enable-imagemap       server-side imagemaps
      --enable-actions        Action triggering on requests
      --enable-speling        correct common URL misspellings
      --enable-userdir        mapping of requests to user-specific directories
      --disable-alias         mapping of requests to different filesystem parts
      --enable-rewrite        rule based URL manipulation
      --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
      --with-apr=PATH         prefix for installed APR or the full path to
                                 apr-config
      --with-apr-util=PATH    prefix for installed APU or the full path to
                                 apu-config
      --with-pcre=PATH        Use external PCRE library
      --with-port=PORT        Port on which to listen (default is 80)
      --with-sslport=SSLPORT  Port on which to securelisten (default is 443)
      --with-distcache=PATH   Distcache installation directory
      --with-z=PATH           use a specific zlib library
      --with-libxml2=PATH     location for libxml2
      --with-brotli=PATH      Brotli installation directory
      --with-lua=PATH         Path to the Lua 5.3/5.2/5.1 prefix
      --with-ssl=PATH         OpenSSL installation directory
      --with-nghttp2=PATH     nghttp2 installation directory
      --with-mpm=MPM          Choose the process model for Apache to use by
                              default. MPM={event|worker|prefork|winnt} This will
                              be statically linked as the only available MPM
                              unless --enable-mpms-shared is also specified.
      --with-module=module-type:module-file
                              Enable module-file in the modules/<module-type>
                              directory.
      --with-program-name     alternate executable name
      --with-suexec-bin       Path to suexec binary
      --with-suexec-caller    User allowed to call SuExec
      --with-suexec-userdir   User subdirectory
      --with-suexec-docroot   SuExec root directory
      --with-suexec-uidmin    Minimal allowed UID
      --with-suexec-gidmin    Minimal allowed GID
      --with-suexec-logfile   Set the logfile
      --with-suexec-safepath  Set the safepath
      --with-suexec-umask     umask for suexec'd process
    
    Some influential environment variables:
      CC          C compiler command
      CFLAGS      C compiler flags
      LDFLAGS     linker flags, e.g. -L<lib dir> if you have libraries in a
                  nonstandard directory <lib dir>
      LIBS        libraries to pass to the linker, e.g. -l<library>
      CPPFLAGS    (Objective) C/C++ preprocessor flags, e.g. -I<include dir> if
                  you have headers in a nonstandard directory <include dir>
      CPP         C preprocessor
    
    Use these variables to override the choices made by `configure' or to help
    it to find libraries and programs with nonstandard names/locations.
    
    Report bugs to the package provider.
    [root@localhost httpd-2.4.29]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/apache --enable-mpms-shared=all --with-z --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-mpm=event
    [root@localhost httpd-2.4.29]# make && make install 

     模块动静态编译                            

    httpd是高度模块化的程序,各个功能通过加载各个模块来实现。但前提是将功能对应的模块先编译好,以供httpd加载。

    httpd对模块有两种编译方式:静态编译和动态编译。

    • 静态编译:将模块直接编译进httpd的核心中。静态编译的所有模块都会随着httpd的启动和启动。
    • 动态编译:将模块编译好,但不编译到httpd的核心中。要启动动态编译的模块,需要在httpd的配置文件中使用LoadModule指令加载。

    httpd的一个优点是可以实现动态模块的热插拔。因为httpd是独立于终端的守护进程,可以通过发送HUP信号给httpd让其重读配置文件。而是否加载动态编译模块正是由httpd配置文件中的LoadModule指令决定的。当想要加载某个模块A时(即模块热插),使用LoadModule指定A模块的链接地址,再发送HUP信号重读配置文件即可。而想要停止某个模块A时(即模块热拔),只需将对应模块的LoadModule指令行给注释,再重读配置文件即可。

    甚至,可以随时动态编译某个外部模块到httpd中,然后再热插。因为何时编译需要动态加载的模块对httpd来说是无关紧要的,它只需LoadModule和重读配置文件两个过程对模块进行控制。

    在编译选项中,有几种类型的选项:

    --disable-FEATURE:禁用某特性,等价于--enable-FEATURE=no
    --enable-FEATURE[=ARG]:启用某特性,默认参数值为YES
    --enable-Module_Name=shared:指定的模块Module_Name以动态编译方式安装
    --enable-Module_Name=static:指定的模块Module_Name以静态编译方式安装

    对于./configure --help中给定的选项,如果该选项是--disable的,那么表示该选项默认是启用的,需要显式使用--disable选项禁用;如果该选项是--enable的,那么表示该选项默认是禁用的,需要使用--enable选项来启用。例如:

    --disable-authz-user  :表示authz-user特性默认启用,编译时无需指定该项。如果要禁用,编译时需显式指定--disable-authz-user
    --enable-echo    :表示echo特性默认是禁用的,如果要启用,则编译时需显式指定--enable-echo

    模块名的书写是有规则的,一般模块的全称类似于"mod_BASENAME.so"格式,例如"mod_charset_lite.so",但在编译选项中指定模块时,只需指定BASENAME,且如果basename中包含下划线时,需要转换为短横线。例如"--enable-echo"表示编译的模块是"mod_echo.so"。

    此外,还支持3种列表方式的动静态编译选项:列表项之间使用空格分隔,但要使用单引号包围。

    --enable-modules='Module_Name1 Moduel_Name2'
    --enable-mods-shared='Module_Name1 Module_Name2'
    --enable-mods-statics='Module_Name1 Module_Name2'

    列表部分还可以使用关键字"all/few/most/reallyall"。分别表示编译所有、少量、大多数、真正的所有模块。

    "--enable-modules"基本等价于"--enable-mods-shared",都是动态编译给定列表中的模块,但"--enable-modules"可以额外使用一个关键字"none",表示不编译所有模块。

    动静态编译的优先级规则                          

    httpd动静态模块编译有一套规则,各种动静态便宜选项之间有优先级的存在。例如,某个非核心模块既指定了动态编译,同时又指定了静态编译,那到底是静态还是动态编译?

    以下是我总结的一些优先级规则。

    1. 不指定任何模块编译选项时,默认的选项为"--enable-mods-shared",而该选项的默认值又是most,所以等价于"--enable-mods-shared=most"。
    2. 显式指定要动态或静态编译的优先级最高。有以下几种方式显式指定:
    --enable-Module_Name=shared 
    --enable-Module_Name=static 
    --enable-mods-shared='Module_Name1 Module_Name2'
    --enable-mods-statics='Module_Name1 Module_Name2'
    --enable-modules='Module_Name1 Moduel_Name2'

    如果某个模块既显式指定了动态,又显式指定了静态编译,则静态编译优先级更高。例如:

    --enable-echo=shared
    --enable-echo=static

    那么,mod_echo模块将被静态编译。

    指定了关键字(all/most/few/reallyall)的"--enable-mods-static"选项,优先级高于指定或未指定关键字的"--enable-mods-shared"和"--enable-modules"选项,即静态关键字规则强于动态关键字规则。
    例如,下面两个编译配置中,都是"--enable-mods-static=few"生效。第二个编译配置语句中将忽略"--enable-mods-shared=all"。

    ./configure --prefix=/tmp/apache --enable-mods-static=few
    ./configure --prefix=/tmp/apache --enable-mods-static=few --enable-mods-shared=all

    对于下面的例子,authn-file和echo这两个模块既指定了动态编译又指定了静态编译,静态优先级高于动态,所以这两个模块静态被静态编译。由于没有使用关键字,所以会使用默认的"--enable-mods-shared=most"配置。即动态编译大部分,但指定的这两个模块被静态编译。

    ./configure --prefix=/tmp/apache 
    --enable-mods-static='authn-file echo' --enable-mods-shared='authn-file echo'

    而下面这个例子由于额外指定了使用"--enable-mods-static=few"选项,其优先级高于默认的"--enable-mods-shared=most",所以结果是静态编译few,且显式指定的两个模块也被静态编译。

    ./configure --prefix=/tmp/apache 
    --enable-mods-static='authn-file echo' 
    --enable-mods-shared='authn-file echo' 
    --enable-mods-static=few
    使用了关键字的"--enable-mods-static""--enable-mods-shared ""--enable-modules"的选项,隐含了"没有指定何种编译方式的模块"的默认编译方式。
    例如下面的编译配置,"--enable-mods-static"指定了关键字few,它将优先于默认的配置规则"--enable-mods-shared=most",所以没有指定编译方式的模块"data"将以静态的方式编译。
    ./configure --prefix=/tmp/apache --enable-mods-static=few --enable-data
    下面的配置如何编译的?由于默认的是"--enable-mods-shared=most"编译方式,所以模块"data"将以动态的方式编译。
    ./configure --prefix=/tmp/apache --enable-data
    再看下面的例子,配置中出现了"--enable-mods-static=few""--enable-mods-shared"(未给定值时也是默认为most),static的优先级高于shared,所以没有指定编译方式的模块"data"使用静态编译方式编译,而显式指定了编译方式的模块"echo"其优先级最强,所以动态编译"echo"。
    ./configure --prefix=/tmp/apache 
    --enable-mods-static=few --enable-mods-shared --enable-data --enable-echo=shared

     MPM的安装                          

    编译mpm模块(prefork/worker/event)和其他模块差不多,唯一的区别是必须至少编译一个mpm模块,且必须有且仅有一个加载被httpd加载。

    编译安装时默认的mpm是event模式(和发行版有关)。但可以通过"--with-mpm=MPM_NAME"来指定被加载的mpm模块。以下是几个相关编译选项:

    --with-mpm=MPM_Name:用于指定默认的mpm模块,它所指定的模块会被静态编译,并在httpd启动时加载。
    --enable-mpms-shared=MPM-LIST:指定动态编译安装的MPM列表,动态编译的MPM必须使用LoadModule指令加载才能使用。

    如果定"--with-mpm"选项指定了某个mpm,则默认该模块被静态编译,但如果同时使用"--enable-mpms-shared"指定了该mpm,则该mpm模块被动态编译。

    如果某个mpm模块被静态编译,在httpd启动时会加载它,如果想要切换到其他mpm模块,只有一种方法:重新编译httpd。

    而动态编译mpm模块时,则可以通过LoadModule来切换到其他mpm模块。由于编译时自带默认mpm模块,还可以使用"--with-mpm"指定默认mpm模块,所以动态编译mpm模块无疑比静态编译要好。

    "--enable-mpms-shared"可以指定动态编译的mpm列表,使用空格分隔,但需要使用单引号包围。还可以使用关键字"all"表示动态编译所有mpm模块。 例如:

    --enable-mpms-shared='prefork worker'
    --enable-mpms-shared=all

     关于"--enable-so"                              

    一个模块被动态编译,在需要加载的时候使用LoadModule指令指定该模块,并重读配置文件即可。但httpd为什么能加载该动态模块?这就是mod_so的能力。实际上,LoadModule和LoadFile指令就是该模块提供的。

    该选项使得httpd有加载某动态模块的能力(DSO,Dynamic Shared Object),也因此它只能使用静态编译方式随httpd启动被加载。只要不显式指定"--enable-so=shared"或者将其加入显式编译列表,它都会默认以静态方式编译。实际上,只要显式指定了动态方式编译该选项,编译时会报错。

    开始编译httpd                                

    至此,就可以开始编译httpd了。过程如下:

    cd
    tar xf httpd-2.4.27.tar.gz
    cd httpd-2.4.27
    ./configure --prefix=/usr/local/apache --sysconfdir=/etc/apache --with-z --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-mpm=event --enable-mpms-shared=all

    其中安装路径为/usr/local/apache,配置文件路径为/etc/apache。

    [root@localhost ~]# ls /usr/local/apache/ -l
    总用量 36
    drwxr-xr-x.  2 root root  262 12月 16 15:34 bin
    drwxr-xr-x.  2 root root  167 12月 16 15:35 build
    drwxr-xr-x.  2 root root   78 12月 16 15:34 cgi-bin
    drwxr-xr-x.  3 root root 4096 12月 16 15:34 error
    drwxr-xr-x.  2 root root   24 10月 18 2017 htdocs
    drwxr-xr-x.  3 root root 8192 12月 16 15:34 icons
    drwxr-xr-x.  2 root root 4096 12月 16 15:35 include
    drwxr-xr-x.  2 root root   58 12月 16 15:35 logs
    drwxr-xr-x.  4 root root   30 12月 16 15:35 man
    drwxr-xr-x. 14 root root 8192 10月 18 2017 manual
    drwxr-xr-x.  2 root root 4096 12月 16 15:34 modules

    bin目录为二进制程序存放位置,如启动脚本apachectl、httpd、htpasswd、ab(压力测试工具)等;htdocs目录存放网页文件,默认里面有index.html;logs目录存放了日志文件,除了日志文件,默认还有httpd运行的pid文件httpd.pid,这个建议修改到/var/run目录下(方便判断);modules存放了编译后的模块;man目录为帮助文档路径。

    使用httpd的启动脚本bin/apahcectl启动httpd,然后测试其是否正常。

    编译后的规范化操作                          

    设置man路径。

    [root@localhost ~]# echo "MANPATH /usr/local/apache/man" >>/etc/man.config

    设置PATH环境变量。

    [root@localhost ~]# echo 'PATH=/usr/local/apache/bin:$PATH' >/etc/profile.d/apache.sh
    [root@localhost ~]# source /etc/profile.d/apache.sh

    输出头文件

    [root@localhost ~]# ln -s /usr/include /usr/local/apache/include

    提供服务启动脚本。

    提供不提供没多大所谓,因为apachectl或httpd命令自身可以管理进程的启停,但自身管理启停时不提供lock文件。

    如果要提供的话,从yum安装的httpd提供的/usr/lib/systemd/system/httpd.service(systemd)或/etc/init.d/httpd(sysV)拷贝后稍作修改就可以了。以下是按照我上面编译的环境做了修改后的systemd和sysV服务管理脚本。

    以下是httpd的systemd服务管理脚本/usr/lib/systemd/system/httpd.service。

    [root@localhost ~]# vi /usr/lib/systemd/system/httpd.service
    
    [Unit]
    Description=The Apache HTTP Server
    After=network.target remote-fs.target nss-lookup.target
    Documentation=man:httpd(8)
    Documentation=man:apachectl(8)
    
    [Service]
    Type=notify
    EnvironmentFile=/etc/sysconfig/httpd
    ExecStart=/usr/local/apache/bin/httpd $OPTIONS -DFOREGROUND
    ExecReload=/usr/local/apache/bin/httpd $OPTIONS -k graceful
    ExecStop=/bin/kill -WINCH ${MAINPID}
    # We want systemd to give httpd some time to finish gracefully, but still want
    # it to kill httpd after TimeoutStopSec if something went wrong during the
    # graceful stop. Normally, Systemd sends SIGTERM signal right after the
    # ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
    # httpd time to finish.
    KillSignal=SIGCONT
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    ~

    说明:上面的脚本中使用了"kill -WINCH"信号,它是graceful stop的信号。如不明白,见我的另一篇文章:进程和信号

    以下是httpd的sysV服务管理脚本/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.
    #
    ######################################################################
    #  若httpd配置文件中指定了PidFile,则修改此脚本中的pidfile变量            #
    ######################################################################
    
    . /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/apache/bin/apachectl
    httpd=/usr/local/apache/bin/apachectl
    prog=httpd
    pidfile=/usr/local/apache/logs/httpd.pid
    lockfile=/var/lock/subsys/httpd
    RETVAL=0
    STOP_TIMEOUT=${STOP_TIMEOUT-10}
    config=/etc/apache/httpd.conf
    
    # 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 -f $config $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
  • 相关阅读:
    反射 动态导入 元类
    面向对象的继承
    面向对象基础总结
    面向对象基础
    包 logging hashlib copy模块
    os random sys json 模块
    【1012 | Day 43】前端之CSS(下)
    【1011 | Day 42】灵魂拷问:数据放在服务端和客户端的利与弊?
    【1011 | Day 42】socket实现ftp文件的上传和下载
    【1011 | Day 42】前端之CSS(上)
  • 原文地址:https://www.cnblogs.com/liujunjun/p/12049507.html
Copyright © 2011-2022 走看看