Multi PHP Installation (PHP 7.4 and PHP 8.3) on Ubuntu
This document explains how to install and configure multiple PHP versions on Ubuntu without using Docker. We will use PHP-FPM for configuring web servers like Apache and Nginx.
1. Install PHP 7.4 and PHP 8.3
-
Add the PHP repository and update the system:
sudo add-apt-repository ppa:ondrej/php
sudo apt update -
Install PHP 7.4 and PHP 8.3 along with common extensions:
sudo apt install php7.4 php7.4-cli php7.4-fpm php7.4-mysql php7.4-curl php7.4-xml php7.4-mbstring
sudo apt install php8.3 php8.3-cli php8.3-fpm php8.3-mysql php8.3-curl php8.3-xml php8.3-mbstring
2. Add PHP to update-alternatives
-
Add both PHP versions to
update-alternatives
:sudo update-alternatives --install /usr/bin/php php /usr/bin/php7.4 1
sudo update-alternatives --install /usr/bin/php php /usr/bin/php8.3 2 -
Choose the desired PHP CLI version:
sudo update-alternatives --config php
Select the PHP CLI version as needed.
3. Configure Apache for Multi PHP
-
Enable PHP-FPM module and configuration:
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php7.4-fpm
sudo a2enconf php8.3-fpm
sudo systemctl restart apache2 -
Configure Virtual Hosts in Apache:
sudo nano /etc/apache2/sites-available/example.com.conf
PHP 7.4:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html/php7_app
<Directory /var/www/html/php7_app>
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>PHP 8.3:
<VirtualHost *:80>
ServerName php8.example.com
DocumentRoot /var/www/html/php8_app
<Directory /var/www/html/php8_app>
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php8.3-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost> -
Enable
example.com.conf
witha2ensite enable
:sudo a2ensite enable example.com
-
Restart Apache:
sudo systemctl restart apache2
4. Configure Nginx for Multi PHP
-
Open Nginx configuration:
sudo nano /etc/nginx/sites-available/default
-
Add the following server block:
PHP 7.4:
server {
listen 80;
server_name php7.example.com;
root /var/www/html/php7_app;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}PHP 8.3:
server {
listen 80;
server_name php8.example.com;
root /var/www/html/php8_app;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
} -
Test and restart Nginx:
sudo nginx -t
sudo systemctl restart nginx
5. Test PHP Versions
-
Create an
info.php
file in the root directory of each application:sudo nano /var/www/html/php7_app/info.php
sudo nano /var/www/html/php8_app/info.php -
Add the following content to the files:
<?php
phpinfo();
?> -
Access through a browser:
- PHP 7.4:
http://php7.example.com/info.php
- PHP 8.3:
http://php8.example.com/info.php
This way, you can run multiple PHP versions simultaneously on an Ubuntu server using Apache or Nginx.