阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Linux下Nginx多版本PHP共存

129次阅读
没有评论

共计 3728 个字符,预计需要花费 10 分钟才能阅读完成。

应用环境

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 选项:

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=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--enable-fastcgi \
--enable-fpm
make 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-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

深入理解 PHP 中的 ini 配置 http://www.linuxidc.com/Linux/2016-02/128442.htm

剖析 PHP 脚本的超时机制 http://www.linuxidc.com/Linux/2016-02/128441.htm

Ubuntu 14.04 下搭建 PHP 开发环境 PDF  http://www.linuxidc.com/Linux/2016-02/128330.htm

PHP 7 革新与性能优化 http://www.linuxidc.com/Linux/2015-09/123136.htm

PHP 7,你值得拥有  http://www.linuxidc.com/Linux/2015-06/118847.htm 

在 CentOS 7.x / Fedora 21 上面体验 PHP 7.0  http://www.linuxidc.com/Linux/2015-05/117960.htm 

CentOS 6.3 安装 LNMP (PHP 5.4,MyySQL5.6) http://www.linuxidc.com/Linux/2013-04/82069.htm 

在部署 LNMP 的时候遇到 Nginx 启动失败的 2 个问题 http://www.linuxidc.com/Linux/2013-03/81120.htm 

Ubuntu 安装 Nginx php5-fpm MySQL(LNMP 环境搭建) http://www.linuxidc.com/Linux/2012-10/72458.htm 

《细说 PHP》高清扫描 PDF+ 光盘源码 + 全套教学视频 http://www.linuxidc.com/Linux/2014-03/97536.htm 

CentOS 6 中配置 PHP 的 LNMP 的开发环境  http://www.linuxidc.com/Linux/2013-12/93869.htm 

PHP 的详细介绍:请点这里
PHP 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-03/129693.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计3728字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中