linux下nginx多版本php共存
阿新 • • 發佈:2019-02-02
應用環境
LNMP的環境,當前PHP版本5.3.8,遇到一個應用需求只支援PHP 5.2.x,又希望保持現有應用還是用PHP 5.3.8。也就是說需要兩個版本的PHP同時存在,供nginx根據需要呼叫不同版本。
思路
Nginx是通過PHP-FastCGI與PHP互動的。而PHP-FastCGI執行後會通過檔案、或本地埠兩種方式進行監聽,在Nginx中配置相應的FastCGI監聽埠或檔案即實現Nginx請求對PHP的解釋。
既然PHP-FastCGI是監聽埠和檔案的,那就可以讓不同版本的PHP-FastCGI同時執行,監聽不同的埠或檔案,Nginx中根據需求配置呼叫不同的PHP-FastCGI埠或檔案,即可實現不同版本PHP共存了。
配置記錄
下面記錄簡單的配置流程,基於已經安裝了lnmp的debian環境。當前版本的PHP是5.3.8,位於/usr/local/php。
1.下載PHP-5.2.14及相關的FPM、autoconf元件:
mkdir ~/php5.2
cd ~/php5.2
wget -c http://museum.php.net/php5/php-5.2.14.tar.gz
wget -c http://php-fpm.org/downloads/php-5.2.14-fpm-0.5.14.diff.gz
2.解壓PHP-5.2.14,並打上PHP-FPM的補丁:
tar zxvf php-5.2.14.tar.gz gzip -cd php-5.2.14-fpm-0.5.14.diff.gz | patch -d php-5.2.14 -p1
3.如果你已經通過lnmp安裝,應該已經安裝好了autoconf,如果沒有,請自行下載並編譯autoconf-2.13,然後設定autoconf環境變數:
export PHP_AUTOCONF=/usr/local/autoconf-2.13/bin/autoconf¬
export PHP_AUTOHEADER=/usr/local/autoconf-2.13/bin/autoheader
3.編譯安裝PHP-5.2.14在新的路徑(/usr/local/php-5.2.14)下,注意–prefix、–with-config-file-path的路徑,並且開啟fastcgi和fpm選項:
4.設定/usr/local/php-5.2.14/etc/php-fpm.conf,監聽埠:cd php-5.2.14/ ./buildconf --force ./configure --prefix=/usr/local/php-5.2.14 --with-config-file-path=/usr/local/php-5.2.14/etc --with-mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-fpm make ZEND_EXTRA_LIBS='-liconv' make install
<value name="listen_address">127.0.0.1:9001</value>
或者監聽檔案:<value name="listen_address">/path/to/unix/socket</value>
其他引數根據伺服器環境和需求自行定製。
5.啟動php-fpm,以後可以通過php-fpm進行管理:
/usr/local/php-5.2.14/sbin/php-fpm start
字php5.3.3後,php已經將php-fpm繼承到php中,而且內建的php-fpm預設不支援(start|stop|reload)的平滑啟動引數,需要使用官方原始碼中提供的啟動指令碼來控制:cp -f (php -5.3.x-source-dir)/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod 755 /etc/init.d/php-fpm
/etc/init.d/php-fpm start
php-fpm支援的操作:
- start,啟動PHP的FastCGI程序。
- stop,強制終止PHP的FastCGI程序。
- quit,平滑終止PHP的FastCGI程序。
- restart, 重啟PHP的FastCGI程序。
- reload, 重新載入PHP的php.ini。
- logrotate, 重新啟用log檔案。
5.3.3的php-fpm指令碼支援的操作:start|stop|force-quit|restart|reload|status
6.配置好PHP-5.2.14的php.ini,重新載入生效:
vi /usr/local/php-5.2.14/etc/php.ini
/usr/local/php-5.2.14/sbin/php-fpm reload
7.修改nginx配置,對需要的服務配置使用PHP-5.2.14:location ~ .*.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
include fcgi.conf;
}
8.記錄一下自己編譯php5.5.10使用的配置./configure --prefix=/usr/local/php-5.5.10 --with-config-file-path=/usr/local/php-5.5.10/etc --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-bz2 --with-curl=/usr/bin --enable-ftp --enable-sockets --disable-ipv6 --with-gd --with-jpeg-dir=/usr/local --with-png-dir=/usr/local --with-freetype-dir=/usr/local --enable-gd-native-ttf --with-iconv-dir=/usr/local --enable-mbstring --enable-calendar --with-gettext --with-libxml-dir=/usr/local --with-zlib --with-pdo-mysql=mysqlnd --enable-dom --enable-xml --enable-fpm --with-libdir=lib64 --with-mcrypt=/usr/bin --enable-zip --enable-soap --enable-mbstring --with-gd --with-openssl --enable-pcntl --with-xmlrpc --enable-opcache