Gentoo Linux and nginx with PHP-FPM
PHP just added the FPM patch to their 5.3 branch, making it likely that PHP 5.3.3 will support the FPM SAPI. The FPM SAPI is an improved fcgi SAPI that allows for more advanced configuration than the original fcgi SAPI. One of the most useful features is process management, which makes it very useful for lightweight webservers, such as nginx, that does not handle process management of fcgi themselves. Gentoo has lately been behind on the PHP support, with PHP 5.3 still being p.masked. But work is being done to rectify that. FPM being a new SAPI and all, requires some work with the ebuilds as well as aditional testing. Recently, I therefore added a PHP snapshot ebuild written by Tsisaruk V., which includes support the new FPM SAPI, to the php overlay. Setting up nginx (or Lighttpd) with PHP-FPM under Gentoo is fairly straightforward. For those of you who want a more thorough explanation of FPM and how to configure it, read this excellent post by Mike Willbanks.
First obtain the snapshot ebuild from the php overlay:
#install layman. Make sure the git USE flag is enabled emerge -av layman layman -fa php
Emerge nginx and php
#The package is probably both masked and ~arched. #Unmask and keyword as appropriate echo "dev-lang/php ~amd64" >> /etc/package.keywords echo "dev-lang/php" >> /etc/package.unmask #Make sure these useflags are enabled echo "dev-lang/php fpm" >> /etc/package.use echo "www-servers/nginx fastcgi" >> /etc/package.use #Emerge! emerge -av nginx php
The PHP snapshot ebuild comes with a fully functioning php-fpm.conf file installed into /etc/php/fpm-php/php-fpm.conf. However, you might want to change the user and group parameters to something else than nobody. Personally, I changed it to nginx, but you might want to consider creating a new user for your web application. The default nginx config requires a few additional lines in order to work with fastcgi. Open /etc/nginx/nginx.conf and change the file to the following:
...
server {
listen 127.0.0.1;
server_name localhost;
access_log /var/log/nginx/localhost.access_log main;
error_log /var/log/nginx/localhost.error_log info;
root /var/www/localhost/htdocs;
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
}
}
...
Once this is done, start nginx and php-fpm through their init scripts
/etc/init.d/nginx start /etc/init.d/php-fpm start
If you experience any problems with the ebuilds, you ask for help in the gentoo-php IRC channel or post a bug on the Gentoo Bugzilla.
hi, thanks for your php-fpm ebuild.
this is what i saw when trying to emerge php
— Invalid atom in /var/lib/layman/php/profiles/package.mask: =>dev-lang/php-5.3.3_rc1
and only original php 5.2 is available. i can’t go any further.
thanks again for your help
Hi! Thanks for notifying me about the bug.
It has been fixed now.
But the issue you have been struggling with is really about unmasking PHP 5.3. I assume this is the problem since you also do not have PHP 5.3.2 that can also be found in portage.
I’ll add a few lines in the post explaining how to do the unmasking and keywording.
Cheers!
Thank you very much, but I didn’t manage to get php emerged.
I did a synchronization to refresh my local layman portage, then unmasked dev-lang/php with fpm enable. What differs from your post is that I use ~x86 instead of ~amd64 as my gentoo is in 32-bit.
I noticed that the source was built completely and successfully, but it failed after “Don’t forget to run make test” when trying to start build_precommand.php. This operation was stopped and recorded in sandbox log as below.
———————————————————
F: open_wr
S: deny
P: /usr/share/snmp/mibs/.index
A: /usr/share/snmp/mibs/.index
R: /usr/share/snmp/mibs/.index
C: /var/tmp/portage/dev-lang/php-5.3.3_rc1/work/php-5.3.3RC1/sapi/cli/php -n -d open_basedir= -d output_buffering=0 -d memory_limit=-1 -d phar.readonly=0 -d safe_mode=0 /var/tmp/portage/dev-lang/php-5.3.3_rc1/work/php-5.3.3RC1/ext/phar/build_precommand.php
———————————————————
I’m not sure what the problem is or whether I did something wrong. Would you please give me a hand?
Hi again, Alfred.
If you are on x86, you are doing the correct thing. I assumed that Gentoo users would be clever enough to figure that out, as you did
Regarding your problem, it is a known issue (Bug 324739) and it affects not only many versions of PHP, but also other packages (see this forum post)
I am not sure what the correct solution for you is since I haven’t encountered this problem myself. But take a look at the links I just gave you. Maybe you can figure something out. My guess would be to stop snmpd and maybe disable the snmp USE flag if you have it enabled.
Sorry I could not be of more help.
Cheers!
It works!
I disabled snmp USE flag and it works. In fact, snmpd was emerged when first time I tried to emerge php with snmp enabled, and there’s no available configuration for snmpd to start with.
Thanks a million
Is it possible to use php-fpm also with lighttpd?
Hi Liz!
FPM is just like any other FastCGI implementation, so it should work fine with lighttpd. In contrast to nginx, lighty is able to manage fcgi processes. Since nginx does not have such a process manager, the SAPIs has to handle it themselves. The old PHP FCGI SAPI did not. Of course, FPM has other advantages than just managing processes, such as more advanced process management and logging of slow requests.
The configuration should not be much more different than when using the old SAPI, which is described in the Lighttpd docs. The difference would be that you don’t let lighty deal with process spawning, so omit
"socket"and thePHP_FCGI_*environment variables.If you like, I can write another post on how to set PHP-FPM with Lighttpd.
Cheers!