I want to try php 7 and get my site working on php 7 in the same time keeping other sites working on php 5 just in case. so that from the domain virtual host config I can decide which php to run.

I looked at php version management and saw it is possible to run two versions, though the ones I found seems for apache. I'm looking for more clear instructions on how to do this under nginx. I have also hhvm running in case that matters.


Firstly, you need to install php5-fpm and php7.0-fpm from Ondřej Surý's PPA for co-installable php5 and php7.0.:

sudo apt-get install python-software-properties
sudo LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php

We then, with nginx, suggest using the FPM packages. So, install the packages accordingly:

sudo apt-get update

# We need to ***remove*** php5 packages, so we can use php5.6 packages
# from the PPA instead
sudo apt-get remove php5-common
sudo apt-get autoremove

# Now we install php5.6 packages.
sudo apt-get install php5.6-fpm

# Now, install php7.0-fpm.  You may need to install separate PHP plugins
# for databases, extensions, etc. later.
sudo apt-get install php7.0-fpm

Source: An answer on the "How to install php 7?" question, and custom comments and modifications from myself.

Secondly, nginx. nginx is only able to be as good as your configurations. You very likely have a PHP handling block on your nginx server block(s) similar to this (from the 'default' example config):

# pass the PHP scripts to FastCGI server listening on

location ~ \.php$ {
    include snippets/fastcgi-php.conf;

    # With php5-fpm:
    fastcgi_pass unix:/var/run/php5-fpm.sock;

This configuration will only pass to php5-fpm. In effect, this will apply for your entire server block. The easiest way to go about this is to have a second server block (for, say, test.domain.tld), and provide instead the PHP 7.0 fastcgi_pass destination. In php7.0 on Xenial, this is going to end up something like this (extracted from a fix recently uploaded to the Xenial nginx package), however I do not know the layout of Ondrej's package so I cannot give exact specifics there (check /etc/php7.0/fpm/pool.d/www.conf or similar to determine where it's listening):

location ~ \.php$ {
    include snippets/fastcgi-php.conf;

    # With php7.0-fpm:
    fastcgi_pass unix:/var/run/php7.0-fpm.sock;

You will have to do something similar for your php5 block, to validate that php5.6 sockets are used instead of the built-in php5 socket (which we just removed).

Then, use the second test domain to run the php7.0 testing. Make sure, though, to make a copy of your site if you want to do this - it will likely be necessary to have a separate testing copy so that once you've finished getting it working with php7.0, you can just 'switch over' without issue.

There is, unfortunately, no easy way to make php5 and php7.0 work within the same server block, not without altering your paths for your site, applications, etc. to have one for php5 and one for php7.0; such reworking of sites can get nasty, hence the suggestion to run two separate copies of the site code, one for php7.0 migration and one for php5 in production.

| improve this answer | |
  • If you pick this way, then you should stick to using the *php5.6* packages that Ondřej provides, and remove the plain php5 packages. – muru Mar 23 '16 at 13:34
  • @muru probably right, i'll write that in – Thomas Ward Mar 23 '16 at 13:49
  • Thanks! This worked better than I expected. there were problems with pear and cli at first, but turned out it was a bug in the repo I needed to update and it worked. My site works really fast on php7. it's amazing. – Dreaded semicolon Mar 31 '16 at 19:37
  • The fpm sock had a 5.6 in the name as well. – toster-cx Feb 16 '17 at 10:31
  • As of almost 2018, the php5.6 config for NGINX is fastcgi_pass unix:/var/run/php/php5.6-fpm.sock; – Ecksters Nov 28 '17 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.