最近一個專案裡面,客戶使用 CentOS 8 作為網站的系統,由於 CentOS 8 裡面內建的 Apache (2.4.37) 與 OpenSSL (1.1.1) 版本並非最新版本,且網路上尚未找到有善心人士做好 CentOS 8 Apache 的儲存庫 (CodeIt 目前只有 CentOS 7),加上客戶用 NESSUS 做系統掃描,掃出 OpenSSL 版本問題,所以只好自己編譯安裝。

下載檔案

wget https://github.com/openssl/openssl/archive/OpenSSL_1_1_1d.tar.gz
wget https://www.apache.org/dist/httpd/httpd-2.4.41.tar.gz
wget https://www.apache.org/dist/apr/apr-util-1.6.1.tar.gz
wget https://www.apache.org/dist/apr/apr-1.7.0.tar.gz
wget https://www.php.net/distributions/php-7.3.13.tar.gz
wget https://libzip.org/download/libzip-1.5.2.tar.gz

安裝 OpenSSL

編譯安裝必須自己 compile 所以需要安裝 make 套件, 檢查並安裝相關依賴 (CentOS預設沒有裝make)。

yum -y install perl perl-devel gcc gcc-c++ automake autoconf libtool make

解壓縮下載回來的檔案,並切換到 OpenSSL_1_1_1d 目錄。

tar xzvf ./OpenSSL_1_1_1d.tar.gz && cd openssl-OpenSSL_1_1_1d

執行編譯

./config --prefix=/usr/local/openssl/ shared enable-shared zlib
make && make install && make clean

設定

在 /etc/ld.so.conf 文件的最后面,添加如下内容:/usr/local/openssl/lib ,然後執行 ldconfig。
執行完後,在 /etc/profile 加入 openssl 環境變數:

export OPENSSL=/usr/local/openssl/bin
export PATH=$OPENSSL:$PATH:$HOME/bin

執行 openssl version 檢查版本。

注意! 編譯安裝 OpenSSL 此方法造成 yum 發生錯誤,解決方法就是移除 /etc/ld.so.conf 裡 /usr/local/openssl/lib,重新執行 ldconfig 即可,或者使用 yum 安裝舊版本 Openssl。
安裝新版 OpenSSL 主要是為了 lib 可以編譯到 apache 與 php 裡,安裝完 apache 與 php 即可將其關閉。

安裝 Apache

安裝相關依賴包

httpd 依賴包

yum -y install pcre-devel
yum -y groupinstall "Development Tools"

arp-util依賴包

yum install expat-devel

安裝 apr-1.7.0

解壓縮 apr-1.7.0 及切換目錄

tar xvf apr-1.7.0.tar.gz
cd apr-1.7.0

tar xvf apr-1.7.0.tar.gz && cd apr-1.7.0

編譯安裝 apr-1.7.0

./configure -prefix=/usr/local/apr
make && make install && make clean

安裝 apr-util-1.6.1

解壓縮 apr-util-1.6.1 及切換目錄

tar xvf apr-util-1.6.1.tar.gz
cd apr-util-1.6.1

編譯安裝 apr-util-1.6.1

./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
make && make install

安裝 httpd-2.4.41

解壓縮 httpd-2.4.41 及切換目錄

tar xvf httpd-2.4.41.tar.gz
cd httpd-2.4.41

編譯安裝 httpd-2.4.41

./configure --prefix=/usr/local/apache --sysconfdir=/usr/local/apache/conf --enable-so --enable-ssl --enable-cgi --enable-rewrite --enable-mods-shared=all --with-ssl=/usr/local/openssl --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=all --enable-mpms-shared=all --with-mpm=event
make && make install && make clean

設定參數說明

  • –enable-so #支援動態共用模組,如果沒有此功能,php 無法與 apache 一起工作,必須安裝
  • –enable-rewrite #支持 url 重寫
  • –enable-cgi #支持 cgi
  • –enable-pcre #支持pcre
  • –enable-ssl #啟用 ssl 功能,不安裝無法啟用 https
  • –enable-mods-shared=all #編譯所有模組
  • –with-apr=/usr/local/apr #指定apr庫檔路徑
  • –with-apr-util=/usr/local/apr-util/ #指定apr-util庫檔路徑
  • –with-ssl=/usr/local/openssl #指定openssl庫檔路徑
  • –with-mpm=worker #指定apache工作模式
  • 後面加上 shared 開啟所有的共用(動態)編譯模式,如果不加shared,則為靜態編譯模式

執行 httpd

/usr/local/apache/bin/apachectl start

設定 httpd 自動執行,複製 /usr/local/apache/bin/apachectl 檔案至 /etc/init.d/ 並命名為 httpd 並編輯它。註: 如果有其他的版本的Apache存在,也可以直接覆蓋掉。

cp /usr/local/apache/bin/apachectl /etc/init.d/httpd
vi /etc/init.d/httpd

在第一行#!/bin/sh下增加兩行文字如下 (包含 # 符號)

# chkconfig: 35 70 30
# description: Apache

註冊該服務並確認服務是否存在

chkconfig --add httpd
chkconfig --level 345 httpd on
chkconfig --list

開啟防火牆設定

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

安裝 PHP

安裝依賴包 (排除openssl 及 libzip 另外設定)

yum install -y gcc gcc-c++ make zlib zlib-devel pcre pcre-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel  nss_ldap libcurl libcurl-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel

安裝 libzip

在作設定時若發生 libzip 錯誤則需手動安裝 libzip。

解壓縮 libzip-1.5.2 及切換目錄

tar -zxf libzip-1.5.2.tar.gz && cd libzip-1.5.2

編譯安裝 libzip-1.5.2


mkdir build && cd build && cmake -DCMAKE_INSTALL_PREFIX=/usr .. && make && make install

如果提示 cmake: command not found,需要先安裝 cmake,執行 yum -y install cmake 安裝 cmake

安裝 PHP

設定及編譯安裝

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-apxs2=/usr/local/apache/bin/apxs --with-openssl --with-openssl-dir=/usr/local/openssl --enable-fpm --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-soap --with-libxml-dir --with-xmlrpc --with-pcre-regex --with-zlib --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --with-cdb --enable-dom --enable-exif --enable-fileinfo --enable-filter --with-pcre-dir --enable-ftp --with-gd --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype-dir --enable-gd-jis-conv --with-gettext --with-gmp --with-mhash --enable-json --enable-mbstring --enable-mbregex --enable-mbregex-backtrack --with-onig --enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-pdo-sqlite --with-readline --enable-session --enable-shmop --enable-simplexml --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --with-xsl --enable-zip --enable-mysqlnd-compression-support --with-pear --enable-opcache
make && make install
libtool --finish /root/php-7.3.12/libs

複製及編輯設定檔案

cp php.ini* /usr/local/php/etc

修改 httpd.conf,在 IfModule mime_module 區間增加

<IfModule mime_module>
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps
</IfModule mime_module>

檢查 httpd.conf 檔案裡面 modules 是否有打開

LoadModule rewrite_module modules/mod_rewrite.so
LoadModule php7_module modules/libphp7.so

執行 /usr/local/apache/bin/apachectl restart 重新啟動 httpd 並檢查 php 是否正確執行

最後修改日期: 2019 年 12 月 19 日