If you are a developer working with multiple PHP projects , you might have faced, or probably might face in future , the situation where you will be working with multiple projects that support different versions of PHP.
Although it's always advisable to use the latest version in each project , there's always a case where it can be undesirable to do so.
Although you can always switch various versions of php manually using the command line , it is more tedious and requires you to run multiple commands on each version change.
There are a few more recommended ways to use multi version of php , you can learn from blog : Various ways to run multiple PHP versions on your server but in this article, we will be discussing one easier and lightweight way to use multiple php versions in different php projects.
Using Apache or nginx server configuration (i.e. popular web server for php ecosystems), we can define PHP versions based on website or virtual host configurations. Learn about Virtual host Setting in blog: Setting up Multiple Virtual Hosts (Domains) in Apache server in Ubuntu
Tools like Apache's mod_php or Nginx with PHP-FPM allow you to specify different PHP versions for different virtual hosts or directories. The benefit of this approach is that it has more manual configuration but provides flexibility and control.
This method is easier to set up and can be done in no time. So , if you want a quick fix and a lightweight solution, then this can be an appropriate way to solve the problem of multiversion without installing any additional tools or softwares..
Basically, There are two ways by which you can setup project specific PHP version management using the Apache Server configuration:
Before jumping into the configuration settings , we first setup our prerequisities:
Install apache2
$ sudo apt update
$ sudo apt install apache2 -y
$ sudo systemctl status apache2
Next, we need to install multiple php packages as per your need for your projects to run :
Additionally, we need to install FastCGI module for Apache HTTP Server (libapache2-mod-fcgid) :
Get php repository and Install FastCGI module
$ sudo apt install software-properties-common -y
$ sudo add-apt-repository ppa:ondrej/php -y
$ sudo apt update -y
$ sudo apt install libapache2-mod-fcgid
Installation of Modules For php 7.2 (you can look for any versions as per your projects need)
$ sudo apt-get install php7.2 php7.2-fpm php7.2-mysql libapache2-mod-php7.2 -y
Installation of Modules For php 8.1
$ sudo apt install php8.1 php8.1-fpm php8.1-mysql libapache2-mod-php8.1 -y
Verify the installation of php
$ ls -la /var/run/php/
total 20
drwxr-xr-x 2 www-data www-data 260 Mar 19 07:12 .
drwxr-xr-x 31 root root 1100 Apr 30 14:38 ..
lrwxrwxrwx 1 root root 30 Feb 20 19:10 php-fpm.sock -> /etc/alternatives/php-fpm.sock
-rw-r--r-- 1 root root 5 Feb 22 08:49 php7.1-fpm.pid
srw-rw---- 1 www-data www-data 0 Feb 22 08:49 php7.1-fpm.sock
-rw-r--r-- 1 root root 3 Feb 20 19:10 php7.4-fpm.pid
srw-rw---- 1 www-data www-data 0 Feb 20 19:10 php7.4-fpm.sock
-rw-r--r-- 1 root root 3 Feb 20 19:10 php8.1-fpm.pid
srw-rw---- 1 www-data www-data 0 Feb 20 19:10 php8.1-fpm.sock
-rw-r--r-- 1 root root 6 Mar 19 06:22 php8.2-fpm.pid
srw-rw---- 1 www-data www-data 0 Mar 19 06:22 php8.2-fpm.sock
-rw-r--r-- 1 root root 6 Mar 19 07:12 php8.3-fpm.pid
srw-rw---- 1 www-data www-data 0 Mar 19 07:12 php8.3-fpm.sock
You can see various installations of php as shown in the code above. If you can see all of your php versions installed, then you need to enable php-fpm services for both php version that we require:
$ sudo systemctl start php8.1-fpm
$ sudo systemctl start php7.2-fpm
Here, PHP-FPM is a variant of PHP that serves as a FastCGI Process Manager, handling PHP requests for web servers like Apache or Nginx.
Enabling apache modules for fcgi and Restart apache server
$ sudo a2enmod actions fcgid alias proxy_fcgi
$ sudo systemctl restart apache2
With this all of the dependencies required have been installed:
Now , as discussed above, we have two ways to serve multiple php version for each projects:
To enable multiple php configuration using a virtual host, we first need to create a virtual host. If you are new to creating virtual host you can check my earlier blog.
Let's consider two project with folder projectfor8.1 and projectfor7.2 are created, the folder are your folders with php application code :
$ sudo mkdir /var/www/projectfor8.1
$ sudo mkdir /var/www/projectfor7.2
Now we create a virtual host for each of these projects, where we define which php-fpm version will be used in the specific project. Your virtual host for porject projectfor8.1 will look like this
You can open up virtual host file using command(in unix system) or in your apache location in windows:
$ sudo nano /etc/apache2/sites-available/project81.example.com.conf
For project with 8.1 requirement
<VirtualHost *:80>
ServerName project81.example.com
DocumentRoot /var/www/projectfor8.1
<Directory /var/www/projectfor8.1>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php8.1-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>
SetHandler "proxy:unix:/var/run/php/php8.1-fpm.sock|fcgi://localhost"
This code is responsible to define which project is used by this project.
Save this content with filename as the domain name or subdomain of your choice, in our case project81.example.com and close the editor.
Likewise , For Project with 7.2 PHP Requirements
<VirtualHost *:80>
ServerName projectphp72.example.com
DocumentRoot /var/www/projectphp72
<Directory /var/www/projectphp72>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>
Now , we enable these virtual hosts , using
$ sudo a2ensite project81.example.com
$ sudo a2ensite projectphp72.example.com
Now, Restart the apache server
$ sudo systemctl restart apache2
Now add a file index.php inside folder /var/www/projectfor8.1 or projectfor7.1 or any php project and add code
<?php echo phpinfo();?>
You will see , 8.1 version of php and so on for other versions.
Congratualations!! you have successfully setup your multiple php versions using apache2 virtual host configuration settings.
Similarly,
The .htaccess file is a configuration file used by the Apache web server to apply directory-level configuration settings. It allows website administrators to override the global server configuration for specific directories or files. It is similar to the above method but the configuration instead of adding to the virtual host we place it in .htaccess file which is a project wise apache configuration.
To enable .htaccess file you need to enable mod-rewrite in your apache server,
$ sudo a2enmod rewrite
Also in your virtual host file as above you need to add AllowOverride All so that .htaccess file can override the apache2 configuration .
So for project81.example.com you open up a virtual host configuration file in unix system :
$ sudo nano /etc/apache2/sites-available/project81.example.com.conf
The virtual host file looks something like this
<VirtualHost *:80>
ServerAdmin admin@demo.com
ServerName project81.example.com
ServerAlias www.project81.example.com
DocumentRoot /var/www/projectfor8.1
<Directory /var/www/projectfor8.1 />
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/err-demo.com.log
CustomLog ${APACHE_LOG_DIR}/demo.com.log combined
</VirtualHost>
This section AllowOverride All line and require all granted basically allows apache2 to overwrite its configuration to allow .htaccess to overwrite the virutal host configuration in each specific project. Similarly, you can do similar for other php version projects as well
Now in your project root folder
For 8.1
$ sudo nano /var/www/projectfor8.1/.htaccess
Add line in your .htaccess file
<FilesMatch .php>
\ SetHandler "proxy:unix:/var/run/php/php8.1-fpm.sock|fcgi://localhost/"
</FilesMatch>
Save this . and check for the version of php in your project by adding
<?php echo phpinfo()?>
in your index.php file
Congrautlations!! you have successfully setup your .htaccess file configuration to setup multiple php version updating .htaccess file
For various of php version you can use following handlers either in .htaccess or virtualhost based on your preferences and ease of use
For PHP 5.6
<FilesMatch .php$>
\ SetHandler "proxy:unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost/"
</FilesMatch>
For PHP 7.0
<FilesMatch .php$>
\ SetHandler "proxy:unix:/var/run/php/php7.0-fpm.sock|fcgi://localhost/"
</FilesMatch>
For php 7.1
<FilesMatch .php$>
\ SetHandler "proxy:unix:/var/run/php/php7.1-fpm.sock|fcgi://localhost/"
</FilesMatch>
Similarly for other version of php 7
For php 8.0
<FilesMatch .php$>
\ SetHandler "proxy:unix:/var/run/php/php8.0-fpm.sock|fcgi://localhost/"
</FilesMatch>
For php 8.1
<FilesMatch .php$>
\ SetHandler "proxy:unix:/var/run/php/php8.1-fpm.sock|fcgi://localhost/"
</FilesMatch>
Similarly for other php 8 versions and so on.
Other blogs from this Series: