7

Make PHP7 and PHP 8 live together

I've upgraded from PHP 7 to PHP 8. As it's usually the case with PHP, I still have php7.4 in /usr/bin (alongside with php8.0). But, when I run the php -v command, it answers php8.0.

Since then, when trying to install software (for example Docker and VirtualBox), I get the error message php7.4-fpm.service: Failed with result 'exit-code', followed by Failed to start The PHP 7.4 FastCGI Process Manager. It seems therefore that I need php7.4 to run to install such applications.

But I use PHP 8 functionalities to code a website and therefore don't want to get back to php7.4

Is there a way to use php8.0 and php7.4 at the same time? I could make PHP 8 work on a virtual machine but it's quite heavy.

I'm on Ubuntu 20 (but I'm not sure that this problem is OS-specific).

Zlotz

IMSoP
  • 89,526
  • 13
  • 117
  • 169
Zlotz
  • 147
  • 1
  • 2
  • 12
  • This is a great question. I am in the same boat. I run Moodle and Nextcloud. Nextcloud 21 has some big improvements with php8. BUT Moodle won't run on it. There has to be a good way to do this. – walttheboss Feb 24 '21 at 23:10

4 Answers4

7

As @Abilogos has mentioned in his answer, you can have multiple versions of php using update-alternatives and in cli, version which is set to default will work with php command but when it comes to run a specific version for a website with Apache/Nginx, it get's difficult and I also faced this thing recently when I wanted to make my code PHP 8 compatible parallely without hampering the existing working code and finally I was able to get it what I needed to make all this thing work.

Here is what I did on Fedora 33

Step 1. Installed PHP 8.0 as default version and using Remi repo, I've installed other PHP versions as well (5.6, 7.0, 7.1, 7.2, 7.3 & 7.4)

Step 2. I checked to update-alternatives to change the PHP version from 8.0 to 7.4 but there were not entry in it. So, I installed all these in update-alternatives

cp /usr/bin/php /usr/bin/php80 
update-alternatives --install /usr/bin/php php /usr/bin/php80 0
update-alternatives --install /usr/bin/php php /opt/remi/php74/root/usr/bin/php 1
update-alternatives --install /usr/bin/php php /opt/remi/php73/root/usr/bin/php 2

Step 3. Now, it is very easy to change PHP version in cli at any time easily. Example, when your application have dependency on some libraries but they are PHP 8 compatible and you want to change the default PHP version to run the composer install

Step 4. Now, to configure website1 to run on PHP 8 and website2 to run on PHP 7.4, we need two PHP-FPM configured and running on different ports.

Change default PHP-FPM port

vim /etc/php-fpm.d/www.conf

;listen = /run/php-fpm/www.sock
listen = 9001

Similarly, change PHP-FPM 7.4 port

vim /etc/opt/remi/php74/php-fpm.d/www.conf

;listen = /run/php-fpm/www.sock
listen = 9002

Start/Restart PHP-FPM services

systemctl start php-fpm
systemctl start php74-php-fpm

Step 5. Change vhost configuration file to use different version of PHP for website1 and website2

vim /etc/httpd/conf.d/website1.conf 

<FilesMatch \.php$>
    # PHP 8.0
    SetHandler "proxy:fcgi://127.0.0.1:9001"
</FilesMatch>

vim /etc/httpd/conf.d/website2.conf

<FilesMatch \.php$>
    # PHP 7.4
    SetHandler "proxy:fcgi://127.0.0.1:9002"
</FilesMatch>

Restart Apache/Nginx

systemctl restart httpd

And we're done

Haridarshan
  • 1,898
  • 1
  • 23
  • 38
  • The original question was about Ubuntu, "remi" way is only for Fedora/RHEL/CentOS ;) And, for CLI, using the "module" command is probably simpler than setting "alternatives" (as already configured), try "module load php74" – Remi Collet May 16 '21 at 06:20
1

you can have multiple version of php, but only one can be set on as a default.

use this to set a default:

update-alternatives --config  php

using default:

php ./script.php

using specific version:

php7 ./script.php
php8 ./script.php
Kamafeather
  • 8,663
  • 14
  • 69
  • 99
Abilogos
  • 4,777
  • 2
  • 19
  • 39
0

There are a few great tutorials that solve this issue.

https://www.digitalocean.com/community/tutorials/how-to-run-multiple-php-versions-on-one-server-using-apache-and-php-fpm-on-ubuntu-18-04

https://tecadmin.net/how-to-install-multiple-php-version-with-apache-on-ubuntu-20-04/

https://bobcares.com/blog/run-multiple-php-versions-apache-php-fpm/

  • 2
    Link-only answers are discouraged. Please include all the necessary information in your answer, and quote from the linked page in case it becomes unavailable. – Syscall Feb 25 '21 at 06:58
-1

Removing php7.4 with sudo apt-get purge php7.4-common solved my issue.

Zlotz
  • 147
  • 1
  • 2
  • 12
  • 3
    That sounds more like admitting defeat than solving the problem. – IMSoP Feb 04 '21 at 22:43
  • In fact, my goal was to install Docker and keep using PHP8. Do you think I should change the title of my topic ? – Zlotz Feb 06 '21 at 21:37
  • 1
    I doubt installing Docker was ever actually your problem, since there's no reason that would need the php-fpm service to be running. More likely, the install process just triggered some services to restart, and PHP 7.4 had got into a state where it couldn't start; the same error would probably have showed up if you rebooted the server. – IMSoP Feb 06 '21 at 23:21