In this guide we will configure Apache to run Multiple PHP Versions PHP 7.0/7.1/7.2/7.3 and PHP 5.6 simultaneously, and choose between them using .htaccess.
1. Add Repository
Let’s begin by updating the package lists.
For Ubuntu 18.04 users, install libapache2-mod-fcgid
.
Ubuntu 16.04 users, install libapache2-mod-fastcgi
.
You may need software-properties-common
in order to add a repository with add-apt-repository
.
We will add Ondřej’s PHP repository that will allow us to download co-installable versions of PHP 5.6 and PHP 7.x, then update the package lists again.
Press ENTER
when prompted to add the repository.
2. Install Multiple PHP Versions
You can now install the version of PHP you require. Ondřej’s repository provides PHP 5.6 and PHP 7.x. Below are the examples to install PHP 5.6 and PHP 7.0/7.1/7.2/7.3.
Press y
and ENTER
when prompted to install.
Once installed, you should have multiple new sockets in /var/run/php/
.
In Step 4, we will use the <FilesMatch>
directive to tell Apache which PHP socket to use.
Extensions/Libraries
Note that if you need any other libraries or extensions, they must be installed separately per PHP version. For example, if you need cURL
, you would need to install it for all versions.
3. Configure Apache
We need to add some Apache modules using a2enmod
.
Ubuntu 18.04 users.
Ubuntu 16.04 users.
Restart Apache.
You can now use either Virtual Hosts or .htaccess
to instruct Apache which version of PHP to use.
4. .htaccess Method
You can add the <FilesMatch>
directive to your .htaccess
file. Before you do, make sure that AllowOverride
is enabled in Virtual Hosts, otherwise Apache will ignore .htaccess
.
Open the Apache config file.
Scroll down the the following section and make sure that AllowOverride
is set to All
.
Save and exit (press CTRL
+ X
, press Y
and then press ENTER
)
Restart Apache.
Now you can add the <FilesMatch>
directive to .htaccess
Examples for different versions of PHP in .htaccess:
Change version number to 7.1/7.2/7.3 for different PHP versions.
5. Check PHP info
To see which version of PHP Apache is serving, create a new file called info.php
in your web document root.
In this example, we will create a new file in /var/www/html/
Enter the following PHP code.
Save file and exit. (Press CTRL
+ X
, press Y
and then press ENTER
)
We can now load this file in the browser by going to http://example.com/info.php
or http://your_ip/info.php
Below we can see the PHP info page with the PHP version clearly displayed.
Don’t forget to delete info.php
as it contains information that could be useful to hackers.
Let me know in the comments if this helped.
No comments:
Post a Comment