I have a Gentoo Linux web server that serves a few PHP-based web applications using Apache. Most of the applications will run on the latest version of PHP (i.e. 7.2); however, one application needs to run on a very old version of PHP (i.e. 5.6). How can I run multiple versions of PHP simultaneously on the same web server?

  • Gentoo (profile 17).
  • Web Server is Apache 2.4.
  • Need to run PHP 7.2 and PHP 5.6.
  • Apache is currently configured to serve PHP 7.2 via mod_php but PHP 5.6 is also installed.
  • Gentoo is configured to use eselect to change which version of PHP is used for Apache/mod_php.

1 Answer 1


This answer is specifically for Gentoo with Profile 17. It will probably work with a few versions plus and minus. I used Red Hat's PHP Configuration Tips page for inspiration. The same idea should work for other Linux distributions (of course the specific commands will differ).

The solution is to use PHP-FPM (FastCGI Process Manager). Each version of PHP gets its own PHP-FPM service that listens on a socket. Apache passes the PHP execution requests to a particular PHP-FPM service depending on which version of PHP is required.

You can use mod_php for one version of PHP and PHP-FPM for the other(s) but I decided to drop mod_php altogether. First, I read somewhere that you can save some memory overhead by disabling mod_php. Second, the solution seemed a little cleaner with all versions of PHP being handled in the same way. Third, it is always better to have the least amount of/only the necessary software installed from a security perspective.

  1. Change your USE flags for dev-lang/php to add fpm and remove apache2.

    Current Gentoo convention is to create a file like /etc/portage/package.use/php and specify the USE flags here. If you specify exact versions in the file, make sure all versions of PHP you want to use pick up the these USE flags.

  2. Install/reinstall the versions of PHP you are going to be using.

# emerge -av =dev-lang/php-7.2.3
# emerge -av =dev-lang/php-5.6.7
  1. Change your APACHE2_MODULES variable to build the proxy and proxy_fcgi Apache modules.

    You specify what Apache modules using by adding/updating the APACHE2_MODULES variable in /etc/portage/make.conf. Like USE flags, Gentoo has some defaults already set-up for your profile. Unlike USE flags, it seems that you need to explicitly tell Gentoo to start with the defaults by prepending $APACHE2_MODULES to the variable:

  1. Reinstall Apache.
# emerge -av apache
  1. Install PHP-FPM.
# emerge -av php-fpm
  1. Create PHP-FPM startup scripts for each version of PHP you are using with Apache.

    The PHP-FPM installation creates a single startup script (/etc/init.d/php-fpm) that allows you to to choose the version of PHP to run using eselect or by changing the script's file name suffix (e.g. php-fpm-php5.6, php-fpm-php7.2, etc.). Since we would only want to use eselect if we only wanted to use a single version of PHP at a time or if we wanted to use no more than two versions with mod_php for one and PHP-FPM for the second, we will use the suffix method. Instead of copying the script, we want to symlink it (valid suffixes are those directory names from ls -ld /usr/lib64/php* e.g. php5.6, php7.2, etc.):

# cd /etc/init.d
# ln -s php-fpm php-fpm-php5.6
# ln -s php-fpm php-fpm-php7.2
  1. Change the PHP-FPM configuration so that each PHP version's service runs on a different port.

    The specific file names vary between versions but you want to edit a config file for each PHP version in these general locations: /etc/php/fpm-php*/*.conf so that the listen directive has a different port for each.

  2. Configure PHP-FPM services to start.

# rc-update add php-fpm-php5.6 default
# rc-update add php-fpm-php7.2 default
# /etc/init.d/php-fpm-php5.6 start
# /etc/init.d/php-fpm-php7.2 start
  1. Edit the Apache start-up configuration to enable proxy modules.

    Edit /etc/conf.d/apache2 to add -D PROXY and remove -D PHP from APACHE2_OPTS.

  2. Edit the Apache configuration to tie specific directory locations to specific instances of PHP-FPM.

    There are many ways to handle this depending on your specific Apache configuration. I decided to edit the <Directory> entry associated with my web root so that the default PHP handler was version 7.2 by adding:

<FilesMatch \.php$>
    SetHandler "proxy:fcgi://" # <-- Note port num.

With PHP 5.6 used only by exception, I added <Directory> directives for each application that needed to use the old version of PHP:

<Directory /var/www/oldapp>
    <FilesMatch \.php$>
        Sethandler "proxy:fcgi:/" # <-- Note diff port num.
  1. Add index.php as a valid directory index file.

    In Gentoo /etc/apache2/modules.d/70_mod_php.conf includes a directive to add index.php as a directory index file in the same way index.html usually is. Since we disabled mod_php in Step 9, 70_mod_php.conf no longer exists to set this up. To fix this, add DirectoryIndex index.php to the Apache configuration. Otherwise, if you visit paths like /oldapp/ you will get a 403 error.

  2. Restart Apache.

# /etc/init.d/apache restart

Other Notes

  • If you set-up specific configuration settings in your php.ini file (e.g. max POST values, timezone, etc), you will need to set these in all of your /etc/php/fpm-php*/php.ini files (for each version).
  • PHP-FPM defaults to running as user=nobody / group=nobody. You may need to change these (in /etc/php/fpm-php*/php-fpm.conf) to match whatever your web server is running.
  • To handle multiple virtual hosts, perform Steps 10 and 11 for each.
  • 1
    This was a lot of work for no upvotes. The perils of helping out uncommon distros. :-) Jul 23, 2019 at 13:29
  • 1
    @MyrddinEmrys: Ha! Yeah. FWIW, the package maintainer mentioned on one of the forums that they added a link to my answer on their wiki. I can't be the only one running Gentoo wanting more than one version of PHP, can I? Well, at least future me will benefit from this when I need to do it again in a few years! LOL Jul 24, 2019 at 18:47
  • Gentoo has masked php packages all together and it doesn't look supported anymore, so if you want php with gentoo it directs you to the php website: php.net/manual/en/install.php which puts you waist deep in the unix weeds. Upvote for effort. Ubuntu 20.04 works fine with php7.4, but those are binaries. Nov 19, 2022 at 0:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .