Running Two PHP Versions on the Same Server

Running two PHP versions on the same server

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:
Sample Image

site70.local/phpinfo.php:
Sample Image

Multiple PHP versions on the same box

Yes, it is possible. In fact, many servers operate this way. You may see .php4 and .php5 extensions from time to time indicating which version that particular script should be handled with.

ServerFault addressed this question

Running php4 and php5 along side each other

How to run multiple php version in lighttd server at same time?

You can update at your fastcgi-php from conf-available folder.

$ cd /etc/lighttpd/conf-available/

Make a backup file:

$ sudo cp 15-fastcgi-php.conf 15-fastcgi-php.conf.save

Now open 15-fastcgi-php.conf and update as like:

$ sudo vi 15-fastcgi-php.conf and paste of given below code snippet:

    fastcgi.server = ( ".php" =>
((
"bin-path" => "/usr/bin/php-cgi5.6",
"socket" => "/var/run/lighttpd/php.socket",
"max-procs" => 1,
"bin-environment" => (
"PHP_FCGI_CHILDREN" => "4",
"PHP_FCGI_MAX_REQUESTS" => "10000"
),
"bin-copy-environment" => (
"PATH", "SHELL", "USER"
),
"broken-scriptfilename" => "enable"
))
)

$SERVER["socket"] == ":81" {

fastcgi.server = ( ".php" =>
((
"bin-path" => "/usr/bin/php-cgi7.0",
"socket" => "/var/run/lighttpd/php81.socket",
"max-procs" => 1,
"bin-environment" => (
"PHP_FCGI_CHILDREN" => "4",
"PHP_FCGI_MAX_REQUESTS" => "10000"
),
"bin-copy-environment" => (
"PATH", "SHELL", "USER"
),
"broken-scriptfilename" => "enable"
))
)

}

Now, save and close and enable the mod.

$ sudo lighty-enable-mod fastcgi-php

Reload and restart the server:

$ sudo systemctl force-reload lighttpd

$ sudo systemctl restart lighttpd

I hope it will work.

Running two PHP versions on the same server (STRETCH)

It took me a while to figure out what to do but at the end I found an easy solution :

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

Using two different php versions on the same machine

I usualy change the fastcgi_pass socket variable into my nginx conf file.

 location ~ \.php$ {
...

fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;

....
}

Check the sock location for your php version. Mine (using Debian 9) can be located into /var/run/php/

cd /var/run/php

I currently have both php fpm 5.6 and 7.2 installed on my system, and the list command will output both php5.6-fpm.sock and php7.2-fpm.sock files.

You could then substitute the sock pointer into the nginx file:

location ~ \.php$ {
...
fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
....
}

Remember to reload nginx afterwards:

sudo systemctl restart nginx


Related Topics



Leave a reply



Submit