1. Installing Apache2
sudo apt update
sudo apt install apache2 libapache2-mod-fcgid
Now enable a few modules required for the configuration of multiple PHP versions with Apache. These modules are necessary to integrate PHP FPM and FastCGI with the Apache server.
sudo a2enmod actions fcgid alias proxy_fcgi
2. Installing PHP Versions
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
For this tutorial, we are using PHP 8.3 and PHP 8.4 to configure with the Apache webserver. To use the multiple PHP versions, we will use PHP FPM and FastCGI.
Let’s install the following packages on your system.
sudo apt update
sudo apt install php8.3 php8.3-fpm
$ sudo apt install php8.4 php8.4-fpm
After installation, php-fpm services will be started automatically. Use the following commands to make sure both services are running.
sudo systemctl status php8.3-fpm
sudo systemctl status php8.4-fpm
3. Configuring Apache Virtual Hosts
sudo mkdir /var/www/php83
sudo mkdir /var/www/php84
Create an index.php file containing the phpinfo() function.
echo "" | sudo tee -a /var/www/php83/index.php
echo "" | sudo tee -a /var/www/php84/index.php
Let’s start the creation of VirtualHost. Apache keeps all the VirtualHost configuration files under /etc/apache2/sites-available with the extension .conf .
Create a file for the first virtual host and edit in your favorite text editor.
sudo nano /etc/apache2/sites-available/php83.example.com.conf
Add the following content. Make sure to use the correct ServerName and directory path according to your setup. This website is configured to work with PHP 8.3.
<VirtualHost *:80>
ServerName php83.example.com
DocumentRoot /var/www/php83
<Directory /var/www/php83>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php8.3-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>
Similarly, create a second VirtualHost configuration file to work with PHP 8.4.
sudo nano /etc/apache2/sites-available/php84.example.com.conf
Add the following content to file with proper ServerName and DocumentRoot.
<VirtualHost *:80>
ServerName php84.example.com
DocumentRoot /var/www/php84
<Directory /var/www/php84>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php8.4-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>
Both of the websites are configured now. But they are still not active. Apache keeps active sites under /etc/apache2/sites-enabled directory. You can simply create a symbolic link of config files to this directory or use the below command to do the same.
sudo a2ensite php83.example.com
$sudo a2ensite php84.example.com
After making all the changes restart Apache to reload new settings changes.
sudo systemctl restart apache2
4. Test Setup
Edit /etc/hosts file on your local system and make an entry like below. This will resolve temporary names to localhost IP address.
sudo nano /etc/hosts
Add following entry to end of file
127.0.0.1 php83.example.com
127.0.0.1 php84.example.com
Open a web browser and visit both of the sites. You will see that php83.example.com shows the version PHP 8.3 and php84.example.com is showing the PHP 8.4 as the configuration.
http://php83.example.com
http://php84.example.com
OPTIONAL
Command to install most used modules on php:
sudo apt install php8.4 libapache2-mod-php8.4 imagemagick php8.4-mcrypt php8.4-redis php8.4-dom ffmpeg php8.4-imagick php8.4-mysql php8.4-mysqli
php8.4-fpm php8.4-common php8.4-gd php8.4-curl php8.4-zip php8.4-xml php8.4-mbstring php8.4-bz2 php8.4-memcached php8.4-ldap php8.4-intl php8.4-sqlite3 -y
To restart apache and php-fpm run this command:
sudo systemctl restart apache2
sudo service php8.4-fpm restart
Replace 8.4 with your version.



