Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running two PHP versions on the same server

Tags:

php

apache

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().

enter image description here

like image 997
Makubex Avatar asked Mar 09 '17 13:03

Makubex


People also ask

Can I have two versions of xampp?

As two different versions of XAMPP cannot run on the same port, we need to change to the port. Steps to change the port for the XAMPP1_8_2: Open the file HTTP CONF file > change the port from the 80 to 8080. After changing the port click on save and exit.


3 Answers

So, after searching on Google for the whole day. I managed to run my two projects in FastCgi on different php versions. Thanks to the guys from this forum.

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.

  1. Assuming you already installed Apache, created virtual host for the two projects and added the necessary php PPAs. Let's call the projects site56.local for PHP 5.6 and site70.local for PHP 7.0. Install php5.6-fpm and php7.0-fpm by running:

     sudo apt-get install php5.6-fpm  sudo apt-get install php7.0-fpm 
  2. Create 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-fcgi 
  3. Open 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> 
  4. Now enable the new apache config:

     sudo a2enconf php5.6-fpm 
  5. 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 
  6. Create a .htacces file on the project that should run on php7.0 and add this handler:

     AddHandler php70-fcgi .php 
  7. Now 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

site56.local/phpinfo.php:
enter image description here

site70.local/phpinfo.php:
enter image description here

like image 155
Makubex Avatar answered Sep 23 '22 10:09

Makubex


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> 
like image 20
Jun Hsieh Avatar answered Sep 23 '22 10:09

Jun Hsieh


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 :)

like image 34
JGL Avatar answered Sep 20 '22 10:09

JGL