本篇示範以原始碼編譯方式安裝 LAMP,並讓 Apache 與 nginx 並存。適用情境是 PHP 安裝在 /usr/ 底下(若為 /usr/local/ 路徑的 LNMP 請參考另一篇)。
Apache 依賴 apr 與 apr-util,兩個包都能在官網取得。
1. apr
tar xf apr-1.4.6.tar.bz2 cd apr-1.4.6 ./configure --help | less # 先看可用選項 ./configure --prefix=/usr/local/apr make && make install
2. apr-util
tar xf apr-util-1.5.2.tar.bz2 cd apr-util-1.5.2 ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr make && make install
3. httpd
tar xf httpd-2.4.4.tar.bz2 cd httpd-2.4.4 ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd \ --enable-so --enable-ssl --enable-cgi --enable-cgid \ --enable-modules=most --enable-mods-shared=most --enable-rewrite \ --with-zlib --with-pcre --with-apr=/usr/local/apr \ --with-apr-util=/usr/local/apr-util \ --enable-mpms-shared=all --with-mpm=event make && make install
幾個關鍵參數:--sysconfdir 決定設定檔位置;--enable-so 啟用動態共享模組,沒有它 PHP 無法與 Apache 結合;--enable-ssl 支援 https;--enable-cgi/cgid 支援 CGI;--enable-rewrite 提供 URL 重寫;--with-zlib 壓縮傳輸省頻寬;--enable-mpms-shared=all 與 --with-mpm=event 決定多道處理模組。
sudo /usr/local/apache/bin/apachectl start
沿用二進位安裝方式即可,步驟與一般 LNMP 相同。
先補齊相依套件:libbz2、libfreetype6-dev、libltdl-dev、libxml2-dev、libpng12-dev、libjpeg-dev、libmcrypt-dev、libcurl4-openssl-dev。
./configure --prefix=/usr/php --with-config-file-path=/usr/php/etc \ --with-config-file-scan-dir=/usr/php/lib/php \ --with-mcrypt --with-jpeg-dir --with-png-dir \ --with-mysql=/usr/local/mysql \ --with-pdo-mysql=/usr/local/mysql/bin/mysql_config \ --with-mysqli=/usr/local/mysql/bin/mysql_config \ --with-openssl --with-zlib --with-freetype-dir \ --enable-mbstring --enable-sockets --with-curl --with-gd \ --with-iconv-dir --with-pcre-dir --with-xmlrpc --enable-ftp \ --enable-gd-jis-conv --enable-gd-native-ttf --enable-soap \ --enable-mod-charset --enable-mysqlnd --enable-shmop \ --enable-zip --enable-static --enable-debug --enable-exif \ --enable-bcmath --enable-fpm \ --with-apxs2=/usr/local/apache/bin/apxs \ --with-bz2 --enable-maintainer-zts make && make install
cp php.ini-development /usr/php/etc/php.ini useradd --no-create-home --shell=/sbin/nologin www cd /usr/php/etc cp php-fpm.conf.default php-fpm.conf vim php-fpm.conf # 把 user、group 改為 www
Apache 啟動時出現這段訊息:
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
在設定檔加上 ServerName 127.0.0.1:80 即可消除。
另外提醒,本段落的 apr、apr-util、Apache 編譯參數與部分 PHP 設定參考自社群整理;不同版本套件名稱(如 libpng12-dev)在新版發行版可能已改名,遇到找不到時要換成對應的新套件名。