How to use php7-fpm and php5-fpm in parallel with Apache?

Hi,

I have been struggling to find info on the subject of how to run php7 and php5 in parallel for different vhosts using apache. The idea is to be able to test one application written for php5 in parallel with its newer version which needs php7. I have been told by someone that using php-fpm is the preferred option for this and it would work flawlessly but the guy uses nginx and couldn’t give info about Apache. Unfortunately I couldn’t find much info on the subject myself, at least not something clear.

So far the only thing I have done is:

php5 - I have had this installed for a long time, no problems whatsoever

I installed additionally:

  • php5-fpm
  • apache2-mod_fastcgi
  • found php7 and php7-fpm on devel:languages:php and planning to install it (unless someone recommends a better option) as it seems php7 is not part of the standard Leap repositories.

But before going any further I tried to look for more info. The only source which was close to openSUSE was this one (and the following page on the same site) but sadly the info there is not quite explanatory. For example they don’t explain why one should do:

chmod 1733 /var/lib/php5

when actually on my Leap 42.1 system I have:

drwxr-xr-x 2 wwwrun root 4096 Jul 21 01:19 /var/lib/php5//

The next page which describes the php-fpm configuration also seems incomplete and inaccurate. There are references to directories and files which don’t exist on the system and the links to docs for more info lead to the site of fastcgi.com which is closed. So I am kind of stuck without good docs.

Can someone please help me with that?

I still hope to get some help with this if possible.

Although I’ve never done this specifically,
The approaches described in the following 2 articles look more reasonable to me than modifying apache.conf directly.

  1. Use a PHP manager
    The top answer at this Ubuntu link (You’re asking about an application level solution, so although there are major differences in how Apache is laid out in every distro, it’s likely that a specific component within Apache could be distro-agnostic)
    http://askubuntu.com/questions/50344/how-to-have-two-version-of-php-installed-and-switch-easily

  2. Use FastCGI to implement PHP
    I recognize how this works because FastCGI is also the recommended way to implement PHP on nginx.
    If you’ve ever seen an nginx setup, you won’t see big, complex components like in Apache, everything is a very minimalistic approach. So, expect FastCGI is be very simple and direct (so shouldn’t require a separate “manager” app).
    https://cweiske.de/tagebuch/Running%20Apache%20with%20a%20dozen%20PHP%20versions.htm

Method 1 would probably integrate with how the openSUSE LAMP pattern sets up a default version of PHP.
I don’t know if Method 2 might require uninstalling PHP5 as installed by zypper/YAST and re-installing PHP in specified locations. It <might> be possible to point FastCGI to installed locations.

In any case,
I suspect that running multiple scripting environments simultaneously on Apache may have its downsides.
I know that other similar “big” web servers 10+ years ago cached runtimes and there was a big penalty whenever the runtime switched from one scripting environment to another.
I suspect that today’s script-based web servers like nginx might not suffer the same way due to their type of execution plan.

So, if this will be a production configuration with significant loads, be sure to test to understand if there are any performance hits.

Of course the best solution would be to avoid all these uncertainties and just deploy separate webservers to run different technologies.

IMO and FYI
TSU

I have been fighting this for many hours. Here is what I did:

cp /etc/php5/fpm/php-fpm.conf.default /etc/php5/fpm/php-fpm.conf

Then open /etc/php5/fpm/php-fpm.conf and uncomment this line:

error_log = /var/log/php-fpm.log

and set in the same file:

user = wwwrun
group = www
# previously both were set to nobody

Then:

# systemctl enable php-fpm.service 
# systemctl start php-fpm.service
# netstat -tapn | grep fpm
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      24669/php-fpm.conf)
# a2enmod -l
actions alias auth_basic authn_file authz_host authz_groupfile authz_core authz_user autoindex cgi dir env expires include log_config mime negotiation setenvif ssl socache_shmcb userdir reqtimeout php5 version authn_core
# a2enmod fastcgi
# a2dismod php5
# systemctl restart apache2.service
# a2enmod -l
actions alias auth_basic authn_file authz_host authz_groupfile authz_core authz_user autoindex cgi dir env expires include log_config mime negotiation setenvif ssl socache_shmcb userdir reqtimeout version authn_core fastcgi

Added the following lines before the closing in /etc/apache2/conf.d/fastcgi.conf:


AddHandler php5-fcgi .php
Action php5-fcgi /php5-fcgi
Alias /php5-fcgi /srv/www/fcgi-bin
FastCgiExternalServer /srv/www/fcgi-bin/php5-fcgi -host 127.0.0.1:9000 -pass-header Authorization

Created a file info.php:


<?php
phpinfo();
?>

with permissions 644 and put it both in /srv/www/htdocs and /srv/www/fcgi-bin

Then opened my localhost in the browser and got 403 error. Strangely - if I remove the info.php from either of the 2 directories mentioned above - I am getting 404 which means the server requires the file to be in both directories at the same time.

In short: I am unable to run even one php-fpm although I followed the guidelines which I mentioned before. I was hoping that after setting up at least one version, I would be able to simply put the other one on a different port in a separate vhost.

I wonder what I am missing. :\

Here is the full fastcgi.conf: http://paste.opensuse.org/0aa91778

To my eye (and I may be missing something),
It looks like you configured fastcgi yet you didn’t enaable Apache to run it.

Then again, you seem to be doing your own thing instead of following either of the 2 guides I suggested.
Even if you don’t follow the guides I suggested, I recommend you follow <some> guide instead of trying things blindly on your own.

TSU

You said you have never done it yourself, so I <am> following the link I shared in the beginning which is for openSUSE, not for other distros. Right now my first task is to get up and running php-fpm for at least one version of php. What do you mean I didn’t enable Apache to run it? How do I do that?

First, what setting file and dirctory permissions to 1733 does
http://www.iss.net/security_center/reference/vulntemp/file-dir-ww-perms.htm

Your link doesn’t state a date written (I hate all articles on the Internet that aren’t dated) but references to openSUSE 12.2 suggest that the article was written when systemd was first being implemented, and IIRC was using Apache 2.1 (current is Apache 2.4). That <might> mean significant architectural changes but nothing major jumps out at me as I read the reference you’re following.

What I thought was Apache not starting fastcgi was actually your stdout so although I was reading the log incorrectly, the result is the same, one important step is to verify fastcgi is successfully running.

What is your result for

systemctl status php-fpm.service

and

systemctl status apache2.service

It’s dangerous to set your file/group permissions from “nobody” to “wwrun/www” and/or related application settings - openSUSE sets up your default Apache with working settings… Resetting can introduce mistakes. I’d recommend you get things working first before doing non-critical modifications like this.

As described in your reference,
You should also test whether your fastcgi service is listening, and you might also try telnet to port 9000 to further verify

TSU

I finally found a way to run php5-fpm. The key is to enable mod_proxy and mod_fastcgi_proxy and then add in each virtual host:


ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/path/to/your/documentroot/$1
DirectoryIndex /index.php index.php

as explained here:

http://wiki.apache.org/httpd/PHP-FPM

Unfortunately when progressing to the next planned step (installing php7) YaST wants to uninstall php5 to replace it completely with php7. In other words - both seem to be impossible to use side by side, at least not without breaking dependencies which I don’t want to do. I guess my only option is to set up another php version in a virtual machine.

Thanks for the info but my question was why, not what. But that is not important any more.

Your link doesn’t state a date written (I hate all articles on the Internet that aren’t dated) but references to openSUSE 12.2 suggest that the article was written when systemd was first being implemented, and IIRC was using Apache 2.1 (current is Apache 2.4). That mean significant architectural changes but nothing major jumps out at me as I read the reference you’re following.

What I thought was Apache not starting fastcgi was actually your stdout so although I was reading the log incorrectly, the result is the same, one important step is to verify fastcgi is successfully running.

What is your result for

systemctl status php-fpm.service

and

systemctl status apache2.service

They are running successfully. That was not an issue.

It’s dangerous to set your file/group permissions from “nobody” to “wwrun/www” and/or related application settings - openSUSE sets up your default Apache with working settings… Resetting can introduce mistakes. I’d recommend you get things working first before doing non-critical modifications like this.

If I leave them to nobody FPM cannot access files correctly, so it seems it must be running as the apache user.

As described in your reference,
You should also test whether your fastcgi service is listening, and you might also try telnet to port 9000 to further verify

TSU

I showed with the netstat output that it is working.

  1. Yes, openSUSE assumes only one version of PHP is installed and won’t install different versions of PHP side by side AFAIK. The FastCGI link I provided a link to another article which describes how to install multiple PHP versions from php.net (not using the openSUSE OSS, but I assume it <might> be possible to use your currently installed).
    https://cweiske.de/tagebuch/Introducing%20phpfarm.htm

  2. I suspect that you shouldn’t have to use mod_proxy and configure each virtual server to point to your specified PHP version as the original link I gave you describes… although if you do implement mod_proxy, it opens up many new filtering options you might like (see the mod_proxy configuration documentation). Or, you may find that you can use mod_proxy as your switching mechanism to point to your desired PHP version.

TSU