如何在开发LAMP服务器上运行多个版本的PHP 5.x?

我需要用多个版本的PHP 5.x来testing我的PHP应用程序,例如PHP 5.0.0和PHP 5.2.8。

有没有一种方法可以configuration开发LAMP服务器,以便我可以快速testing多个版本的PHP5应用程序?

一个办法是让你的主要版本的PHP设置为mod_php,并通过快速CGI在不同的端口(即81,82,83等)运行所有其他的。 这并不能保证完全一致的行为。

使用CentOS,你可以使用fastcgi的一个版本的PHP和php-fpm的组合,如下所述:

https://web.archive.org/web/20130707085630/http://linuxplayer.org/2011/05/intall-multiple-version-of-php-on-one-server

基于CentOS 5.6,仅适用于Apache

1.启用rpmforge和epel yum版本库

wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm wget http://download.fedora.redhat.com/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm sudo rpm -ivh rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm sudo rpm -ivh epel-release-5-4.noarch.rpm 

2.安装php-5.1

CentOS / RHEL 5.x系列在php-5.1的框中,只需安装yum,例如:

 sudo yum install php php-mysql php-mbstring php-mcrypt 

3.从源代码编译并安装php 5.2和5.3

对于PHP 5.2和5.3,我们可以在Internet上find很多rpm包。 但是,它们都与CentOS提供的php相冲突,所以我们最好从源代码构build和安装它们,这并不难,重点是在不同的位置安装php。

但是,当安装php作为apache模块时,我们只能同时使用一个版本的php。 如果我们需要在同一台服务器上运行不同版本的PHP,例如,不同的虚拟主机可能需要不同版本的php。 幸运的是,酷的FastCGI和PHP-FPM可以提供帮助。

build立并安装php-5.2并启用fastcgi

1)安装所需的开发包

 yum install gcc libxml2-devel bzip2-devel zlib-devel \ curl-devel libmcrypt-devel libjpeg-devel \ libpng-devel gd-devel mysql-devel 

2)编译并安装

 wget http://cn.php.net/get/php-5.2.17.tar.bz2/from/this/mirror tar -xjf php-5.2.17.tar.bz2 cd php-5.2.17 ./configure --prefix=/usr/local/php52 \ --with-config-file-path=/etc/php52 \ --with-config-file-scan-dir=/etc/php52/php.d \ --with-libdir=lib64 \ --with-mysql \ --with-mysqli \ --enable-fastcgi \ --enable-force-cgi-redirect \ --enable-mbstring \ --disable-debug \ --disable-rpath \ --with-bz2 \ --with-curl \ --with-gettext \ --with-iconv \ --with-openssl \ --with-gd \ --with-mcrypt \ --with-pcre-regex \ --with-zlib make -j4 > /dev/null sudo make install sudo mkdir /etc/php52 sudo cp php.ini-recommended /etc/php52/php.ini 

3)创build一个fastcgi包装脚本

创build文件/usr/local/php52/bin/fcgiwrapper.sh

 #!/bin/bash PHP_FCGI_MAX_REQUESTS=10000 export PHP_FCGI_MAX_REQUESTS exec /usr/local/php52/bin/php-cgi chmod a+x /usr/local/php52/bin/fcgiwrapper.sh Build and install php-5.3 with fpm enabled wget http://cn.php.net/get/php-5.3.6.tar.bz2/from/this/mirror tar -xjf php-5.3.6.tar.bz2 cd php-5.3.6 ./configure --prefix=/usr/local/php53 \ --with-config-file-path=/etc/php53 \ --with-config-file-scan-dir=/etc/php53/php.d \ --enable-fpm \ --with-fpm-user=apache \ --with-fpm-group=apache \ --with-libdir=lib64 \ --with-mysql \ --with-mysqli \ --enable-mbstring \ --disable-debug \ --disable-rpath \ --with-bz2 \ --with-curl \ --with-gettext \ --with-iconv \ --with-openssl \ --with-gd \ --with-mcrypt \ --with-pcre-regex \ --with-zlib make -j4 && sudo make install sudo mkdir /etc/php53 sudo cp php.ini-production /etc/php53/php.ini sed -i -e 's#php_fpm_CONF=\${prefix}/etc/php-fpm.conf#php_fpm_CONF=/etc/php53/php-fpm.conf#' \ sapi/fpm/init.d.php-fpm sudo cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm sudo chmod a+x /etc/init.d/php-fpm sudo /sbin/chkconfig --add php-fpm sudo /sbin/chkconfig php-fpm on sudo cp sapi/fpm/php-fpm.conf /etc/php53/ 

configurationphp-fpm

编辑/etc/php53/php-fpm.conf,更改一些设置。 这一步主要是取消一些设置的注释,如果你喜欢,可以调整值。

 pid = run/php-fpm.pid listen = 127.0.0.1:9000 pm.start_servers = 10 pm.min_spare_servers = 5 pm.max_spare_servers = 20 

然后,启动fpm

 sudo /etc/init.d/php-fpm start 

安装并设置mod_fastcgi,mod_fcgid

 sudo yum install libtool httpd-devel apr-devel wget http://www.fastcgi.com/dist/mod_fastcgi-current.tar.gz tar -xzf mod_fastcgi-current.tar.gz cd mod_fastcgi-2.4.6 cp Makefile.AP2 Makefile sudo make top_dir=/usr/lib64/httpd/ install sudo sh -c "echo 'LoadModule fastcgi_module modules/mod_fastcgi.so' > /etc/httpd/conf.d/mod_fastcgi.conf" yum install mod_fcgid 

设置和testing虚拟主机

1)将以下行添加到/ etc / hosts

 127.0.0.1 web1.example.com web2.example.com web3.example.com 

2)创buildWeb文档根目录下放一个index.php,显示phpinfo切换到root用户,运行

 mkdir /var/www/fcgi-bin for i in {1..3}; do web_root=/var/www/web$i mkdir $web_root echo "<?php phpinfo(); ?>" > $web_root/index.php done 

注意:需要清空/ var / www / fcgi-bin目录,请勿删除它

3)创buildApacheconfiguration文件(附加到httpd.conf)

 NameVirtualHost *:80 # module settings # mod_fcgid <IfModule mod_fcgid.c> idletimeout 3600 processlifetime 7200 maxprocesscount 17 maxrequestsperprocess 16 ipcconnecttimeout 60 ipccommtimeout 90 </IfModule> # mod_fastcgi with php-fpm <IfModule mod_fastcgi.c> FastCgiExternalServer /var/www/fcgi-bin/php-fpm -host 127.0.0.1:9000 </IfModule> # virtual hosts... ################################################################# #1st virtual host, use mod_php, run php-5.1 ################################################################# <VirtualHost *:80> ServerName web1.example.com DocumentRoot "/var/www/web1" <ifmodule mod_php5.c> <FilesMatch \.php$> AddHandler php5-script .php </FilesMatch> </IfModule> <Directory "/var/www/web1"> DirectoryIndex index.php index.html index.htm Options -Indexes FollowSymLinks Order allow,deny Allow from all </Directory> </VirtualHost> ################################################################# #2nd virtual host, use mod_fcgid, run php-5.2 ################################################################# <VirtualHost *:80> ServerName web2.example.com DocumentRoot "/var/www/web2" <IfModule mod_fcgid.c> AddHandler fcgid-script .php FCGIWrapper /usr/local/php52/bin/fcgiwrapper.sh </IfModule> <Directory "/var/www/web2"> DirectoryIndex index.php index.html index.htm Options -Indexes FollowSymLinks +ExecCGI Order allow,deny Allow from all </Directory> </VirtualHost> ################################################################# #3rd virtual host, use mod_fastcgi + php-fpm, run php-5.3 ################################################################# <VirtualHost *:80> ServerName web3.example.com DocumentRoot "/var/www/web3" <IfModule mod_fastcgi.c> ScriptAlias /fcgi-bin/ /var/www/fcgi-bin/ AddHandler php5-fastcgi .php Action php5-fastcgi /fcgi-bin/php-fpm </IfModule> <Directory "/var/www/web3"> DirectoryIndex index.php index.html index.htm Options -Indexes FollowSymLinks +ExecCGI Order allow,deny Allow from all </Directory> </VirtualHost> 

4)重新启动Apache。 请访问3个站点查看phpinfo并validation结果。 即:

 http://web1.example.com http://web2.example.com http://web3.example.com 

如果一切正常,您可以使用3虚拟主机之一作为模板来创build新的虚拟主机,具有所需的PHP版本。

有多个Apache + PHP的实例从来没有真正痒痒我的幻想,但它可能是最简单的方法来做到这一点。 如果你不喜欢KISS …这是一个想法。

让你的Apache启动并运行,并尝试configuration它像debian和Ubuntu做到这一点,例如,有加载模块的目录。 你的apache conf可以使用这样的行:

 Include /etc/apache2/mods-enabled/*.load Include /etc/apache2/mods-enabled/*.conf 

然后build立你的第一个版本的PHP,并给它一个明确包含版本号的前缀,例如/usr/local/php/5.2.8,/usr/local/php/5.2.6 …

conf / load看起来像这样:

php5.2.6.load

 LoadModule php5_module /usr/local/php/5.2.6/libphp5.so 

php5.2.8.load

 LoadModule php5_module /usr/local/php/5.2.8/libphp5.so 

要切换版本,你所要做的就是改变apache目录下的加载和configuration文件。 你可以用一个简单的bash脚本自动化(删除实际的文件,复制备用版本文件,并重新启动apache。

这种设置的一个优点是一切都是consitent,只要你保持php.ini的选项和模块(你将不得不做的CGI)相同。 他们都在通过SAPI。 您的应用程序不需要任何更改,也不需要使用相对URL。

认为这应该起作用,但是再一次,我没有尝试过,也不可能这样做,因为我没有和你一样的要求。 如果你曾经尝试过,请做评论。

注意:以下方法将在Windows上工作。

另一种方法(如果一次运行单个版本的PHP也可以)定义多个Apache服务,每个Apache服务将使用不同的PHP版本。

首先在Apacheconfiguration文件中使用条件:

  <ifdefine php54> SetEnv PHPRC C:/apache/php54/ ScriptAlias /php/ "C:/apache/php54/" AddType application/x-httpd-php .php Action application/x-httpd-php "/php/php-cgi.exe" </ifdefine> <ifdefine php55> SetEnv PHPRC C:/apache/php55/ ScriptAlias /php/ "C:/apache/php55/" AddType application/x-httpd-php .php Action application/x-httpd-php "/php/php-cgi.exe" </ifdefine> 

现在使用httpd.exe从命令行(提升到pipe理员)创build两个单独的服务:

 httpd.exe -k install -n Apache224_php54 -D php54 httpd.exe -k install -n Apache224_php55 -D php55 

现在,您可以一次启动上述服务之一(在启动另一项服务之前应先closures一项服务)。

如果您之前已经将Apache安装为服务,则可以使用以下命令删除该服务(将服务名称replace为您使用的名称):

 apache -k uninstall -n Apache224 

还有一点需要注意的是,我个人使用了一个名为“Seobiseu”的“通知区域图标程序”来根据需要启动和停止服务。 我已经添加了上述两个服务。

要知道你可能正在谈论一个本地/台式机器,并可能会继续谈论本地/桌面机器,我会给你一个替代scheme,以防万一它可能帮助你或其他人:

在云中设置多个虚拟服务器实例,并在他们之间分享你的代码作为一个git仓库(或者说,我想,尽pipe我没有个人经验,你真正需要的是分散的东西)。 这样做的好处是可以让您尽可能地接近生产体验,如果您有设置服务器的经验,那么就不是那么复杂(或者是昂贵的,如果您只想旋转服务器,做你需要做的事情,然后再旋转下来,然后你说几分钱,说50美分,如果你只是让它跑了几美元。

我现在在云中完成了我所有的项目开发工作,而且我发现pipe理基础架构要比使用本地/非虚拟化安装时要简单得多,而且这种并行scheme也是公平的直截了当。 如果你没有考虑到,我只是想把它放在那里。

我刚刚在Ubuntu 10上从PHP5.3成功降级。

为此,我使用了以下脚本:

 #! /bin/sh php_packages=`dpkg -l | grep php | awk '{print $2}'` sudo apt-get remove $php_packages sed s/lucid/karmic/g /etc/apt/sources.list | sudo tee /etc/apt/sources.list.d/karmic.list sudo mkdir -p /etc/apt/preferences.d/ for package in $php_packages; do echo "Package: $package Pin: release a=karmic Pin-Priority: 991 " | sudo tee -a /etc/apt/preferences.d/php done sudo apt-get update sudo apt-get install $php_packages 

对于任何不知道如何从命令行运行脚本的人,下面是一个简短的教程:

 1. cd ~/ 2. mkdir bin 3. sudo nano ~/bin/myscriptname.sh 4. paste in the script code I have posted above this 5. ctrl+x (this exits and prompts for you to save) 6. chmod u+x myscriptname.sh 

这6个步骤在主目录中的名为“bin”的文件夹中创build一个脚本。 然后您可以通过调用以下命令来运行此脚本:

 ~/bin/myscriptname.sh 

Oulia!

希望这有助于你们中的一些人!

作为参考,这里是我得到的脚本: Ubuntu5.0的PHP5.2.10

那里有好几个人都在证实这是有效的,这对我来说是一种享受。

创buildPHP的Rasmus Lerdorf正在维护一个主动的Vagrant解决scheme,似乎可以解决您的需求。 它允许在PHP版本之间快速切换,目前支持20多个不同的版本。 它带有一个nginx服务器,但可以通过预configuration的设置轻松切换到apache2。 它也支持MySQL的开箱即用。

通过这种方式,您将可以访问所有PHP版本,可以在两台主要的Web服务器上部署,在一个漂亮的stream浪盒中,由PHP后面的大个子维护。

欲了解更多信息,我想参考先生的讲话。 Lerdorf在https://youtu.be/6XnysJAyThs?t=2864

包含Vagrant解决scheme的github存储库位于https://github.com/rlerdorf/php7dev

这是一个链接,详细的程序 。 这是我遇到的最好的。 目前,我已经成功地使用了PHP 5.4作为旧项目,使用PHP 5.6作为新项目,在HHVM和PHP 7上testing了Wordpress。我花了一些时间来设置,有很多热门和试用版,但现在运行起来很顺利。 我的下一个方法是学会在docker工作。

为了testing,我只是在不同的IP地址上运行多个httpd实例,所以我的php7运行在192.168.0.70和php5.6上运行192.168.0.56。 在生产中,我有一个网站运行一个老的oscommerce运行php5.3,我只是有一个不同的conf文件的网站

httpd -f /etc/apache2/php70.conf httpd -f /etc/apache2/php53.conf

这也是一个干净的方式,为不同的网站有不同的php.ini文件。 如果你只是有一些网站,如果一个很好的方式来保持组织,你不必担心更多的1站点,当你升级的东西

我有几个项目在我的箱子上运行。 如果你已经安装了多个版本,这个bash脚本应该可以帮助你轻松切换。 目前,我有php5,php5.6和php7.0,我经常根据我正在工作的项目来回切换。 这是我的代码。

随意复制。 确保你了解代码的工作原理。 这是为webhostin。 我的本地盒子我的mods存储在/ etc / apache2 / mods-enabled /

  #!/bin/bash # This file is for switching php versions. # To run this file you must use bash, not sh # # OS: Ubuntu 14.04 but should work on any linux # Example: bash phpswitch.sh 7.0 # Written by Daniel Pflieger # growlingflea at g mail dot com NEWVERSION=$1 #this is the git directory target #get the active php enabled mod by getting the array of files and store #it to a variable VAR=$(ls /etc/apache2/mods-enabled/php*) #parse the returned variables and get the version of php that is active. IFS=' ' read -r -a array <<< "$VAR" array[0]=${array[0]#*php} array[0]=${array[0]%.conf} #confirm that the newversion veriable isn't empty.. if it is tell user #current version and exit if [ "$NEWVERSION" = "" ]; then echo current version is ${array[0]}. To change version please use argument exit 1 fi OLDVERSION=${array[0]} #confirm to the user this is what they want to do echo "Update php" ${OLDVERSION} to ${NEWVERSION} #give the user the opportunity to use CTRL-C to exit ot just hit return read x #call a2dismod function: this deactivate the current php version sudo a2dismod php${OLDVERSION} #call the a2enmod version. This enables the new mode sudo a2enmod php${NEWVERSION} echo "Restart service??" read x #restart apache sudo service apache2 restart