Running Multiple Versions of PHP Using Apache on Gentoo












0















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.










share|improve this question



























    0















    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.










    share|improve this question

























      0












      0








      0








      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.










      share|improve this question














      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.







      apache-httpd php gentoo






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 1 hour ago









      Steve KalemkiewiczSteve Kalemkiewicz

      148116




      148116






















          1 Answer
          1






          active

          oldest

          votes


















          0














          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:




          APACHE2_MODULES="$APACHE2_MODULES proxy proxy_fcgi"



          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://127.0.0.1:9972" # <-- Note port num.
          </FilesMatch>


          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:/127.0.0.1:9956" # <-- Note diff port num.
          </FilesMatch>
          </Directory>




          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.






          share|improve this answer

























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "106"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505980%2frunning-multiple-versions-of-php-using-apache-on-gentoo%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            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:




            APACHE2_MODULES="$APACHE2_MODULES proxy proxy_fcgi"



            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://127.0.0.1:9972" # <-- Note port num.
            </FilesMatch>


            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:/127.0.0.1:9956" # <-- Note diff port num.
            </FilesMatch>
            </Directory>




            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.






            share|improve this answer






























              0














              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:




              APACHE2_MODULES="$APACHE2_MODULES proxy proxy_fcgi"



              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://127.0.0.1:9972" # <-- Note port num.
              </FilesMatch>


              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:/127.0.0.1:9956" # <-- Note diff port num.
              </FilesMatch>
              </Directory>




              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.






              share|improve this answer




























                0












                0








                0







                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:




                APACHE2_MODULES="$APACHE2_MODULES proxy proxy_fcgi"



                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://127.0.0.1:9972" # <-- Note port num.
                </FilesMatch>


                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:/127.0.0.1:9956" # <-- Note diff port num.
                </FilesMatch>
                </Directory>




                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.






                share|improve this answer















                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:




                APACHE2_MODULES="$APACHE2_MODULES proxy proxy_fcgi"



                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://127.0.0.1:9972" # <-- Note port num.
                </FilesMatch>


                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:/127.0.0.1:9956" # <-- Note diff port num.
                </FilesMatch>
                </Directory>




                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.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 1 hour ago

























                answered 1 hour ago









                Steve KalemkiewiczSteve Kalemkiewicz

                148116




                148116






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505980%2frunning-multiple-versions-of-php-using-apache-on-gentoo%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Loup dans la culture

                    How to solve the problem of ntp “Unable to contact time server” from KDE?

                    ASUS Zenbook UX433/UX333 — Configure Touchpad-embedded numpad on Linux