当前位置: 首页>>技术问答>>正文


如何安装两个版本的PHP并轻松切换它们?

, , , ,

问题描述

我想在我的Ubuntu机器上安装PHP 5.2.17和PHP 5.3.5,并根据需要切换。我怎样才能做到这一点?

最佳解决方案

你可以使用一个PHP版本管理器来实现这一点。不同的版本管理器可用,如:

我最喜欢的是phpbrew。希望这可以帮助。

次佳解决方案

您可以一次运行2个不同的PHP版本,但并不像apt-getting那样简单。您需要运行一个单独安装的版本,并根据您的apache配置中的设置进行配置。

例如,您可以使用fastcgi完成此操作:基本上,您要查找的是您在this page上看到的配置。根据您所需的情况/端口/域,在您的配置中添加不同的处理程序。诀窍,安装两个版本后,就是这一步:

===从那个页面==

  1. The last step was to create virtual hosts. In the end I have three files in /etc/apache2/sites-enabled: 000-default, php5.3.17 and php5.4.7 With the following contents

默认:

    <VirtualHost *:80>
      ServerName localhost
      DocumentRoot /var/www
      <Directory "/var/www">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
        AddHandler php-cgi .php
        Action php-cgi /php-fcgi/php5317.fcgi
      </Directory>
    </VirtualHost>

php5.3.17:

    <VirtualHost *:80>
      ServerName 5317.localhost
      DocumentRoot /var/www
      <Directory "/var/www">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
        AddHandler php-cgi .php
        Action php-cgi /php-fcgi/php5317.fcgi
      </Directory>
    </VirtualHost>

php5.4.7:

    <VirtualHost *:80>
      ServerName 547.localhost
      DocumentRoot /var/www
      <Directory "/var/www">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
        AddHandler php-cgi .php
        Action php-cgi /php-fcgi/php547.fcgi
      </Directory>
    </VirtualHost>

请参阅完整安装the linked question。不要忘记看标题中的两个链接,它们看起来像漂亮的教程(但压缩比较少)。 ThisThat

(不要因为链接的问题是一个没有被接受的答案的大问题而放弃这个方法,该方法应该(并且确实)正常工作,但是用户忘记使用<?php并且使用<?关闭了短标签,参见评论)

第三种解决方案

我安装了PHP 5.6和7.0,但除了所有提示保持运行5.6,所以这个命令节省了一天(在我的情况下,我chosse选项1,并重新启动Apache):

sudo update-alternatives --config php

第四种方案

这个适用于我:https://lornajane.net/posts/2016/php-7-0-and-5-6-on-ubuntu

与”Growling Flea”中的脚本相同,但使用新版本。

Add the PPA

The PHP 5.6 and PHP 7.0 packages are from a third party PPA, not provided by the official Ubuntu repositories from Canonical. The PPAs I’m recommending here are from Ondřej Surý who packages PHP for Debian (which is then used by Ubuntu) so while it’s not an official repository, he’s not exactly random! The PPA itself is here

To add the PPA to your setup:

sudo add-apt-repository ppa:ondrej/php   

Then we’ll also want to grab information about what is on offer from this new PPA, so then run:

sudo apt-get update    

Install new PHP versions

I already had some of the php5 packages installed, but I didn’t uninstall anything, I just let apt work out what it wanted to do when I asked it to install the new versions:

sudo apt-get install php5.6 php7.0 

This resulted in a lot of complaining from apt and lots of conflicts. The first suggested resolution was to remove all the stock php5 packages so that PHP 5.6 could be installed – so I just accepted the first suggestion.

I use apache so this setup gave me apache with both php5.6 and php7.0 modules available, and the php5.6 module actually loaded.

As well as just the PHP itself, all the extensions and other tools you’d expect with PHP are there for both versions of PHP so it’s very easy to add in the modules that you need. I was very, very impressed with how nicely this is done.

Configuring and switching versions

Now you have two completely separate versions of PHP installed on your system, so let’s have a look at where all the pieces went!

The config files are all in /etc/php/5.6 and /etc/php/7.0 respectively – inside here is where you can configure which extensions are loaded, set the ini settings, and everything else for each version in isolation.

I’m an apache user, and as I mentioned both modules are available. So to switch from one to the other I need to do:

sudo a2dismod php5.6 sudo a2enmod php7.0 sudo service apache2 restart 

For nginx users, the changes are almost as easy, Digital Ocean have good documentation on this (they do have great docs!) so check out their guide: https://www.digitalocean.com/community/tutorials/how-to-upgrade-to-php-7-on-ubuntu-14-04 as it includes a section on reconfiguring nginx to use another version of PHP.

From the command-line, I have both php5.6 and php7.0 available as commands. I also still have a php command – look in /etc/alternatives to see that it symlinks to a particular version of PHP cli*. You can also quickly check which yours is using by running php -v.

&ast; more specifically, run which php to see which version of PHP is being used – but this will probably point to /usr/bin/php, which for me is itself a symlink to the /etc/alternatives/php command.

Working with extensions

This PPA comes with the usual php-pear package which offers the pecl command for both versions of PHP, so any extensions that are available via PECL can be installed in the usual way. You will also need the relevant headers so either php5.6-dev or php7.0-dev should be installed.

When the pecl installation completes, you’ll get a note to add the *.so file to your php.ini; in fact the best thing to do here is to look at what’s in /etc/php/mods-available. There will be some modules already here, each in its own file named after the extension and ending in .ini. You can copy one to use as a template or create your own and put all the relevant configuration for the extension in it (as a minimum, you need extension=[extensionName].so).

Once the new extension is in mods available, enable and then check it by doing:

sudo phpenmod extension   php -m   

This will create the symlinks in the right places for your current version of PHP to load this module, and you should see it in the list of modules output by the php -m. Pro tip: if you don’t see it in the output, scroll all the way to the top of the output and see if there are any useful error messages there.

第五种方案

From this post,我只是把命令和顺序解释为我服务(Ubuntu 12.04)

这种方法给你:

  1. 带菜单的脚本,允许在安装的php版本之间切换(不能同时)

  2. 您的服务器上安装了各种PHP版本

  3. 分离的conf文件

安装你需要的所有版本(我有两个)

安装依赖关系:

sudo apt-get install flex apache2-threaded-dev libxml2-dev apache2 apache2-mpm-prefork apache2-threaded-dev apache2-utils apache2.2-bin apache2.2-common

第一次安装PHP 5.3。下载php资源

md5sum Downloads/php-5.3.10.tar.bz2
mkdir ~/Sources
cd ~Sources/
cp -Rf ../Downloads/php-5.3.10.tar.bz2 .
tar xjf php-5.3.10.tar.bz2
cd php-5.3.10/
sudo mkdir /usr/local/php/php_5.3.10

第一次安装PHP5.6。下载php资源

md5sum Downloads/php-5.6.11.tar.bz2
mkdir ~/Sources
cd ~Sources/
cp -Rf ../Downloads/php-5.6.11.tar.bz2 .
tar xjf php-5.6.11.tar.bz2
cd php-5.6.11/
sudo mkdir /usr/local/php/php-5.6.11

脚本管理器版本PHP:创建一个名为php.sh的文件并将其放入/bin/中:

#!/bin/bash
opcion=0
cat << CABECERAMENU
Opciones del menu
1 => PHP 5.3.10
2 => PHP 5.6.11
CABECERAMENU
echo -n "Ingrese su eleccion: "
read opcion
echo
case $opcion in
    "1")
        rm /etc/apache2/php.conf
        ln -s /usr/local/php/php_5.3.10.conf /etc/apache2/php.conf
        /etc/init.d/apache2 restart
    ;;
    "2")
        rm /etc/apache2/php.conf
        ln -s /usr/local/php/php_5.6.11.conf /etc/apache2/php.conf
        /etc/init.d/apache2 restart
    ;;
    *)
        echo "Opcion no valida"
    ;;
esac

编译并安装php 5.3:

cd ~/Sources/php-5.3.10/
sudo ./configure --prefix=/usr/local/php/php_5.3.10 --with-config-file-path=/usr/local/php/php_5.3.10/lib --with-mysql --with-libdir=/lib/x86_64-linux-gnu --with-apxs2=/usr/bin/apxs2 --enable-zip --with-gd --with-curl --with-xmlrpc --with-freetype-dir=/usr/lib/x86_64-linux-gnu  --with-jpeg-dir=/usr/lib/x86_64-linux-gnu --with-pdo-mysql --with-pdo-pgsql --enable-soap
sudo make clean
sudo make
sudo ls -lhart /usr/lib/apache2/modules/libphp5.*
sudo rm -rf /usr/lib/apache2/modules/libphp5.*
sudo make install
sudo ls -lhart /usr/lib/apache2/modules/libphp5.*
sudo rm -rf /usr/local/php/php-5.3.10/modules/libphp5.so
sudo mv /usr/lib/apache2/modules/libphp5.so /usr/local/php/php_5.3.10/modules/
ls -lhart /usr/local/php/php_5.3.10/modules/
sudo a2dismod php5
sudo service apache2 restart

编译并安装php 5.6

cd ~/Sources/php-5.6.11
sudo ./configure --prefix=/usr/local/php/php_5.6.11 --with-config-file-path=/usr/local/php/php_5.6.11/lib --with-mysql --with-libdir=/lib/x86_64-linux-gnu --with-apxs2=/usr/bin/apxs2 --enable-zip --with-gd --with-curl --with-xmlrpc --enable-calendar --enable-sockets --with-freetype-dir=/usr/lib/x86_64-linux-gnu  --with-jpeg-dir=/usr/lib/x86_64-linux-gnu --with-pdo-mysql  --with-pdo-pgsql --enable-soap
sudo make clean
sudo make
sudo ls -lhart /usr/lib/apache2/modules/libphp5.*
sudo rm -rf /usr/lib/apache2/modules/libphp5.*
sudo make install
sudo ls -lhart /usr/lib/apache2/modules/libphp5.*
sudo rm -rf /usr/local/php/php_5.6.11/modules/libphp5.so
sudo mv /usr/lib/apache2/modules/libphp5.so /usr/local/php/php_5.6.11/modules
ls -lhart /usr/local/php/php_5.6.11/modules
sudo a2dismod php5
sudo service apache2 restart

让我们来使用它们:

$ sudo php.sh
Opciones del menu
1 => PHP 5.3.10
2 => PHP 5.6.11
Ingrese su eleccion: 1

 * Restarting web server apache2                                                                                                                                                                                                                                        apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
 ... waiting apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName

第六种方案

问题:

  1. 单一的apache实例不能同时运行两个不同的PHP版本(至少据我所知)。

  2. 两个不同的PHP版本不能从软件包安装,因为它们会发生冲突(并尝试覆盖相同的目录)。

解:

  • 对于b)从源代码编译“另一个”PHP版本,手动(或者如果您愿意,可以抓取相应软件包的源代码并修改它以避免冲突并修改安装路径)

  • 对于a)为不同的模块路径和单独的启动脚本创建单独的一组配置。当然还可以在单​​独的端口上运行。

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/987.html,未经允许,请勿转载。