31 May 2022 Reading time: 4 minutes

Dmitri Drevko

Web Programmer

Why do you need multiple versions of PHP and how to set them up

Ispmanager

When hosting websites on a server, they may require different versions of PHP. This is due to the specifics of CMS. This can happen, for example, if one of the content management systems has not been updated for a long time and uses an earlier version of PHP, and the other is updated frequently and requires a more recent version of PHP “under the hood”. There are cases when the CMS simply does not support certain features of the PHP version and refuses to run on it.


КIn this article, I will explain how to correctly configure alternative versions of PHP on the server and get around the problems.

Manual installation, configuration and selection of the PHP version

The process of installing PHP on the server depends on the operating system and the version of PHP to be installed. All actions are performed in the command line. The basic installation of the PHP version is done first. The administrator can then install alternate versions of PHP and choose one of them as the primary version.

Basic PHP version installation

Basic installation in the most popular operating systems looks like this:

  • In Debian-like systems, the command apt install php is used apt install php;
  • In CentOS — yum install php;

These commands install the latest version of the language available in the repository.

If you need to install a particular version of PHP which is not yet available in the operating system's repository, you first need to run two commands:

apt install software-properties-common — this command installs software to manage distributions and independent software vendors.

add-apt-repository ppa:ondrej/php — adds a repository to the system from which the necessary versions of PHP will be installed directly.

You can then run a command to install PHP, e.g. version 7.4:
apt install php7.4

Selecting the version of PHP to set it as the default

When the basic PHP version installation is complete, it becomes the main version by default.

To check which version of PHP is currently installed and running by default, run the command:
php -v

To change the default version, use the command:
update-alternatives --config php

After running it, you can see a similar list of versions and their status. For example:

Selection Path Priority Status
*0 /usr/bin/php5.6 56 auto mode
1 /usr/bin/php7.4 74 manual mode
2 /usr/bin/php8.0 80 manual mode

In this example, the * (asterisk) indicates the version of PHP currently selected on the server - 5.6.

To change the version, the system will offer a choice: press Enter to leave the current version selected, or specify the number of the required versopm (from those listed above) and press Enter:
Press to keep the current choice[*], or type selection number: 1

In the example, PHP version 7.4 will be selected and installed by default.

Installing extensions

To install the necessary PHP extensions, run the command:
apt install php<версия>-<расширение>

For PHP, which is installed on the system by default, the command is
apt install php-mysqli

If the version (e.g., 7.4) is installed manually, then the command will be
apt install php7.4-mysqli

Setting up PHP

To edit the settings of a particular version of PHP, you need to open the php.ini file for editing. It may be saved in different directories depending on the module installed. This means that the path to be written in the command will be different. For example in Ubuntu, for a version of PHP 7.4 that runs in the command line, the command is as follows:
nano /etc/php/7.4/cli/PHP.ini

For apache module:
nano /etc/php/7.4/apache2/PHP.ini

For fpm module:
nano /etc/php/7.4/fpm/PHP.ini

As you can see, to edit PHP settings and install modules, you need to know the current PHP version and the installation path. If you are using the ispmanager web server control panel, the complexities can be bypassed.

Important: If you have ispmanager on your server, do not use the manual method – it can create conflicts causing PHP not to work properly. I am talking about the manual method for information and for those who prefer to work without a control panel.

How ispmanager simplifies the installation and configuration of PHP and its modules

When working through ispmanager, you do not need to use the command line to configure PHP. PHP installation and configuration are under Settings -> PHP Settings

Section in ispmanager with PHP settings
Section in ispmanager with PHP settings

ЗHere you can install a missing version of PHP without having to worry about whether the required repository is installed, configure the required version of PHP, and install or disable the required extensions for it. Disabling extensions may be necessary if there is a conflict with other software installed on the server. The details of PHP management are described in the documentation:

Working with PHP in ispmanager

I do not work with PHP through ispmanager only when I need to set up alternate versions. Next time I will tell you how I use the panel to upload a lot of data via PHP and install extensions.

Ispmanager for web server management

 

You can try ispmanager free for two weeks. The panel makes it easy to administer web servers and work with websites. For example, instead of manually configuring Apache or Nginx or installing languages, you can simply select the settings in the interface, and ispmanager will make all the settings automatically.