Below is a simple Markdown tutorial for configuring Apache to run two PHP versions (PHP 7.4 and PHP 8.0) concurrently using PHP-FPM. This assumes you're using a Linux system with Apache.
Contents
- 1 Running Multiple PHP Versions with Apache and PHP-FPM
- 1.1 Prerequisites
- 1.2 Install Apache
- 1.3 Add Ondrej Repository (if needed)
- 1.4 Install PHP Versions
- 1.5 Enable Apache Modules
- 1.6 Configure PHP-FPM Sockets
- 1.7 Configure Apache Virtual Hosts
- 1.8 Enable Virtual Hosts
- 1.9 Create Document Roots
- 1.10 Test Configuration
- 1.11 install common php7.4 extension if you got extension error
- 1.12 Run manual change CLI version
- 1.13 Interchange php version on Apache
- 1.14 conclusion
Running Multiple PHP Versions with Apache and PHP-FPM
before starting, check if we already has two php version
sudo update-alternatives --display php
Prerequisites
- Linux server (Ubuntu used in this example)
- Apache web server
- PHP 7.4 and PHP 8.0 installed
Install Apache
sudo apt-get update
sudo apt-get install apache2
Add Ondrej Repository (if needed)
sudo add-apt-repository ppa:ondrej/php
sudo apt update
if got add-apt-repository: command not found
install software properties common firstsudo apt-get update sudo apt-get install software-properties-common
Install PHP Versions
sudo apt-get install php7.4 php7.4-fpm php7.4-mysql php7.4-pdo php7.4-gd php7.4-curl php7.4-json php7.4-xml php7.4-mbstring php7.4-zip php7.4-opcache php7.4-intl php7.4-imagick php7.4-soap
sudo apt-get install php8.0 php8.0-fpm php8.0-cli php8.0-mysql php8.0-xml php8.0-mbstring php8.0-bcmath php8.0-curl php8.0-tokenizer php8.0-intl php8.0-readline php8.0-ctype php8.0-gd
sudo apt-get install php8.1 php8.1-fpm php8.1-cli php8.1-mysql php8.1-xml php8.1-mbstring php8.1-bcmath php8.1-curl php8.1-tokenizer php8.1-intl php8.1-readline php8.1-ctype php8.1-gd
Enable Apache Modules
sudo a2enmod proxy_fcgi actions
sudo systemctl restart apache2
Configure PHP-FPM Sockets
Check the PHP-FPM configuration files for the listening sockets:
grep -R "^listen" /etc/php/7.4/fpm/pool.d/
grep -R "^listen" /etc/php/8.0/fpm/pool.d/
Configure Apache Virtual Hosts
Create Virtual Host configuration files for each PHP version:
/etc/apache2/sites-available/php74.example.com.conf
<VirtualHost *:80>
ServerName php74.example.com
DocumentRoot /var/www/php74.example.com
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost/"
</FilesMatch>
<Directory /var/www/php74.example.com>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
/etc/apache2/sites-available/php80.example.com.conf
<VirtualHost *:80>
ServerName php80.example.com
DocumentRoot /var/www/php80.example.com
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php8.0-fpm.sock|fcgi://localhost/"
</FilesMatch>
<Directory /var/www/php80.example.com>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Enable Virtual Hosts
sudo a2ensite php74.example.com
sudo a2ensite php80.example.com
sudo systemctl restart apache2
Create Document Roots
sudo mkdir /var/www/php74.example.com
sudo mkdir /var/www/php80.example.com
Test Configuration
Create index.php
files in the document roots:
<?php phpinfo(); ?>
Visit http://php74.example.com
and http://php80.example.com
in your browser to verify the PHP versions.
install common php7.4 extension if you got extension error
sudo apt-get install php7.4-mysql php7.4-pdo php7.4-gd php7.4-curl php7.4-json php7.4-xml php7.4-mbstring php7.4-zip php7.4-opcache php7.4-intl php7.4-imagick php7.4-soap
Run manual change CLI version
https://gridpane.com/kb/setting-the-cli-php-version/
Checking Your CLI PHP Version
To check your CLI PHP version, you can run the following command:
sudo update-alternatives --display php
This will output the following, only with the correct PHP version should your setting be different:
root@servername:~# sudo update-alternatives --display php
php - manual mode
link best version is /usr/bin/php8.1
link currently points to /usr/bin/php7.4
link php is /usr/bin/php
slave php.1.gz is /usr/share/man/man1/php.1.gz
/usr/bin/php7.3 - priority 73
slave php.1.gz: /usr/share/man/man1/php7.3.1.gz
/usr/bin/php7.4 - priority 74
slave php.1.gz: /usr/share/man/man1/php7.4.1.gz
/usr/bin/php8.0 - priority 80
slave php.1.gz: /usr/share/man/man1/php8.0.1.gz
/usr/bin/php8.1 - priority 81
slave php.1.gz: /usr/share/man/man1/php8.1.1.gz
root@servername:~#
Changing Your CLI PHP Version on Nginx or OpenLiteSpeed
NGINX AND OPENLITESPEED UPDATE
As of November 8th 2022, setting the PHP CLI version on both Nginx and OpenLiteSpeed has been brought to parity so that the same commands work on both server stacks.
STEP 1. SET YOUR PREFERRED CLI PHP VERSION
Run the following command to change the PHP CLI version:
sudo update-alternatives --set php /usr/bin/phpX.X
X.X = the version number, for example: 7.4, 8.0, 8.1.
For example:
sudo update-alternatives --set php /usr/bin/php7.4
STEP 2. MAKE THE CHANGE PERSIST
Our systems check for a token file for the CLI PHP version. If it’s not present, your changes will be reverted. Create the token file with:
/root/preferred_php_cli.version
Add the PHP version – number only. For example:
7.4
Now save the file with CTRL+O followed by Enter. Exit nano with CTRL+X.
Interchange php version on Apache
To ensure that Apache is using PHP 8.0 instead of PHP 7.4, you need to disable the PHP 7.4 module and enable the PHP 8.0 module for Apache. Here are the steps:
- ensure apache extension for both php is isntalled
sudo apt-get install libapache2-mod-php7.4 sudo apt-get install libapache2-mod-php8.0
- Disable the PHP 7.4 module:
sudo a2dismod php7.4
- Enable the PHP 8.0 module:
sudo a2enmod php8.0
- Restart Apache to apply the changes:
sudo systemctl restart apache2
- Verify the PHP version used by Apache by creating a
phpinfo.php
file in your web root directory:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/phpinfo.php
Then, navigate to http://your-server-ip/phpinfo.php
in your web browser. It should display information about PHP 8.0.
If you still see PHP 7.4, double-check that the PHP 8.0 packages and the Apache module for PHP 8.0 are installed correctly.
conclusion
Please adjust paths, domain names, and configurations based on your specific setup.
defautl php running is the first installed php so consider which version you use when chosing which one to install first