In this tutorial we will configure Apache to run PHP 7.3 and PHP 7.4 simultaneously, and choose between them using Virtual Hosts or .htaccess.
There are several ways to achieve the same goal and run multiple websites on different PHP versions simultaneously. As for running multiple PHP versions on one server, my choice is Apache with FastCGI.
Prerequisites
- You should already have Apache installed and serving web pages before following this guide.
- Basic understanding/knowledge of Linux commands.
- Basic knowledge of Apache and PHP.
Step 1: Let’s begin by updating the package lists.
sudo yum install epel-release && sudo yum update
In order to install multiple versions of PHP, you will need to install and enable the Remi repository to your system. Which also offers the latest versions of the PHP stack on CentOS 7 and 8 system.
You can add the both repository to your system using the below commands:
sudo dnf install http://rpms.remirepo.net/enterprise/remi-release-8.rpm
Step 2: Install multiple PHP version 7.3 and 7.4 by below process.
sudo dnf install php73 php73-php-fpm -y
sudo dnf install php74 php74-php-fpm -yNext, verify the status and enable service of
php73-php-fpm and php74-php-fpm
service with the following commands:
To Start fpm service:
sudo systemctl start php73-php-fpm && sudo systemctl start php74-php-fpmTo Enable fpm serivce:
sudo systemctl enable php73-php-fpm && sudo systemctl enable php74-php-fpmTo Verify fpm serivce:
sudo systemctl status php73-php-fpm && sudo systemctl status php74-php-fpm
Step 3: Apache Installation
sudo yum install httpdNow, Verify and enable apache service:
sudo systemctl enable httpd and sudo systemctl status httpdStep 4 : Create two Virtual Host for php 7.3 and php7.4 (Change the domain name) Create webroot directory for your both website.
cd /var/www/ && mkdir site1.your_domain site2.your_domainCreate VirtualHost for 1st website.
sudo vim /etc/httpd/conf.d/php73_example.conf
<VirtualHost *:80> ServerName site1.example.com DocumentRoot /var/www/site1.your_domain DirectoryIndex info.php ErrorLog /var/log/httpd/site1.your_domain-error.log CustomLog /var/log/httpd/site1.your_domain-access.log combined <Directory /var/www/site1.your_domain> Options -Indexes +FollowSymLinks +MultiViews AllowOverride All Require all granted </Directory> <FilesMatch \.php$> SetHandler "proxy:unix:/var/run/php/php7.3-fpm.sock|fcgi://localhost" </FilesMatch> </VirtualHost>Create VirtualHost for 2nd website.
sudo vim /etc/httpd/conf.d/php74_example.conf
<VirtualHost *:80> ServerName site2.example.com DocumentRoot /var/www/site2.your_domain DirectoryIndex info.php ErrorLog /var/log/httpd/site1.your_domain-error.log CustomLog /var/log/httpd/site1.your_domain-access.log combined <Directory /var/www/site2.your_domain> Options -Indexes +FollowSymLinks +MultiViews AllowOverride All Require all granted </Directory> <FilesMatch \.php$> SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost" </FilesMatch> </VirtualHost>Check the syntax of apache Virtualhost and verify.
sudo httpd -tRestart apache and fpm services of both php:
sudo service httpd restart && sudo service php73-php-fpm restart && sudo service php74-php-fpm restart
Step 5: Test PHP
Add info.php in both the website directory and paste the below code.sudo vim /var/www/site1.your_domain/info.php
sudo cp /var/www/site1.your_domain/info.php /var/www/site2.your_domain/We can now check the php version for both website by going to http://site1.example.com and http://site2.example.com.
Using the above tutorial you have learned How To Handle Multiple PHP Versions With Apache Server.
100% LikesVS
0% Dislikes