Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make PHP7 and PHP 8 live together

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


1 Answers

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

like image 177
Haridarshan Avatar answered Jul 28 '26 21:07

Haridarshan