How to Install Different / Multiple PHP Versions of PHP in Fedora

Share:
how-to-install-different-multiple-php-versions-of-php-in-fedora

Different applications may require various versions of PHP. In this post, we are going to install multiple versions of PHP on Fedora. In my particular case, I'm using Fedora 35.

We will also show how to set the default version of PHP to be used on the Fedora system as when required. We will be installing PHP 7.4 and PHP 8 on a single Fedora system.

Currently, the supported stable version of PHP in the Fedora software repositories is the PHP 8.0. You can confirm this by running the dnf command below.

$ dnf info php-fpm

The output should be similar with below:

Latest php version in Fedora

PHP 8 Installation

To install the default PHP version from the Fedora software repositories, use the command below:

$ sudo dnf install php-cli php-fpm php-gd php-curl php-mysqlnd php-zip php-xml php-mbstring php-bcmath
Install php 8 Fedora

Just press "y" followed by "Enter" and the installation process will continue.

When the installation process finished, let's verify our installation by typing this command:

$ php -v

It should respond with something like this:

PHP 8.0.14 (cli) (built: Dec 16 2021 03:01:07) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.14, Copyright (c) Zend Technologies

PHP 7.4 Installation

Next, we'll install an older PHP version, version 7.4. Unfortunately, Official Fedora software repository only provide PHP 8. We must add another repository to be able to install older PHP version. This repository is called Remi repository.

To add Remi repository to our Fedora 35 system:

$ sudo dnf -y install https://rpms.remirepo.net/fedora/remi-release-35.rpm
Add remi repo

Some common dependencies are available in Remi repository, which need to be enabled:

$ sudo dnf config-manager --set-enabled remi
$ sudo dnf module reset php
Enable remi repo

Now it's time to install PHP 7.4.

$ sudo dnf install php74-php-cli php74-php-fpm php74-php-gd php74-php-curl php74-php-mysqlnd php74-php-zip php74-php-xml php74-php-mbstring php74-php-bcmath

Sometimes, in the middle of installation process, you'll be asked something like this:

Importing GPG key 0x478F8947:
Userid : "Remi's RPM repository (https://rpms.remirepo.net/) <[email protected]>"
Fingerprint: B1AB F71E 14C9 D748 97E1 98A8 B195 27F1 478F 8947
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-remi-35
Is this ok [y/N]: y

Just press "Y" followed by "Enter" to continue with installation process.

When the installation process finished, let's verify our installation by typing this command:

$ php -v

It should respond with something like this:

PHP 8.0.14 (cli) (built: Dec 16 2021 03:01:07) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.14, Copyright (c) Zend Technologies

For PHP 7.4, verify installation by using this command:

$ php74 -v

It should respond with something like this:

PHP 7.4.27 (cli) (built: Dec 14 2021 17:17:06) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

Update Nginx Configuration

To be able to use desired PHP version for specific virtual host, we need to update Nginx configuration.

First, we'll need to make sure both PHP versions are running normally. Just like another systemd-based distribution, we can ask systemctl:

$ systemctl status php-fpm
$ systemctl status php74-php-fpm

The output should be similar with below (Active state is green):

Systemctl PHP-FPM

Next, we'll need to each version's configuration file and figure out where are their listen addresses. In my case, it is:

PHP 8.0 : /run/php-fpm/www.sock

PHP 7.4: /var/opt/remi/php74/run/php-fpm/www.sock

Once we figured them out, we need to adjust our virtual host configuration file.

Final Words

I hope that you now know how to install different / multiple PHP versions in Fedora 35. If you run into any issues or have any feedback feel free to drop a comment below.

Tags PHP Linux
Share:

1 comment

Leave a reply

Your email address will not be published. Required fields are marked *