<?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>IT Know-It-All &#187; php</title>
	<atom:link href="http://itkia.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://itkia.com</link>
	<description>Applications, OS, Networking, Data</description>
	<lastBuildDate>Sat, 31 Jul 2010 12:53:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Externally Spawned FastCGI for Apache a Pain</title>
		<link>http://itkia.com/externally-spawned-fastcgi-for-apache-a-pain/</link>
		<comments>http://itkia.com/externally-spawned-fastcgi-for-apache-a-pain/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 02:10:29 +0000</pubDate>
		<dc:creator>IT Know-It-All</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[fastcgi]]></category>
		<category><![CDATA[mod_fastcgi]]></category>
		<category><![CDATA[mod_fcgid]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://itkia.com/?p=62</guid>
		<description><![CDATA[Apache mod_fastcgi can use an external fastcgi server, but mingled code and content is problematic.]]></description>
			<content:encoded><![CDATA[<div>
<p>I haven&#8217;t yet converted my real web server from Apache/mod_fcgid to Apache/mod_fastgi with an externally spawned FastCGI process. It is doable and works fine, but it is a pain to configure it how I want it to work.</p>
<p>I&#8217;m not sure I can blame Apache or FastCGI. It actually makes sense how they are doing it. However, I tend to install PHP software packages like Drupal, Simple Machines Forum, Mambo/Joomla, Gallery2, WordPress and so forth. Generally these applications are built with Apache/mod_php in mind and take some tweaking to make work on other setups. But I don&#8217;t want to tweak, especially if I have to do it periodically when upgrading.</p>
<p>I switched from lighttpd/ModFastCGI to Apache/mod_php/Squid because I was tired of fiddling with lighttpd rewrites to make pretty urls for Drupal and SMF which include ready-made Apache pretty rewrite rules.</p>
<p>Apache/mod_fcgid has worked pretty well so far, and with the AddHandler directive it behaves like mod_php: Any .php files under the document root will be parsed by the PHP interpreter. The downside is that any caching&#8211;like APC&#8211;is not share across the PHP processes.</p>
<p>So I wanted to spawn my own PHP FastCGI process with several child processes sharing a cache like I did with lighttpd and then have Apache use it. That works, but the mod_fastcgi FastCgiExternalServer directive behaves much like a ScriptAlias directive: if you specify a folder, anything under that folder will be processed by the FastCGI server, so you can&#8217;t easily mix static files with PHP script files. I tried hacking my way around this with various combinations of symlinks, aliases, handlers and rewrites, and I even got a couple of the combinations to work, but I would have to make changes to each Apache virtual host (or enforce a strict naming scheme for my vhosts), and if I missed something it would be too easy to accidentally serve up the source of a .php file.</p>
<p>Now if I were making an application from scratch it would be fine to put the PHP files in a script folder and the static files elsewhere, and I could alter premade software like that with rather simple aliases, but I would have to do it for every installed instance and then re-tweak every time I updated the software. No thanks.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://itkia.com/externally-spawned-fastcgi-for-apache-a-pain/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>External FastCGI With Apache</title>
		<link>http://itkia.com/external-fastcgi-with-apache/</link>
		<comments>http://itkia.com/external-fastcgi-with-apache/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 13:52:37 +0000</pubDate>
		<dc:creator>IT Know-It-All</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[apc-cache]]></category>
		<category><![CDATA[fastcgi]]></category>
		<category><![CDATA[lighttpd]]></category>
		<category><![CDATA[mod_fastcgi]]></category>
		<category><![CDATA[mod_fcgid]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[server]]></category>

		<guid isPermaLink="false">http://itkia.com/?p=59</guid>
		<description><![CDATA[Example of Apache mod_fastcgi connecting to external server using FastCgiExternalServer directive.]]></description>
			<content:encoded><![CDATA[<div>
<p>I was able to spawn a separate PHP FastCGI server with children and have Apache connect to it. It was trickier than I thought. The big benefit is that one shared APC cache can serve all the PHP child processes and being able to use a multithreaded Apache without worrying about whether my PHP dependencies are thread safe.</p>
<p>The trick isn&#8217;t getting it working, but getting it working the way I wanted. I want &#8220;.php&#8221; files to be processed by the FastCGI server and have the other files sent by Apache. Without some tricky configuration, Apache&#8217;s mod_fastcgi can only send specified file requests or specified directories&#8211;plus all their contents&#8211;to the external FastCGI server.</p>
<p>But I am getting ahead of myself. Let me back up to my old lighttpd setup: I had lighttpd installed, and a script that launced several php-cgi processes and listened on a network socket. Lighttpd would connect to the php-cgi processes and let them handle PHP processing. Apache can do this, too, but it was hard for me to easily find out how online.</p>
<p>As it turns out, the <a href="http://redmine.lighttpd.net/projects/spawn-fcgi">spawn-fcgi</a> program from lighttpd that I used to start the FastCGI server is now a project on its own. Supposedly the mod_fastcgi developers have a launcher program, too, but I couldn&#8217;t easily find it, and I was already familiar with spawn-fcgi and was happy to see it&#8217;s being maintained. I downloaded the source package from the site, extracted it and then did the usual &#8220;./compile&#8221;, &#8220;make&#8221; and &#8220;sudo make install&#8221;. So now I have /usr/local/bin/spawn-fcgi installed.</p>
<p>There is some good info on <a href="http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ModFastCGI">lighttpd&#8217;s ModFastCGI documentation site</a> on launching a PHP server with spawn-fcgi and various helper scripts. I modified one slightly to make it use a unix socket instead of a network tcp socket:</p>
<pre>#!/bin/bash

## ABSOLUTE path to the spawn-fcgi binary
SPAWNFCGI="/usr/local/bin/spawn-fcgi"

## ABSOLUTE path to the PHP binary
FCGIPROGRAM="/usr/bin/php-cgi"

## TCP port to which to bind on localhost
FCGIPORT="1026"

## bind to unix domain socket
FCGISOCKET="/tmp/php.sock"

## number of PHP children to spawn
PHP_FCGI_CHILDREN=4

## maximum number of requests a single PHP process can serve before it is restarted
PHP_FCGI_MAX_REQUESTS=1000

## IP addresses from which PHP should access server connections
FCGI_WEB_SERVER_ADDRS="127.0.0.1"

# allowed environment variables, separated by spaces
ALLOWED_ENV="ORACLE_HOME PATH USER"

## if this script is run as root, switch to the following user
USERID=www-data
GROUPID=www-data

################## no config below this line

if test x$PHP_FCGI_CHILDREN = x; then
  PHP_FCGI_CHILDREN=5
fi

export PHP_FCGI_MAX_REQUESTS
export FCGI_WEB_SERVER_ADDRS

ALLOWED_ENV="$ALLOWED_ENV PHP_FCGI_MAX_REQUESTS FCGI_WEB_SERVER_ADDRS"

### This if-then-else is for opening a network TCP port
#if test x$UID = x0; then
#  EX="$SPAWNFCGI -n -p $FCGIPORT -f $FCGIPROGRAM -u $USERID -g $GROUPID -C $PHP_FCGI_CHILDREN"
#else
#  EX="$SPAWNFCGI -n -p $FCGIPORT -f $FCGIPROGRAM -C $PHP_FCGI_CHILDREN"
#fi

### This if-then-else is for opening a unix socket
if test x$UID = x0; then
  EX="$SPAWNFCGI -n -s $FCGISOCKET -f $FCGIPROGRAM -u $USERID -g $GROUPID -C $PHP_FCGI_CHILDREN"
else
  EX="$SPAWNFCGI -n -s $FCGISOCKET -f $FCGIPROGRAM -C $PHP_FCGI_CHILDREN"
fi

# copy the allowed environment variables
E=

for i in $ALLOWED_ENV; do
  E="$E $i=${!i}"
done

# clean the environment and set up a new one
exec env - $E $EX</pre>
<p>In the above script I had to use /bin/bash instead of Ubuntu&#8217;s default /bin/sh as it uses some of bash&#8217;s features. Also note that with spawn-fcgi you can have a network tcp socket or a unix socket, but not both. On my test server I just simply ran the above script as root; it won&#8217;t restart itself if the VPS is restarted or if the script crashes. I have daemontools on my real server, and I&#8217;ll use that to start and monitor the launcher script. The link to lighttpd&#8217;s site has other startup scripts worht looking at.</p>
<p>You can&#8217;t use mod_fcgid to connect to the externally spawned FastCGI process. It can only launch and manage the processes itself. So I loaded mod_fastcgi and used the FastCgiExternalServer directive:</p>
<pre>&lt;IfModule mod_fastcgi.c&gt;
  FastCgiExternalServer /srv/www/site/fcgi -socket /tmp/php.sock
&lt;/IfModule&gt;</pre>
<p>That tells Apache that any request under the /srv/www/site/fcgi directory gets passed to the FastCGI process with a unix socket at /tmp/php.sock. Unfortunately there is not an simple configuration to have it just run php files, and the FastCGI server may not know what to do with static files like pictures or .css files.</p>
<p>There is a good <a href="http://whocares.de/fastcgiexternalserver-demystified/all/1/">article explaining the FastCgiExternalServer directive</a>. Its solution to having just the .php files be handled by the external server involve adding a handler, assigning an action to the handler pointing to a nonexistent script and then aliasing the nonexistent script back to a folder symlinked to the original directory. The only way I could find to simplify that was to use a ReWriteRule. In either case we need to unfortunately modify the configuration for each vhost to make it work.</p>
<p>I have several vhosts under /srv/www/. Following the articles example I created a symlink /srv/fcgi pointing to /srv/www . Then I modified my mod_fastcgi configuration as such:</p>
<pre>&lt;IfModule mod_fastcgi.c&gt;
  FastCgiExternalServer /srv/fcgi -socket /tmp/php.sock
  ReWriteEngine On
  ReWriteCond %{DOCUMENT_ROOT} ^/srv/www/(.*)
  ReWriteRule ^/(.*\.php(3|4)?(\?.*)?)$ /srv/fcgi/%1/$1
&lt;/IfModule&gt;</pre>
<p>Now the external FastCGI server is invoked whenever a file under /srv/fcgi is accessed, but /srv/fcgi is just a symlink to /srv/www. Instead of the above article&#8217;s gyrations I figured out the above rewrite rules that will rewrite any request for a .php file to /srv/fcgi/(rest-of-document-root)/(request_URL) . So in effect the rewrite points back to the original file, but through a symlink that makes Apache use the FastCGI server to process it. The ReWriteCond shown doesn&#8217;t actually make a decision; it is giving me a reference to use when constructing my rewritten path name.</p>
<p>Now I have to modify my vhosts. Rewrite rules don&#8217;t carry over to vhosts by default. For each VirtualHost section I have to add the following which allows the server rewrite rules inherit to the vhost:</p>
<pre>ReWriteEngine On
ReWriteOptions Inherit</pre>
<p>Alternately I could just put the rewrite rules in each VirtualHost section. In fact I may need to if I have other rewrite rules for pretty URLs.</p>
<p>With FastCGI&#8211;whether externally spawned or managed by mod_fcgid or mod_fastcgi&#8211;you also need ExecCGI enabled in the Options directive.</p>
<p>I used Apache benchmark and verified that all the child proceses are being used concurrently. And now the APC cache is shared among all the child processes.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://itkia.com/external-fastcgi-with-apache/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multithreaded Apache In Small VPS</title>
		<link>http://itkia.com/multithreaded-apache-in-small-vps/</link>
		<comments>http://itkia.com/multithreaded-apache-in-small-vps/#comments</comments>
		<pubDate>Sun, 09 Aug 2009 22:48:39 +0000</pubDate>
		<dc:creator>IT Know-It-All</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[fastcgi]]></category>
		<category><![CDATA[lighttpd]]></category>
		<category><![CDATA[mod_fcgid]]></category>
		<category><![CDATA[mpm_worker]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[ulimit]]></category>
		<category><![CDATA[vps]]></category>

		<guid isPermaLink="false">http://itkia.com/?p=57</guid>
		<description><![CDATA[My detailed report configuring multithreaded Apache mod_fcgid for PHP with limited resources.]]></description>
			<content:encoded><![CDATA[<div>
<p>My best-performing small VPS setup was with lighttpd and FastCGI PHP, but I got tired of trying to make rewrites work in lighttpd and switched to a two-process prefork Apache with mod_php and Squid as a web accelerator. That worked pretty well, but not as fast as lighttpd and FastCGI. What I really want is a multithreaded Apache and a FastCGI PHP that will fit in my small, cheap VPS.</p>
<p>I had tried Apache&#8217;s worker MPM and FastCGI before, but at the time both Apache and the PHP FastCGI process bloated and took up all my RAM despite my settings. Recently I decided to try again and was able to find out how to make it work.</p>
<p>Under Linux, by default each thread is assigned 8MB of stack memory, so an Apache process with 25 threads would try to take up 25*8=200MB of RAM!!! Plus the size of the Apache parent process, plus anything else that runs on my VPS. Not going to work in my small VPS. However, each thread doesn&#8217;t really need that much RAM. In fact, 128k is working fine for me so far. Apache 2.2 has a new directive ThreadStackSize for the worker MPM, and I set mine to &#8220;ThreadStackSize 131072&#8243;, and now I can have two Apache processes with 25 threads each taking up about 25MB worth of privvmpages. Another way to accomplish this is add &#8220;ulimit -s 128&#8243; to the Apache startup scripts. For Apache 2.0 you have to do it this way. Since I am using Apache 2.2 I didn&#8217;t have to use ulimit, but when I was testing the effects of changing the stack size I used this script which worked as a temporary measure:</p>
<pre>#!/bin/sh

ulimit -s 128
/usr/sbin/invoke-rc.d apache2 restart</pre>
<p>My 25 MB RAM usage above is without mod_php, though. Google searches lead to conflicting information about whether PHP is thread safe, so I want to use FastCGI. My problem with Apache FastCGI before was that it spawned several times as many PHP processes as I thought I had told it to. I was using mod_fcgid and pointing it to the same FastCGI PHP wrapper script that I had used for lighttpd. But that script set PHP to launch child processes, and I have since learned that mod_fcgid does not multiplex and therefore will not use the child processes. Instead it launches as many processes as it sees fit, and my configuration had each of those launching 4 children. No wonder my RAM got chewed up so quickly. So now I am letting mod_fcgid call /usr/bin/php-cgi directly:</p>
<pre>&lt;IfModule mod_fcgid.c&gt;
        AddHandler fcgid-script .fcgi .php
        # Where to look for the php.ini file?
        DefaultInitEnv PHPRC        "/etc/php5/cgi"
        # Maximum requests a process should handle before it is terminated
        MaxRequestsPerProcess       1000
        # Maximum number of PHP processes
        MaxProcessCount             4
        # Number of seconds of idle time before a php-cgi process is terminated
        IPCCommTimeout              120
        IdleTimeout                 120
        #Or use this if you use the file above
        FCGIWrapper /usr/bin/php-cgi .php
&lt;/IfModule&gt;</pre>
<p>Unfortunately, since each PHP processes is launched separately, any caching such as eAccelerator or APC will not be shared across each process. And each process uses up another X MB of RAM for the cache. So if I&#8217;m using APC with the default 30 MB cache and have 4 FastCGI PHP processes going, my APC caches are taking up 120 MB all by themselves! At the moment this is exactly what I&#8217;m doing, because I&#8217;ve moved up from a 256 MB VPS to a 390 MB VPS, and my total memory usage seems to be hovering near 256 MB when all processes are running. However, when PHP processes aren&#8217;t needed, mod_fcgid will kill them off to save memory, so most of the time I&#8217;m using much less RAM. I will see if I can set up the FastCGI processes like I did with lighttpd and then connect to it from Apache. I think it&#8217;s doable, but I haven&#8217;t tried yet.</p>
<p>I like using the worker MPM and FastCGI better than using the prefork MPM, mod_php and Squid. First of all the log files are a lot easier to parse. Apache (as I have it configured) can handle 50 concurrent static requests including 4 concurrent PHP requests, and I was able to enable KeepAlives again so my sites feel more responsive. With prefork Apache and Squid I could see a row of GIF smileys load up left-to-right when posting a reply in one of my forums. Now it happens so fast I can&#8217;t see it anymore. If I can get the PHP cache&#8211;previously eAccelerator, but I just switched to APC&#8211;to share itself across all my PHP processes then the overal RAM usage will be much lower, and I&#8217;ll be able to have more PHP processes.</p>
<p>And I like the new setup better than lighttpd because I can use the Apache rewrite rules provided by software programs like Drupal and SMF rather than try to translate them into lighttpd rewrites.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://itkia.com/multithreaded-apache-in-small-vps/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
