I have several virtualhosts, and I need to run different PHP versions. I have php-fpm and FastCgi installed, but how can I configure it? I'm using Ubuntu 16.04

  • If one of the answers solved your question, please mark that answer as the accepted one by clicking the gray checkmark next to up/down arrows on that answer Nov 30, 2018 at 8:35

2 Answers 2


Maybe not the response that you're waiting for, but it gets painful to manage several php versions in your machine.

I've found that the best approach is to have a newer php version (php7+) as primary, and if I need something older or another specific version, I use a container with apache/php from here or use this as base image and modify it as necessary, it has great instructions to do that.

In the case that I need an older php version (unsupported so be careful), I can just build from an older OS version with support for that particular version.

The advantages of this approach are, first, that you don't mess up your machine with lots of packages and dependencies. Second, you don't pollute your php install with all the dependencies and packages that eventually will conflict with each other due to version-incompatibility. Finally, and super importantly, dockerizing apps will give you a replicable recipe for your production environment.

Even if you don't choose this as your approach, take a look at docker. It will make your like easier and it's "the thing" used in development nowadays.


With nginx, all you have to do is use sockets via proxy_pass:

    location ~ ^/index\.php(/|$) {
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            fastcgi_param DOCUMENT_ROOT $realpath_root;

With apache you need to use proxy:

<FilesMatch "\.php$">
     SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost/"

This way, you can use different sockets (each for different version of PHP) by configuring them within each Virtual Host

  • I'm new here, where I find the config file to put these? @janmyszkier
    – s4n.ty
    Nov 30, 2018 at 8:08
  • for apache it's /etc/apache2/sites-available and for nginx /etc/nginx/sites-available Nov 30, 2018 at 8:30

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