編譯安裝Apache HTTP Server 2.4.23 以及配置HTTP/HTTPS反向代理

編譯安裝Apache HTTP Server 2.4.23
以及配置HTTP/HTTPS反向代理
一,依賴軟件:

1.1 GCC和C++編譯器

  • GCC
  • C++ Compiler

1.1.1 如果沒有安裝以上依賴包請執行以下命令安裝:

[root@xxx ~]# yum install -y gcc gcc-c++

1.1.2 如果已經安裝其中一個,可以單獨執行以下命令安裝其中一個:

[root@xxx ~]# yum install -y gcc
[root@xxx ~]# yum install -y gcc-c++

  • 如果沒有安裝GCC,在編譯其它依賴包的時候會遇到以下錯誤:

configure: error: no acceptable C compiler found in $PATH.

  • 如果沒有安裝C++ Compiler,在編譯其它依賴包的時候會遇到以下錯誤:

configure: error: you need a C++ compiler for C++ support.

1.2 APR,APR-Util和PCRE

  • Apache Portable Runtime (APR) 1.5.2 (大小807KB)
  • Apache Portable Runtime Utility (APR-Util) 1.5.4(大小678KB)
  • Perl-Compatible Regular Expressions Library (PCRE) 9.39 (大小1.48MB)
    如果沒有安裝以上依賴包請執行以下命令下載,服務器需要能夠訪問外網:
    [root@xxx ~]# wget http://mirrors.cnnic.cn/apache//apr/apr-1.5.2.tar.bz2
    [root@xxx ~]# wget http://mirrors.cnnic.cn/apache//apr/apr-util-1.5.4.tar.bz2
    [root@xxx ~]# wget http://ncu.dl.sourceforge.net/project/pcre/pcre/8.39/pcre-8.39.tar.bz2
    如果不能訪問外網,請另外下載然後上傳到服務
    1.2.1 解壓、編譯、安裝APR 1.5.2,先cd切換到壓縮包所在目錄:
    [root@xxx ~]# tar xvf apr-1.5.2.tar.bz2
    [root@xxx ~]# cd apr-1.5.2
    [root@xxx apr-1.5.2]# ./configure --prefix=/usr/local/apr
    [root@xxx apr-1.5.2]# make && make install
  • 參數--prefix爲指定安裝目錄(建議使用,下同),可以自行修改
    1.2.2 解壓、編譯、安裝APR-Util 1.5.4,先cd切換到壓縮包所在目錄:
    [root@xxx ~]# tar xvf apr-util-1.5.4.tar.bz2
    [root@xxx ~]# cd apr-util-1.5.4
    [root@xxx apr-util-1.5.4]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
    [root@xxx apr-util-1.5.4]# make && make install
  • 參數--prefix爲指定安裝目錄,可以自行修改
  • 參數--with-apr爲指定上述APR的安裝目錄
    1.2.3 解壓、編譯、安裝PCRE 9.39,先cd切換到壓縮包所在目錄:
    [root@xxx ~]# tar xvf pcre-8.39.tar.bz2 
    [root@xxx ~]# cd pcre-8.39
    [root@xxx pcre-8.39]# ./configure --prefix=/usr/local/pcre
    [root@xxx pcre-8.39]# make && make install
  • 參數--prefix爲指定安裝目錄,可以自行修改
    二,如果需要支持HTTPS,需要下載安裝OPENSSL,版本需要>=0.98,本例使用1.0.2版本(大小5.06MB):
    2.1 檢查系統是否已經安裝ssl還有版本:
    [root@xxx ~]# openssl version
    OpenSSL 1.0.0-fips 29 Mar 2010
    或者執行:
    [root@xxx ~]# rpm -qa|grep openssl
    openssl-1.0.0-20.el6_2.5.x86_64
    如果命令返回類似上述結果,證明系統已經安裝,如果沒有請參照下面步驟編譯安裝:
    [root@xxx ~]# wget https://www.openssl.org/source/old/1.0.2/openssl-1.0.2i.tar.gz
    如果不能訪問外網,請另外下載然後上傳到服務器
    2.2 解壓、編譯、安裝OPENSSL 1.0.2,先cd切換到壓縮包所在目錄:
    [root@xxx ~]# tar xvf openssl-1.0.2i.tar.gz
    [root@xxx ~]# cd openssl-1.0.2i
    [root@xxx openssl-1.0.2i]# ./config -fPIC --prefix=/usr/local/openssl --openssldir=/usr/local/openssl
    [root@xxx openssl-1.0.2i]# make && make install
  • 參數--prefix,--openssldir爲指定安裝目錄,可以自行修改
  • make && make install安裝步驟可能需要幾分鐘
    2.3 之前試過使用1.1.0b版本(openssl-1.1.0b.tar.gz),但是在編譯Apache HTTP Server 2.4.23的時候遇到以下錯誤,覺得是版本不兼容,所以使用openssl-1.0.2i版本:
    ab.c:2416: warning: implicit declaration of function 'CRYPTO_malloc_init'
    /usr/local/apr/build-1/libtool --silent --mode=link gcc -std=gnu99 -g -O2 -pthread......
    ab.o: In function main':<br/>httpd-2.4.23/support/ab.c:2416: undefined reference toCRYPTO_malloc_init'
    httpd-2.4.23/support/ab.c:2357: undefined reference to `SSLv2_client_method'
    三,下載Apache HTTP Server 2.4.23(大小6.05MB),服務器需要能夠訪問外網:
    [root@xxx ~]# wget http://mirrors.cnnic.cn/apache//httpd/httpd-2.4.23.tar.bz2
    如果不能訪問外網,請另外下載然後上傳到服務器
    3.1 解壓、編譯、安裝Apache HTTP Server 2.4.23,先cd切換到壓縮包所在目錄:
    [root@xxx ~]# tar xvf httpd-2.4.23.tar.bz2
    [root@xxx ~]# cd httpd-2.4.23
    [root@xxx httpd-2.4.23]# ./configure --prefix=/usr/local/apache2 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre=/usr/local/pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-ssl=/usr/local/openssl --enable-modules=most
    [root@xxx httpd-2.4.23]# make && make install
    --prefix:指定安裝目錄
    --with-pcre:pcre安裝目錄
    --with-apr:apr安裝目錄
    --with-apr-util:apr-util安裝目錄
    --enable-ssl:啓用ssl (如果不需要支持HTTPS,不需要添加此參數)
    --with-ssl:openssl安裝目錄 (如果不需要支持HTTPS,不需要添加此參數)
    其它參數爲可選的,看個人需求
    四,測試安裝是否成功
    4.1 修改ServerName
    打開/usr/local/apache2/conf/httpd.conf文件(/usr/local/apache2是httpd的安裝目錄),查找ServerName,格式爲以下所示,默認端口爲80:
    ServerName ip:80或者ServerName dns:80
    修改後保存
  • 如果不修改的話,啓動的時候有可能會出現以下錯誤:
    [root@xxx bin]# ./httpd -k start
    AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using xxx.xxx.xxx.xxx. Set the 'ServerName' directive globally to suppress this message
    4.2 綁定監聽的ip和訪問端口
    打開/usr/local/apache2/conf/httpd.conf文件(/usr/local/apache2是httpd的安裝目錄),查找Listen,格式爲以下所示,默認端口爲80,如果需要監聽其它端口,請修改:
    #Listen ip:port
    Listen 80
    4.3 啓動httpd
    進入到bin目錄,執行./httpd -k start
    [root@xxx bin]# ./httpd -k start
    httpd (pid 29502) already running
    如果出現類似上述結果證明啓動成功
    4.4 頁面驗證
    打開瀏覽器輸入服務器的ip或者域名(如果上面使用非80端口,需要輸入指定的端口),如果出現It works!證明啓動成功
    4.5 httpd重啓、停止命令
    ./httpd -k restart|stop
    五,修改默認啓動group/user
    httpd默認的啓動group/user都是daemon,如有需要可以修改,步驟如下:
    打開/usr/local/apache2/conf/httpd.conf文件(/usr/local/apache2是httpd的安裝目錄),查找User daemon,修改並保存,例如如果使用apache/apache啓動的話,替換daemon:
    User apache
    Group apache
  • 系統需要添加apache的group和user並且修改/usr/local/apache2的目錄權限
  • 啓動的時候使用root,httpd會自動切換,查看進程:
    [root@xxx bin]# ps -ef|grep httpd
    root      29502     1  0 09:40 ?        00:00:00 ./httpd -k start
    daemon   29503 29502  0 09:40 ?        00:00:00 ./httpd -k start
    daemon   29504 29502  0 09:40 ?        00:00:00 ./httpd -k start
    daemon   29505 29502  0 09:40 ?        00:00:00 ./httpd -k start
    root      31623 30134  0 10:20 pts/0     00:00:00 grep httpd
    六,配置反向代理轉發所有請求到應用服務器ls
    6.1 查看已經加載的proxy模塊,默認是沒有(視乎版本)
    [root@xxx bin]# ./httpd -M|grep proxy
    6.2 修改需要加載的proxy模塊
    打開/usr/local/apache2/conf/httpd.conf文件(/usr/local/apache2是httpd的安裝目錄),查找mod_proxy.so和mod_proxy_http.so,把開頭的#去掉:
    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_http_module modules/mod_proxy_http.s
    另外還需要加載Virtial hosts的配置,查找httpd-vhosts.conf,把開頭的#去掉:

    Virtual hosts

    Include conf/extra/httpd-vhosts.conf

  • 啓用Virtual hosts需要mod_log_config.so模塊,它是默認加載的,確認開頭是沒有#:
    LoadModule log_config_module modules/mod_log_config.so
    重啓
    [root@xxx bin]# ./httpd -k restart
    重新查看已經加載的proxy模塊,這個時候應該出現如下所示的2個模塊證明加載成功:
    [root@xxx bin]# ./httpd -M|grep proxy
     proxy_module (shared)
     proxy_http_module (shared)
    6.3 如果需要使用ssl,需要按照上述方法加載mod_ssl.so模塊:
    LoadModule ssl_module modules/mod_ssl.so
    以及Secure (SSL/TLS) connections配置,把開頭的#去掉:

    Secure (SSL/TLS) connections

    Include conf/extra/httpd-ssl.conf

  • 啓用Secure (SSL/TLS) connections需要以下模塊,確認開頭的#已經去掉:
    LoadModule log_config_module modules/mod_log_config.so
    LoadModule setenvif_module modules/mod_setenvif.so
    LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
    6.4 配置HTTP反向代理
    打開/usr/local/apache2/conf/extra/httpd-vhosts.conf文件(/usr/local/apache2是httpd的安裝目錄),修改節點<VirtualHost :80>:
    <VirtualHost
    :80>
        ServerAdmin [email protected]                              #管理員郵箱
        DocumentRoot "/usr/local/apache2/htdocs"  #應用文件根目錄,在安裝目錄下面
        ServerName xxx.com                                         #監聽的域名或者ip
        ErrorLog "logs/error.log"                                    #錯誤日誌相對路徑
        CustomLog "logs/access.log" common          #訪問日誌相對路徑
        ProxyVia Off                                                         #用於控制在http首部是否使用Via,off
        ProxyRequests Off                                              #是否開啓apache正向代理的功能,off
        ProxyPreserveHost On                                      #是否使用轉發,On
        ProxyPass / http://ip:port/                                   #監聽的路徑和轉發的路徑,根據實際情況修改
        ProxyPa***everse / http://ip:port/                   #需要輸入http://和最後面的/
    </VirtualHost>
  • 如果需要使用多個不同端口,可以配置多個<VirtualHost *:端口>節點
  • 修改任何conf文件保存後都必須重啓httpd
    6.5 配置HTTPS反向代理
    打開/usr/local/apache2/conf/extra/httpd-ssl.conf文件(/usr/local/apache2是httpd的安裝目錄),修改以下配置:

    監聽的HTTPS端口,默認是443

    Listen 443

    Inter-Process Session Cache:

    Configure the SSL Session Cache: First the mechanism 

    to use and second the expiring timeout (in seconds).

    SSLSessionCache        "shmcb:/usr/local/apache2/logs/ssl_scache(512000)"
    SSLSessionCacheTimeout  300

    SSL Virtual Host Context

    <VirtualHost default:443>                              #端口需要和上面一致
      DocumentRoot "/usr/local/apache2/htdocs" #應用文件根目錄,在安裝目錄下面
      ServerName www.xxx.com                             #監聽的域名或者ip
      ServerAdmin [email protected]                             #管理員郵箱
      ErrorLog "/usr/local/apache2/logs/ssl_error.log"            #錯誤日誌路徑
      TransferLog "/usr/local/apache2/logs/ssl_access.log"  #訪問日誌路徑
      SSLEngine on                                                                      #啓用SSLEngine
      SSLProxyEngine on                                                            #啓用SSLProxyEngine
      #如果使用RSA或DSA或ECC certificate的話也要一起列出
      SSLCertificateFile "/usr/local/apache2/conf/server.crt"     #Server Certificate證書路徑
      SSLCertificateKeyFile "/usr/local/apache2/conf/server.key"  #Server Private Key路徑
      #下面是可選的,如果有的話,需要去掉前面的#
      #SSLCertificateChainFile "/usr/local/apache2/conf/server-ca.crt"  #Server Certificate Chain
      #SSLCACertificatePath "/usr/local/apache2/conf/ssl.crt"           #Certificate Authority (CA)
      #Certificate Revocation Lists (CRL)
      #SSLCARevocationPath "/usr/local/apache2/conf/ssl.crl"
      #SSLCARevocationFile "/usr/local/apache2/conf/ssl.crl/ca-bundle.crl"
      #SSLCARevocationCheck chain
      #Client Authentication (Type):
      #SSLVerifyClient require
      #SSLVerifyDepth  10
      #TLS-SRP mutual authentication
      #SSLSRPVerifierFile "/usr/local/apache2/conf/passwd.srpv"
      #定製化格式日誌
      CustomLog "/usr/local/apache2/logs/ssl_request.log" \
              "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
      #監聽的路徑和轉發的路徑,根據實際情況修改,需要輸入https://和最後面的/
      ProxyRequests Off
      ProxyPass / https://ip:443/                
      ProxyPa***everse / https://ip:443/             
    </VirtualHost>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章