Hi all
I’m running PHP5 & nginx on a number of servers now, and I’m really
happy with everything… except one item which is starting to bug me.
Basically, on all the servers I run PHP5 by issuing the following
command from a shell script:
sudo -u nginx sh -c ‘PHP_FCGI_MAX_REQUESTS=50 PHP_FCGI_CHILDREN=4
/usr/local/bin/php-cgi -b 10005 &’
Which works fine, other than it doen’t start php on boot. Its getting
annoying because if the server reboots I have to manually start PHP.
Has anyone got a happy php-fastcgi install which doesn’t use spawn-fcgi?
–
Phillip B Oldham
The Activity People
[email protected] mailto:[email protected]
Policies
This e-mail and its attachments are intended for the above named
recipient(s) only and may be confidential. If they have come to you in
error, please reply to this e-mail and highlight the error. No action
should be taken regarding content, nor must you copy or show them to
anyone.
This e-mail has been created in the knowledge that Internet e-mail is
not a 100% secure communications medium, and we have taken steps to
ensure that this e-mail and attachments are free from any virus. We must
advise that in keeping with good computing practice the recipient should
ensure they are completely virus free, and that you understand and
observe the lack of security when e-mailing us.
Use php-fpm - you won’t ever go back 
Renaud Allard wrote:
php-fpm is probably the best way to start php as a cgi.
http://php-fpm.anight.org/
Just compiling support now, but the documentation’s a little light. Once
its finished, how would I use it?
–
Phillip B Oldham
The Activity People
[email protected] mailto:[email protected]
Policies
This e-mail and its attachments are intended for the above named
recipient(s) only and may be confidential. If they have come to you in
error, please reply to this e-mail and highlight the error. No action
should be taken regarding content, nor must you copy or show them to
anyone.
This e-mail has been created in the knowledge that Internet e-mail is
not a 100% secure communications medium, and we have taken steps to
ensure that this e-mail and attachments are free from any virus. We must
advise that in keeping with good computing practice the recipient should
ensure they are completely virus free, and that you understand and
observe the lack of security when e-mailing us.
Phillip B Oldham wrote:
Which works fine, other than it doen’t start php on boot. Its getting
annoying because if the server reboots I have to manually start PHP.
Has anyone got a happy php-fastcgi install which doesn’t use spawn-fcgi?
php-fpm is probably the best way to start php as a cgi.
http://php-fpm.anight.org/
Phillip B Oldham wrote:
Which works fine, other than it doen’t start php on boot. Its getting
annoying because if the server reboots I have to manually start PHP.
Has anyone got a happy php-fastcgi install which doesn’t use spawn-fcgi?
Here are some examples. They do create named sockets per user.
Linux:
#!/bin/ash
chkconfig: 234 25 75
description: php FastCGI daemon
case $1 in
restart)
/etc/init.d/phpFcgid stop
/etc/init.d/phpFcgid start
;;
start)
users=“www”
www_childs=‘2’
for user in ${users}; do
socketdir="/tmp/.fastcgi.${user}"
mkdir -p ${socketdir}
chown ${user}:nobody ${socketdir}
chmod 0750 ${socketdir}
eval export PHP_FCGI_CHILDREN=${${user}_childs}
su -m ${user} -c “/opt/php5/bin/php-cgi -b
${socketdir}/socket&”
done
;;
stop) /usr/bin/pkill php-cgi ;;
esac
FreeBSD:
#!/bin/sh
PROVIDE: phpFcgid
REQUIRE: LOGIN
KEYWORD: shutdown
. /etc/rc.subr
name=“phpFcgid”
rcvar=set_rcvar
load_rc_config $name
: ${phpFcgid_enable=“NO”}
: ${phpFcgid_users=“www”}
: ${phpFcgid_children=“2”}
start_cmd=phpFcgid_start
stop_cmd=phpFcgid_stop
phpFcgid_start() {
echo “Starting $name.”
export PHP_FCGI_CHILDREN=${phpFcgid_children}
for user in ${phpFcgid_users}; do
socketdir="/tmp/.fastcgi.${user}"
mkdir -p ${socketdir}
chown ${user}:www ${socketdir}
chmod 0750 ${socketdir}
su -m ${user} -c “/usr/local/bin/php-cgi -b
${socketdir}/socket&”
done
}
phpFcgid_stop() {
echo “Stopping $name.”
pids=pgrep php-cgi
pkill php-cgi
wait_for_pids $pids
}
run_rc_command “$1”
Also, I think there may be a bug with the original script
/etc/init.d/phpFcgid start
su -m ${user} -c "/opt/php5/bin/php-cgi -b
${socketdir}/socket&"
done
;;
stop) /usr/bin/pkill php-cgi ;;
esac
On my CentOS box, service phpFcgid start returns the following:
env: /etc/init.d/phpFcgid: No such file or directory
–
Phillip B Oldham
The Activity People
[email protected] mailto:[email protected]
Policies
This e-mail and its attachments are intended for the above named
recipient(s) only and may be confidential. If they have come to you in
error, please reply to this e-mail and highlight the error. No action
should be taken regarding content, nor must you copy or show them to
anyone.
This e-mail has been created in the knowledge that Internet e-mail is
not a 100% secure communications medium, and we have taken steps to
ensure that this e-mail and attachments are free from any virus. We must
advise that in keeping with good computing practice the recipient should
ensure they are completely virus free, and that you understand and
observe the lack of security when e-mailing us.
you can just run php with lighttpd spawn-fcgi, and daemontools for
monitoring it. i find it rock solid.
Thanks Volodymyr.
How would one add in PHP_FCGI_MAX_REQUESTS to such scripts?
Volodymyr K. wrote:
/usr/local/bin/php-cgi -b 10005 &’
#!/bin/ash
users=“www”
;;
start_cmd=phpFcgid_start
su -m ${user} -c "/usr/local/bin/php-cgi -b
run_rc_command “$1”
–
Phillip B Oldham
The Activity People
[email protected] mailto:[email protected]
Policies
This e-mail and its attachments are intended for the above named
recipient(s) only and may be confidential. If they have come to you in
error, please reply to this e-mail and highlight the error. No action
should be taken regarding content, nor must you copy or show them to
anyone.
This e-mail has been created in the knowledge that Internet e-mail is
not a 100% secure communications medium, and we have taken steps to
ensure that this e-mail and attachments are free from any virus. We must
advise that in keeping with good computing practice the recipient should
ensure they are completely virus free, and that you understand and
observe the lack of security when e-mailing us.
Phillip B Oldham wrote:
Thanks Volodymyr.
How would one add in PHP_FCGI_MAX_REQUESTS to such scripts?
Dunno, maybe like this:
case $1 in
restart)
/etc/init.d/phpFcgid stop
/etc/init.d/phpFcgid start
;;
start)
users=“www”
www_childs=‘2’
www_maxrequests=‘1000’
for user in ${users}; do
socketdir="/tmp/.fastcgi.${user}"
mkdir -p ${socketdir}
chown ${user}:nobody ${socketdir}
chmod 0750 ${socketdir}
eval export PHP_FCGI_CHILDREN=\${${user}_childs}
eval export PHP_FCGI_MAX_REQUESTS=\${${user}_maxrequests}
: ${phpFcgid_enable=“NO”}
: ${phpFcgid_users=“www”}
: ${phpFcgid_children=“2”}
: $phpFcgid_maxrequests=“0”}
start_cmd=phpFcgid_start
stop_cmd=phpFcgid_stop
phpFcgid_start() {
echo “Starting $name.”
export PHP_FCGI_CHILDREN=${phpFcgid_children}
if [ “${phpFcgid_maxrequests}” -ne “0” ]; then
export PHP_FCGI_MAX_REQUESTS="${phpFcgid_maxrequests}"
phpFcgid_stop() {
echo “Stopping $name.”
pids=pgrep php-cgi
pkill php-cgi
wait_for_pids $pids
}
run_rc_command “$1”
I personally don’t see any point setting 'em.
Stefanita Rares Dumitrescu wrote:
you can just run php with lighttpd spawn-fcgi, and daemontools for
monitoring it. i find it rock solid.
I’ve had nothing but problems with that approach. From what I
understand, there’s a couple of bug in the spawn-fcgi binary, one with
memory leaks and another with race-conditions occuring when the PHP
child hits its upper limit of 250 requests at which point it is forced
to restart; it still accepts requests even though its restarting and
fails to complete. All we were seeing was regular 500 errors from lighty
and semi-regular ones from nginx. Starting PHP with the -b switch fixed
all those problems.
–
Phillip B Oldham
The Activity People
[email protected] mailto:[email protected]
Policies
This e-mail and its attachments are intended for the above named
recipient(s) only and may be confidential. If they have come to you in
error, please reply to this e-mail and highlight the error. No action
should be taken regarding content, nor must you copy or show them to
anyone.
This e-mail has been created in the knowledge that Internet e-mail is
not a 100% secure communications medium, and we have taken steps to
ensure that this e-mail and attachments are free from any virus. We must
advise that in keeping with good computing practice the recipient should
ensure they are completely virus free, and that you understand and
observe the lack of security when e-mailing us.
Phillip B Oldham wrote:
/etc/init.d/phpFcgid stop
eval export PHP_FCGI_CHILDREN=\${${user}_childs}
su -m ${user} -c "/opt/php5/bin/php-cgi -b
${socketdir}/socket&"
done
;;
stop) /usr/bin/pkill php-cgi ;;
esac
On my CentOS box, service phpFcgid start returns the following:
env: /etc/init.d/phpFcgid: No such file or directory
This script is actually the contents of /etc/init.d/phpFcgid. To start
using it you should put it there, make it runnable and register it with
chkconfig. You should also have php installed with FastCgi support and
you can build it alone as I do, just in my case php is installed with
prefix /opt/php5.
Yep, I did all that - still got that message.
Thanks for the script though. I’m currently mashing bits together from
your script, the one which starts nginx, and a few other bits. Once its
running I’ll drop it onto the mailing list for prosperity. 
Volodymyr K. wrote:
restart)
chmod 0750 ${socketdir}
This script is actually the contents of /etc/init.d/phpFcgid. To start
using it you should put it there, make it runnable and register it
with chkconfig. You should also have php installed with FastCgi
support and you can build it alone as I do, just in my case php is
installed with prefix /opt/php5.
–
Phillip B Oldham
The Activity People
[email protected] mailto:[email protected]
Policies
This e-mail and its attachments are intended for the above named
recipient(s) only and may be confidential. If they have come to you in
error, please reply to this e-mail and highlight the error. No action
should be taken regarding content, nor must you copy or show them to
anyone.
This e-mail has been created in the knowledge that Internet e-mail is
not a 100% secure communications medium, and we have taken steps to
ensure that this e-mail and attachments are free from any virus. We must
advise that in keeping with good computing practice the recipient should
ensure they are completely virus free, and that you understand and
observe the lack of security when e-mailing us.
I’ve had problems with spawn-fcgi. It completely ignores
PHP_FCGI_MAX_REQUESTS and never actually terminates the children… I
noticed this on every box I had.
Why are people still using the wrong tools for the job anyway? PHP-FPM
does it properly, and the latest development version even allows for
strict process control (a proper max_execution_time that uses normal
seconds, not CPU seconds) and will soon allow for apache style child
process spawning - so it will adapt to your load.
spawn-fcgi is useless. If you can’t use PHP-FPM, all you really need
to do is run
set the two environment variables, and then:
sudo -u $user /usr/local/bin/php-cgi -b $port
I put this in an upstart file and it worked great. It also recycled
engines properly and when I killed the parent the children died. I
assume this could be done the same way with daemontools since it runs
like an init process (upstart, inittab or daemontools should all
support this) - I had issues also with spawn-fcgi not killing the
children when I killed the parent, which is kind of against the point
to begin with
PHP has built-in fastcgi management, its just not
robust… so people use tools like spawn-fcgi which are not required.
PHP-FPM makes that more robust and works so amazingly well - I run it
on all my boxes now and will -never- go back to anything else. With
luck, Andrei will get it into PHP core too so it will become the
defacto standard for FastCGI management without any external patch
anyway.
This is what I’ve got working:
PROG=“php-cgi”
stop() {
;;
start)
start
;;
stop)
stop
;;
*)
echo $“Usage: $0 {start|stop|restart}”
esac
Suggestions welcome!
–
Phillip B Oldham
The Activity People
[email protected] mailto:[email protected]
Policies
This e-mail and its attachments are intended for the above named
recipient(s) only and may be confidential. If they have come to you in
error, please reply to this e-mail and highlight the error. No action
should be taken regarding content, nor must you copy or show them to
anyone.
This e-mail has been created in the knowledge that Internet e-mail is
not a 100% secure communications medium, and we have taken steps to
ensure that this e-mail and attachments are free from any virus. We must
advise that in keeping with good computing practice the recipient should
ensure they are completely virus free, and that you understand and
observe the lack of security when e-mailing us.
php-fpm looks interesting (although there’s quite a bit of what loooks
like russian =). I’ll be checking it out.
In the meantime, attached is what I use as an init script. Also, the
following is the default file to go with it.
START=yes
Which user runs PHP? (default: www-data)
EXEC_AS_USER=foo
Host and TCP port for FASTCGI-Listener (default: localhost:9000)
FCGI_HOST=localhost
FCGI_PORT=9000
Environment variables, which are processed by PHP
PHP_FCGI_CHILDREN=2
PHP_FCGI_MAX_REQUESTS=1000
I will ditto what Mike is saying here. It took me about 20 minutes to
get
PHP-FPM all setup and configured and running great. I looked into the
spawn-fcgi script and found lots of people with major issues such as
child
processes not respawning.
We’re actually using some home-brewed stuff that uses spawn-fcgi and
monit which works really reliably. It was somewhat painful to create
though. We’re not a php host company but we do have PHP apps come
through occasionally. Looks like we might need to investigate this.
Thanks for the tip!
you can use google translate on the php-fpm site and it makes it
understandable enough…
the sample config file is in english anyway, so just download the
patch, patch your php and then look at the example config file - you
can setup multiple fastcgi pools (for instance one for each user/group
for privilege separation - which is one of the biggest reasons I need
it)
I cannot emphasize it enough - PHP-FPM is -the- way to manage
PHP/FastCGI. anything else is hokey and should be deprecated
We
need to get more exposure for PHP-FPM so more people are managing
their FastCGI properly (also not to mention it helps mature/fix some
FastCGI issues in PHP itself)
Phillip B Oldham wrote:
Renaud Allard wrote:
php-fpm is probably the best way to start php as a cgi.
http://php-fpm.anight.org/
Just compiling support now, but the documentation’s a little light. Once
its finished, how would I use it?
Just like a normal init script:
/usr/local/sbin/php-fpm start
So basically, you just need to link it into /etc/init.d if you are using
linux, then use update-rc.d (for debian based systems) ot chkconfig (for
redhat based systems) to update the links into the different runlevels.
On Jun 13, 2008, at 3:55 AM, mike wrote:
Use php-fpm - you won’t ever go back 
Is anyone using php-fpm with php 4.4.8? php doesn’t seem to compile
once the php-fpm patch is applied.