<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>HostingFu &#187; fastcgi</title>
	<atom:link href="http://hostingfu.com/tag/fastcgi/feed" rel="self" type="application/rss+xml" />
	<link>http://hostingfu.com</link>
	<description>Web Hosting Blog by a Software Developer</description>
	<lastBuildDate>Mon, 19 Jul 2010 09:27:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Keeping Your PHP/FastCGI Processes Alive</title>
		<link>http://hostingfu.com/article/keeping-your-php-fastcgi-processes-alive</link>
		<comments>http://hostingfu.com/article/keeping-your-php-fastcgi-processes-alive#comments</comments>
		<pubDate>Sat, 30 May 2009 04:10:08 +0000</pubDate>
		<dc:creator>scotty</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[fastcgi]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://hostingfu.com/?p=188</guid>
		<description><![CDATA[A few days ago I spotted this forum post on Linode Forum &#8212; someone&#8217;s Nginx to PHP FastCGI set up kept on throwing 502 Gateway Error after a few days. I have a mystery and I&#8217;m not sure how to solve it. My 360 Linode is running several WordPress-based sites using nginx. Everything is great, [...]]]></description>
			<content:encoded><![CDATA[<p>A few days ago I spotted <a href="http://www.linode.com/forums/viewtopic.php?t=4234">this forum post</a> on Linode Forum &#8212; someone&#8217;s Nginx to PHP FastCGI set up kept on throwing <b>502 Gateway Error</b> after a few days.</p>
<blockquote><p>I have a mystery and I&#8217;m not sure how to solve it. My 360 Linode is running several WordPress-based sites using nginx. Everything is great, except I find that, every few days, fastcgi fails and I start getting 502 gateway errors when I try to post a comment or upload an image, etc. Restarting fastcgi fixes the problem.</p></blockquote>
<p>It looks like a very similar issue I had with Ubuntu 8.04 (and PHP 5.2.4), which was fixed when I <a href="http://hostingfu.com/article/debian-gnu-linux-5-lenny-released">migrated to Debian 5 Lenny</a>. It appears that the OP is indeed running the problematic Ubuntu 8.04. Well. I still have <b>no idea</b> what the real issue was (thus do not know how to report a bug), except my PHP FastCGI process will crash multiple times during busy time of the day (but it&#8217;s only 2-3 requests/second average).</p>
<p><span id="more-188"></span></p>
<p>Before switching to Lenny, I have tested multiple methods to keep my PHP FastCGI processes alive, including running check/restarting script every 5 minutes, etc. However at the end even 5 minutes downtime felt a bit excessive, so I wrote a simple daemon in Python to keep an eye on Nginx&#8217;s error log, and restart PHP/FastCGI processes whenever something bad has been detected. The problem here is that the PHP/FastCGI processes actually do not die &#8212; Nginx simply cannot communicate with them. So I need to restart PHP whenever Nginx splits a dummy.</p>
<p><del>I put the code on <b>Linode Pastebin here</b></del>. <ins>Linode has appeared to remove that script from their Pastebin, but you can <a href="http://hostingfu.com/files/python/phpmonitor.py"><b>Download it here</b></a></ins>, modify some variables in the code (for example the email addresses, start/stop script location, etc), and then run it as root.</p>
<p>What it does:</p>
<ul>
<li>It will damonize first.</li>
<li>Scanning <code>/var/log/nginx/error.log</code> every 2 seconds, using similar algorithm as <code>tail -f</code>.</li>
<li>If &#8220;104: Connection reset by peer&#8221; or &#8220;111: Connection refused&#8221; is detected in the log,
<ul>
<li>Run <code>killall -9 php-cgi</code></li>
<li>Run <code>/etc/init.d/spawn-fcgi-php start</code> to restart the PHP FastCGI processes</li>
</ul>
</li>
</ul>
<p>It should use minimum memory + CPU/IO (around 3.5MB RSS, and stat calls are pretty fast). At least it keeps me peace at night that I know if my PHP processes are crashing, there&#8217;s a guardian to bring them back up. It still runs on my VPS at Linode &#8212; even though with Debian 5 I am getting crashes once every 2 weeks instead of 4-5 times a day.</p>
<p>Hopefully it will be useful to some.</p>
]]></content:encoded>
			<wfw:commentRss>http://hostingfu.com/article/keeping-your-php-fastcgi-processes-alive/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Use spawn-fcgi for lighttpd + PHP-FastCGI</title>
		<link>http://hostingfu.com/article/use-spawn-fcgi-for-lighttpd-php-fastcgi</link>
		<comments>http://hostingfu.com/article/use-spawn-fcgi-for-lighttpd-php-fastcgi#comments</comments>
		<pubDate>Sat, 20 May 2006 04:00:42 +0000</pubDate>
		<dc:creator>scotty</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[fastcgi]]></category>
		<category><![CDATA[lighttpd]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://hostingfu.com/?p=21</guid>
		<description><![CDATA[I had a problem with Lighttpd and PHP on the default Gentoo install. The configuration that comes with Gentoo, and the &#8220;official recommendation&#8221; use bin-ath in the mod_fastcgi to point to PHP&#8217;s CGI executable. For example, fastcgi.server = ( ".php" => ( "localhost" => ( "bin-path" => "/usr/bin/php-cgi", "host" => "127.0.0.1", "port" => 1026 ) [...]]]></description>
			<content:encoded><![CDATA[<p>I had a problem with <a href="http://www.lighttpd.org/">Lighttpd</a> and <a href="http://www.php.net/">PHP</a> on the default Gentoo install. The configuration that comes with Gentoo, and the &#8220;<a href="http://trac.lighttpd.net/trac/wiki/TutorialLighttpdAndPHP">official recommendation</a>&#8221; use <code>bin-ath</code> in the <code>mod_fastcgi</code> to point to PHP&#8217;s CGI executable.</p>
<p><span id="more-21"></span></p>
<p>For example,</p>
<pre class="code">
fastcgi.server = (
    ".php" => (
        "localhost" => (
            "bin-path"  => "/usr/bin/php-cgi",
            "host" => "127.0.0.1",
            "port" => 1026
        )
    )
)
</pre>
<p>When lighttpd starts, <code>lighttpd</code> process itself will spawn multiple <code>php-cgi</code> processes, and communicate with them on port 1026 using the FastCGI protocol. Whenever a PHP script needs to be executed, one of spawned <code>php-cgi</code> child will take over the execution.</p>
<p>It is all fine &#8212; until the <em>restart time</em>.</p>
<p>There are some issues with lighttpd&#8217;s restarting/reloading. First of all, in my experience lighttpd does not always handle reloading properly via the standard SIGHUP. The init script on Gentoo nor implemented &#8220;reload&#8221; &#8212; logrotate script basically relies on the service to restart <em>(<b>Update</b>: logrotate is now using SIGHUP after lighttpd 1.4.11 has been emerged)</em>. However, due to the way lighttpd implemented <a href="http://blog.lighttpd.net/articles/2005/09/02/graceful-restart">graceful restart</a>, sometimes I found the FastCGI processes (in this case <code>php-cgi</code>) do not get restarted together with lighttpd. I think it is how it happens.</p>
<ol>
<li>Run &#8220;<code>/etc/init.d/lighttpd restart</code>&#8220;, which sends a SIGINT to the old process, and then fire up a new lighttpd process.</li>
<li>Old lighttpd process received SIGINT, unbind the HTTP port to stop accepting new connections, however it cannot be terminated straight away because some of its PHP sub-processes are still finishing up their requests.</li>
<li>New lighttpd process started, and bind itself to HTTP/HTTPS port which are now available. However, it cannot spawn the <code>php-cgi</code> FastCGI process, because port 1026 is still occupied by the <code>php-cgi</code> processes from the old lighttpd instance.</li>
<li>New, old lighttpd process died after all its <code>php-cgi</code> children have finished their tasks. However the new lighttpd process is still left child-less. All new PHP requests will return 500 Internal Error.</li>
</ol>
<p>I had that happening this morning, and this VM stopped serving PHP scripts for almost 4 hours, until I restarted lighttpd again! It has actually happened quite a few times before, but being procrastinating in nature, I only managed to &#8220;do something about it&#8221; today.</p>
<p>The solution, for me at least, is to use the <code>spawn-fcgi</code> script that also gets installed when you emerged lighttpd. It starts <code>php-cgi</code> <strong>independently</strong> from lighttpd, so that restarting lighttpd process do not have to restart all PHP FastCGI processes.</p>
<p>The configuration file can be found in <code>/etc/conf.d/spawn-fcgi</code>, and its default setting is quite reasonable for PHP. It starts 5 PHP FastCGI instance when it starts, and binds to TCP port 1026. First of all, you need to modify your lighttpd&#8217;s <code>mod_fastcgi.conf</code> file so that it would not try to look for local executable. All you need to do is to comment out the line where <code>bin-path</code> is specified. For example,</p>
<pre class="code">
fastcgi.server = (
    ".php" => (
        "localhost" => (
#           "bin-path"  => "/usr/bin/php-cgi",
            "host" => "127.0.0.1",
            "port" => 1026
        )
    )
)
</pre>
<p>And then you need to restart lighttpd. <code>spawn-fcgi</code> comes with its own init script, so you&#8217;ll also need to do</p>
<pre class="code">
<span style="color:red;">localhost</span> ~ # /etc/init.d/spawn-fcgi start
 <span style="color:#51fb49">*</span> Starting spawn-fcgi ...        [ <span style="color:#51fb49">ok</span> ]
<span style="color:red;">localhost</span> ~ # rc-update add spawn-fcgi default
 <span style="color:#51fb49">*</span> spawn-fcgi added to runlevel default
 <span style="color:#51fb49">*</span> rc-update complete.
<span style="color:red;">localhost</span> ~ #
</pre>
<p>Yup &#8212; remember to use <code>rc-update</code> to add <code>spawn-fcgi</code> to your start up script.</p>
<p>Now starting/stopping lighttpd would not affect starting and stopping of PHP FastCGI processes.</p>
]]></content:encoded>
			<wfw:commentRss>http://hostingfu.com/article/use-spawn-fcgi-for-lighttpd-php-fastcgi/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
