Install to php 8.0 the php8.1 and php8.2 and set for different sites in Ubuntu 22.04 and Apache 2.4.58

At the moment I have running PHP 8.0 with Mautic and Moodle on a VPS with Ubuntu Server 22.04.

I want to install Suite CRM what needs php8.1 and another app who needs php8.2.

  1. How to install php8.1 and php8.2. I want to keep my default php8.0 and the applications will initially continue to run under 8.0?

  2. How can I set the PHP version for every site/app?

  3. About cron jobs: At the moment the cron job in Mautic is e.g. * * * php /var/www/html/mautic/bin/console mautic:campaigns:trigger
    How do i set an cron job that it use a special php version? (Mautic and Moodle needs cron jobs)

Here ist my config for mo.stefan-franz.de:

<VirtualHost *:80>
ServerAdmin info@stefan-franz.de
DocumentRoot /var/www/html/moodle/
ServerName  mo.stefan-franz.de

<Directory /var/www/html/moodle/>
Options +FollowSymlinks
AllowOverride All
Require all granted
</Directory>

<FilesMatch .php$>
    SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost/"
</FilesMatch>

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

RewriteEngine on
RewriteCond %{SERVER_NAME} =mo.stefan-franz.de
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

My SSL configuration_

    root@mautic01:/etc/apache2/sites-available# ls -alh
total 44K
    drwxr-xr-x 2 root root 4.0K Nov 14 12:19 .
    drwxr-xr-x 8 root root 4.0K Nov  7 08:31 ..
    -rw-r--r-- 1 root root 1.3K Apr  8  2023 000-default.conf
    -rw-r--r-- 1 root root 4.5K Apr  8  2023 default-ssl.conf
    -rw-r--r-- 1 root root  647 Sep 30 12:40 ma.stefan-franz.de-le-ssl.conf
    -rw-r--r-- 1 root root  532 Nov 14 12:19 ma.stefan-franz.de.conf
    -rw-r--r-- 1 root root  626 Oct  8 19:56 mo.stefan-franz.de-le-ssl.conf
    -rw-r--r-- 1 root root  466 Oct  8 19:56 mo.stefan-franz.de.conf
    -rw-r--r-- 1 root root  636 Nov  7 08:31 shop.stefan-franz.de-le-ssl.conf
    -rw-r--r-- 1 root root  474 Nov  7 08:31 shop.stefan-franz.de.conf

And here is the SSL config for moodle:

<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin info@stefan-franz.de
DocumentRoot /var/www/html/moodle/
ServerName  mo.stefan-franz.de

<Directory /var/www/html/moodle/>
Options +FollowSymlinks
AllowOverride All
Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined


SSLCertificateFile /etc/letsencrypt/live/mo.stefan-franz.de/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mo.stefan-franz.de/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
Header always set Strict-Transport-Security "max-age=31536000"
Asked By: Stefan Franz

||

Yes, you can install multiple versions of PHP on the same Ubuntu server and configure different applications to use different PHP versions. This is particularly useful when managing applications like Mautic, Moodle, and SuiteCRM, each requiring specific PHP versions. Here’s a step-by-step guide to help you through this process:

1. Installing Additional PHP Versions

First, you’ll need to add a repository that includes newer PHP versions:

sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update

Then, install the PHP versions you need (in your case, PHP 8.1 and 8.2):

sudo apt install php8.1 php8.1-cli php8.1-fpm php8.1-xml php8.1-mysql php8.1-gd php8.1-curl ...
sudo apt install php8.2 php8.2-cli php8.2-fpm php8.2-xml php8.2-mysql php8.2-gd php8.2-curl ...

Replace ... with any additional modules your applications may require.

2. Configuring Apache/Nginx for Different PHP Versions

Depending on your web server (Apache or Nginx), you need to configure it to use different PHP versions for different sites.

For Apache:

Edit the VirtualHost configuration for each site and set the PHP-FPM version. Example for a site using PHP 8.1:

<VirtualHost *:80>
    ServerName yoursite.com
    DocumentRoot /path/to/your/site

    <FilesMatch .php$>
        SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost/"
    </FilesMatch>
</VirtualHost>

For Nginx:

Edit the server block configuration for each site:

server {
    listen 80;
    server_name yoursite.com;

    root /path/to/your/site;
    index index.php;

    location ~ .php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    }
}

3. Configuring PHP for CLI

For command-line tasks (like cron jobs), you can specify the PHP version directly in the command. Update your cron jobs to specify the PHP version:

For Mautic with PHP 8.0:

* * * * * php8.0 /var/www/html/mautic/bin/console mautic:campaigns:trigger

4. Restarting Services

After making changes, restart your web server and the PHP-FPM services:

sudo systemctl restart apache2  # If using Apache
sudo systemctl restart nginx    # If using Nginx
sudo systemctl restart php8.1-fpm
sudo systemctl restart php8.2-fpm
Answered By: buttery-06ripper

If you want to use different PHP versions on your sites, you have to use PHP FPM(FastCGI Process Manager).

1. Installing php8.1 and php8.2

Some of the PHP versions you want are not in the official repos, so you can use ppa:ondrej/php repository:

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt upgrade

Install PHP FPM and required extensions:

sudo apt install php8.0-fpm
sudo apt install php8.1-fpm
sudo apt install php8.2-fpm

sudo a2enmod proxy_fcgi
sudo service apache2 restart

You also have to install the required PHP extensions by the sites/apps you want to run. For example if your PHP8.2 site need MySQL you will have to install that too:

sudo apt install php8.2-mysql

2. Set the PHP version for every site

Edit your Apache2 vhost configs for each site in /etc/apache2/sites-available/. Between <VirtualHost> and </VirtualHost>, add the following code (replace 8.1 with the version you want for each site):

<FilesMatch .php$>
    SetHandler "proxy:unix:/var/run/php/php8.1-fpm.sock|fcgi://localhost/"
</FilesMatch>

You have to add these line to your -le-ssl.conf files too. For example /etc/apache2/sites-available/mo.stefan-franz.de-le-ssl.conf should look like this:

<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin info@stefan-franz.de
DocumentRoot /var/www/html/moodle/
ServerName  mo.stefan-franz.de

<Directory /var/www/html/moodle/>
Options +FollowSymlinks
AllowOverride All
Require all granted
</Directory>

<FilesMatch .php$>
    SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost/"
</FilesMatch>

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined


SSLCertificateFile /etc/letsencrypt/live/mo.stefan-franz.de/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mo.stefan-franz.de/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
Header always set Strict-Transport-Security "max-age=31536000"
</VirtualHost>
</IfModule>

3. Use a specific PHP version for the cron

The php command in your cron is using the CLI version of PHP. You have to install the version specific php-cli package. You can use update-alternatives to configure which version php will use. For example if you want to use PHP 8.2 for php, do this:

sudo apt install php8.2-cli

sudo update-alternatives --set php /usr/bin/php8.2

Alternatively, you can use the command php8.2 instead of php in your cron.

To make changes to the settings in php.ini, you have several different files:

  • Sites running PHP FPM 8.2:

    Edit: /etc/php/8.2/fpm/php.ini

    After changes to this file, you should restart the FPM service:

    sudo service php8.2-fpm restart
    
  • Sites running PHP FPM 8.1:

    Edit: /etc/php/8.1/fpm/php.ini

    After changes to this file, you should restart the FPM service:

    sudo service php8.1-fpm restart
    
  • Sites running PHP FPM 8.0:

    Edit: /etc/php/8.0/fpm/php.ini

    After changes to this file, you should restart the FPM service:

    sudo service php8.0-fpm restart
    
  • Default for sites without SetHandler in their vhost config:

    Edit: /etc/php/8.1/apache2/php.ini

    After changes to this file, you should restart the Apache2 service:

    sudo service apache2 restart
    
  • When using the command php from the cron (replace 8.2 with the version you set in update-alternatives):

    Edit: /etc/php/8.2/cli/php.ini

Answered By: sotirov
Categories: Answers Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.