linux下nginx多版本php共存__linux

來源:互聯網
上載者:User
應用環境

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.2cd ~/php5.2wget -c http://museum.php.net/php5/php-5.2.14.tar.gzwget -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.gzgzip -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選項:

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-fpmmake ZEND_EXTRA_LIBS='-liconv'make install
4.設定/usr/local/php-5.2.14/etc/php-fpm.conf,監聽連接埠:
<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-fpmchmod 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

參考資料: http://ixdba.blog.51cto.com/2895551/806622 http://www.yinfor.com/blog/archives/2008/05/install_zend_optimizer_33_on_c.html http://ideas.spkcn.com/technology/php-technology/133.html http://zhangxugg-163-com.iteye.com/blog/1894990
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.