I have two projects on the local server, one project is running PHP5.6 and the other one is running PHP7.0. Now would it be possible to enable these two versions based on the projects? I already tried adding AddHandler application/x-httpd-php7 .php in one of the project htaccess but it's not working. Currently, PHP7.0 and PHP5.6-fpm have already installed on the server. Below is the screenshot of the phpinfo().
8 Answers
I finally managed to run my two projects in FastCGI on different PHP versions, thanks to the guys from the PHP subreddit.
I uninstalled everything including Apache and started over again. Below are the steps I used to enable two versions of PHP on my local server. Btw, my computer is running on Linux Mint 18.
Assuming you already installed Apache, created virtual host for the two projects and added the necessary PHP PPAs. Let's call the projects
site56.localfor PHP 5.6 andsite70.localfor PHP 7.0. Installphp5.6-fpmandphp7.0-fpmby running:sudo apt-get install php5.6-fpm sudo apt-get install php7.0-fpmCreate two files under
/usr/lib/cgi-bin/(honestly I don't know if this step is still necessary), and save:sudo nano /usr/lib/cgi-bin/php56-fcgi sudo nano /usr/lib/cgi-bin/php70-fcgiOpen the php56 conf file
/etc/apache2/conf-available/php5.6-fpm.conf, add this config and save:<IfModule mod_fastcgi.c> AddHandler php56-fcgi .php Action php56-fcgi /php56-fcgi Alias /php56-fcgi /usr/lib/cgi-bin/php56-fcgi -socket /var/run/php/php5.6-fpm.sock -pass-header Authorization Action php70-fcgi /php70-fcgi Alias /php70-fcgi /usr/lib/cgi-bin/php70-fcgi -socket /var/run/php/php7.0-fpm.sock -pass-header Authorization </IfModule> <Directory /usr/lib/cgi-bin> Require all granted </Directory>Now enable the new Apache config:
sudo a2enconf php5.6-fpmIf you installed php5.6 and php5.7, make sure you disable this two and restart Apache:
sudo a2dismod php5.6 php7.0 sudo systemctl restart apache2Create a
.htaccessfile on the project that should run on php7.0 and add this handler:AddHandler php70-fcgi .phpNow create a phpinfo file on the two projects and if you see something like this, then congratulations!
PS: Make sure you enable .htaccess in your apache2.conf or httpd.conf
5 Comments
Alias takes 1-2 arguments, a fakename and a realname, or a realname in a Locationmod_php for all virtualhosts by default, and php-fpm (of a different version) for a specific <Directory>? SetHandler should have a priority, but unfortunately does not work for me (PHP module always takes over).First of all, ensure all the PHP related configuration are disabled by running the following commands:
# ls -la /etc/apache2/conf-enabled | grep php
# ls -la /etc/apache2/mods-enabled | grep php
Set up a different version of PHP-FPM for a specific site:
Add the following line to your existing VirtualHost file.
Include "conf-available/php7.2-fpm.conf"
For example,
# vim /etc/apache2/sites-enabled/symfony.local.conf
<VirtualHost *:80>
ServerName symfony.local
Include "conf-available/php7.2-fpm.conf"
ServerAdmin webmaster@localhost
DocumentRoot /var/www/symfony.local/curr/public
<Directory /var/www/symfony.local/curr/web>
AllowOverride All
</Directory>
</VirtualHost>
2 Comments
It doesn't work for debian 9 Stretch. It took me a while to firgure out what to do but at the end I found a solution which seems even easier :
sudo apt-get install php5.6-fpm
sudo apt-get install php7.0-fpm
sudo a2enconf php5.6-fpm
If you installed php5.6 and php5.7, make sure you disable this two and restart apache.
sudo a2dismod php5.6 php7.0
sudo systemctl restart apache2
At this point all of your sites must work on php 5.6.
For the sites who need php 7, add this line in the vhost :
ProxyPassMatch "^/(.*\.php(/.*)?)$" "unix:/var/run/php/php7.0-fpm.sock|fcgi://localhost/path/to/my/main/file"
It should do the trick :)
Comments
I tried to implement Makubex's solution and it worked. There is just one problem I had with a newly installed LAMP and that was the fact that fastcgi was not enabled. I just installed php again using the following script and enabled fastcgi afterwards.
sudo apt-get install libapache2-mod-fastcgi php5.6-fpm php5.6 php5.6-dev php5.6-mcrypt php5.6-mbstring php5.6-mysql php5.6-zip php5.6-gd php5.6-xml php7.1-fpm libapache2-mod-fastcgi php7.1-fpm php7.1 php7.1-dev php7.1-mbstring php7.1-mysql php7.1-zip php7.1-gd php7.1-xml php7.1-curl php7.1-intl php7.1-json php7.1-mcrypt
And then:
a2enmod actions
a2enmod fastcgi
After this, the solution worked perfectly. I got the two above from Run multiple PHP version on the same server using php-fpm and xdebug on Ubuntu 16.04
Comments
You can do this using .htaccess files
First You need to install requred FPM using
sudo apt install php7.2-fpm
Then run below command
sudo a2enmod actions alias proxy_fcgi fcgid
Then Restart Your server
sudo systemctl restart apache
Then do below changes in .htaccess file
<FilesMatch \.php$>
# Apache 2.4.10+ can proxy to unix socket
SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost/"
</FilesMatch>
Here is example of running PHP 7.2 when PHP 5.6 is enabled
1 Comment
We have got this covered in case of PHP on Ubuntu + Nginx:
To change CLI PHP version
sudo update-alternatives --set php /usr/bin/php7.2
To change Web PHP version
fastcgi_pass unix:/run/php/php7.2-fpm.sock
Where php7.2 can be replaced by the PHP version of your choice.
2 Comments
update-alternatives there are everywhere symbolic links (ln -fs) like /run/php/php8.2-fpm.sock -> /run/php/php-fpm.sock or /run/php/php5.6-fpm.sock -> /run/php/php-fpm.sock.ExecStartPost=....install /run/php/php-fpm.sock within /lib/systemd/system/php5.6-fpm.service and /lib/systemd/system/php8.2-fpm.service, ie pointing at the same path at the same time. Same for php binary of two versions.After trying to follow your directions I found a possibly easier way for the same result.
By adding the following line
ProxyPassMatch "^/php70project/.*\.php(/.*)?$" "fcgi://localhost:9070/var/www/" enablereuse=on
in the apache vhost configuration, you could change the php version inside the directory php70project in your vhost root /var/www to version 7 running as fastcgi.
The following approach for use with unix socket
ProxyPassMatch "^/php70project/.*\.php(/.*)?$" "unix:/var/run/php70fpm.sock|fcgi://localhost/var/www"
is not working as intended. Unfortunately i couldnt find the correct code to get this working but as there is no example for unix socket use with subfolder on that page https://httpd.apache.org/docs/2.4/mod/mod_proxy_fcgi.html it might just not be possible.
Comments
This is made a lot harder by all of the linux package stuff muddying the process. The easy manual, system independent way:
Build your 2 versions of PHP from source. You'll have say libphp7.so and libphp5.so in your modules directory. Just run a separate httpd.conf file for each. I ran 5 and 7 for years:
httpd-php7.conf
httpd-php5.conf
then. just put your virtual hosts that can't run 7 in the php5 config and either start them manually at boot or create the rc.d files or whatever your system uses for startup.



mod-enabledin your apache directory ? are you sure that you have both of php5.x and php7.0x enabled there ?