Simple implementation of PHP5 and PHP7 versions supported in Ubuntu

  • 2021-10-27 06:44:46
  • OfStack

Preface

Recently, when writing a tool, I used the PHP namespace feature. If I want to reference constants and functions in the namespace, I need PHP 5.6 or above, but the version installed on Alibaba Cloud ECS is PHP 5.5. 9. Because ECS deploys a lot of PHP code, if I rashly upgrade the higher version PHP, there may be some compatibility problems. Suddenly, I wonder if I can support two versions of PHP in the same operating system.

Simply consulted the relevant information, in fact, it is very simple to support the double version of PHP by using the package installation method in Ubuntu, and the two versions can exist without interference with each other. Next, it is briefly introduced, but there are many related articles.

1: Get the PHP7 source

My operating system version is Ubuntu 14.04.5 LTS, and the default PHP source is version 5.5. 9, which can be confirmed using the following command:


$ apt show php

To get the various versions of the PHP source,

The official PPA in Ubuntu contains many software sources. In order to support PPA package, you can use add-apt-repository command line installation. If this tool is not installed, you can run the following command installation:


$ apt install python-software-properties

A simple understanding of the add-apt-repository tool serves as follows:

Adds a repository into the /etc/apt/sources.list or /etc/apt/sources.list.d or removes an existing one

That is to say, you can add sources through this tool without manually modification/etc/apt/sources. list.

To support multiple versions of the source of PHP, run the following command:


$ add-apt-repository ppa:ondrej/php

After the run is complete, the file/etc/apt/sources. list. d/ondrej-php-trusty. list is actually updated.

2: Update the system

After adding the source, you need to run the following command to update the system before installing each version of PHP.


$ apt-get update -y

3: Install PHP 7.1

Let's see how many versions of PHP can be installed at present.


$ apt-cache pkgnames | grep php7

The key outputs are as follows:

php7.0-fpm
php7.1-fpm
php7.2-fpm
libapache2-mod-php7.0
libapache2-mod-php7.1
libapache2-mod-php7.2

That is to say, two kinds of SAPI are supported. I mainly use Nginx+FPM, and I can see that three versions of PHP7 are currently supported.

I mainly want to use the command line PHP7 version, and by the way, I also want to install FPM. Run the following command for details:


$ apt-cache depends php7.1-fpm

 Depends: php7.1-cli
 Depends: php7.1-common
 Depends: php7.1-json
 Depends: php7.1-opcache

You can see that php7.1-fpm also includes the command line PHP (php7.1-cli). Next, install:


$ apt-get install php7.1-fpm php7.1-curl

3: Observe the files after installation

Run the following command to see which files are installed in php 7.1-fpm.


$ dpkg -L php7.1-fpm

The key outputs are as follows:

/usr/sbin/php-fpm7.1
/etc/php/7.1/fpm/php-fpm.conf
/etc/apache2/conf-available/php7.1-fpm.conf

As you can see, you can run an PHP7 version of FPM service, and the configuration files used by/etc/php5/fpm/php-fpm. conf are isolated from each other. My websites www. simplehttps. com and blog. simplehttps. com use two versions of FPM.

Next, see what files are installed in php 7.1-cli, which is my biggest concern.


$ dpkg -L php7.1-cli

The key outputs are as follows:

/usr/bin/php7.1

4: How to Switch Between PHP Versions

For the command line, the addresses of the two versions are as follows:

/usr/bin/php7.1
/usr/bin/php5

Do you use the full path when running different versions? You can actually use the update-alternatives tool to configure the version of PHP that the default item runs.


$ update-alternatives --set php /usr/bin/php7.1

Running php-v in this way is equivalent to running the/usr/bin/php7.1-v. If you want to use the PHP5 version, you can switch by running the following command:


$ apt install python-software-properties
0

Summarize


Related articles: