<?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>randys.org &#187; gentoo</title>
	<atom:link href="http://www.randys.org/tag/gentoo/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.randys.org</link>
	<description>Wasting your precious bandwidth since 1999</description>
	<lastBuildDate>Thu, 03 Jun 2010 02:43:39 +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>How-To: Lighttpd, ExecWrap, PHP, WordPress &amp; Gallery2 On A Gentoo VPS</title>
		<link>http://www.randys.org/2006/08/27/lighttpd-execwrap-php-wordpress-gallery2-on-a-gentoo-vps/</link>
		<comments>http://www.randys.org/2006/08/27/lighttpd-execwrap-php-wordpress-gallery2-on-a-gentoo-vps/#comments</comments>
		<pubDate>Sun, 27 Aug 2006 06:20:00 +0000</pubDate>
		<dc:creator>randy</dc:creator>
				<category><![CDATA[Code Chunks]]></category>
		<category><![CDATA[General Nerdery]]></category>
		<category><![CDATA[Home]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[gentoo]]></category>
		<category><![CDATA[lighttpd]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[vps]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.randys.org//2006/08/27/lighttpd-execwrap-php-wordpress-gallery2-on-a-gentoo-vps</guid>
		<description><![CDATA[Part of my decision to change hosting providers was to expand my knowledge of technologies. I know how to write PHP, SQL and a whole host of other languages. What I was less familiar with was the servers that run them and other parts of a hosting system (mail, dns et al). Switching to a [...]]]></description>
			<content:encoded><![CDATA[<p>Part of my <a href="http://www.randys.org/2006/08/21/so-long-dreamhost-ill-miss-you/">decision to change hosting providers</a> was to expand my knowledge of technologies. I know how to write PHP, SQL and a whole host of other languages. What I was less familiar with was the servers that run them and other parts of a hosting system (mail, dns et al). Switching to a VPS setup allowed me to explore my options in what I would run on my system and fine tune the processes to run under limited resources.</p>

<p>I was already familiar with <a href="http://httpd.apache.org/">Apapche</a> and how to set that up with PHP. Apache2 makes it really easy to setup suExec with mod_suphp. Simply add &#8220;SuPHP_UserGroup $user $group&#8221; to a virtual host and viola, all PHP processes run as that user (as fastcgi). That was great and all, but on a system with limited resources, apache is dog. It sucks up way too much memory.  After setting up my VPS and running all the services, I was up to about 150MB of RAM used. That&#8217;s with apache2, php, mysqld, postfix, postgrey, courier-imapd (and ssl), courier-pop3d (and ssl) and mailman (which is another memory hog, but that&#8217;s another post) running. Granted 150MB isn&#8217;t that bad for a web server, especially if you have an entire system to yourself that has 1 or 2 GB of RAM. I&#8217;m on a <a href="http://www.vpslink.com/gentoo-vps/">VPS with a mere 256MB</a> of RAM.</p>

<h2>Enter Lighty</h2>

<p><a href="http://www.lighttpd.net">Lighttpd</a> is an open source, fast and efficient alternative to Apache. It pretty much does everything Apache does but with a much smaller footprint. Yes, it was a little more difficult to setup, but most of my troubles came from not knowing the Lighty configuration syntax. It&#8217;s not hard to master, just different than Apaches familiar tag based config files.</p>

<p>So far, this is what I have running on my VPS:</p>

<pre><code>lighttpd (1.4.11)
php* (5.1.4) (+fastcgi)
mysql (4.1.21)
postfix (2.2.10)
postgrey (1.24)
courier (4.0.4)
openssh (4.3_p2)
tinydns (1.05)
proftpd (1.2.10)
</code></pre>

<p>Current memory usage:</p>

<pre><code>Total: 239 MB   Used: 96 MB   Free: 143 MB
</code></pre>

<p><strong>UPDATE</strong>: I recently added a <a href="http://typosphere.org/">Typo</a> blog (<a href="http://www.rubyonrails.org">RoR</a> application) to one of the domains I&#8217;m hosting and my memory usage jumped a little&#8230; well, a lot really. I&#8217;m probably sitting at about 120-130 MB used at the moment.</p>

<h2>And now the HOWTO</h2>

<p>Configuring lighty wasn&#8217;t that hard. The hardest part was figuring out things like setting up rewrite rules for web applications like <a href="http://www.wordpress.org">WordPress</a> and <a href="http://gallery.menalto.com/">Gallery2</a> <a href="http://www.randys.org/content/2004/07/26/google-friendly-urls-with-php-and-apache/">search engine friendly URLs</a>. The other tricky part was getting ExecWrap (similar to Apache&#8217;s suExec wrapper) working properly. Well, it wasn&#8217;t that tricky, I just had some settings wrong so it <em>appeard</em> to be tricky. Let&#8217;s tackle the ExecWrap part first.</p>

<h4>ExecWrap Your PHP</h4>

<p>You&#8217;ll need to <a href="http://cyanite.org/execwrap/">grab, build and install the ExecWrap</a> wrapper first. It&#8217;s actually pretty straight forward. The important part is setting the correct permissions on the files involved in this setup (and using the correct UIDs and GIDs for the wrapper). For the sake of this post, I&#8217;ll skip that part. If I get enough questions about it, I&#8217;ll post a follow up on how to set this up properly.</p>

<p>So, here&#8217;s my setting for PHP/FastCGI setup on lighty:</p>

<pre><code>fastcgi.server = (
    ".php"  =&gt; ((
            # socket - this needs to be writable by the webserver itself
            "socket"            =&gt; "/var/run/fastcgi/fastphp.socket",
            # bin-path - the path to the execwrap script -- see NB below
            "bin-path"          =&gt; "/usr/lib/php5/bin/execwrap",
            # check-local - Not 100%, but I'm pretty sure this
            # disables cheking that the local file exists
            "check-local"       =&gt; "disable",
            # max-procs - Maximum number of procs to fire up.
            # I'm pretty stingey here, but my site doesn't see
            # a lot of traffic.
            "max-procs"         =&gt; 1,
            # bin-environment
            "bin-environment"   =&gt; (
                # Howman PHP_CFGI_CHILDREN to start up
                "PHP_FCGI_CHILDREN"     =&gt; "4",
                # Maximum request (per child? i dunno)
                "PHP_FCGI_MAX_REQUESTS" =&gt; "1000",
                # UID - User ID you want the script to execute as
                "UID"                   =&gt; "1000",
                # GID - Group ID you the script to execute as
                "GID"                   =&gt; "1000",
                # TARGET - the actual script to run
                "TARGET"                =&gt;  "/usr/lib/php5/bin/randy.php.sh",
                # CHECK_GID - this just checks the GID of the wrapper script
                "CHECK_GID"             =&gt; "1"
            ),
            # Copied from another site... not quite sure what it
            # does other than copying those env to $_ENV
            "bin-copy-environment"  =&gt; ("PATH", "SHELL", "USER"),
            # Fixes broken $_SERVER['PATH_INFO] I believe
            "broken-scriptfilename" =&gt; "enable"
        )
    )
)
</code></pre>

<p>The contents of <em>randy.php.sh</em>:</p>

<pre><code>#!/bin/sh
exec /usr/lib/php5/bin/php-cgi
</code></pre>

<p><strong>NB</strong>: Note that the execwrap script must be executable by lighty and must also have the SUID bit set. Also, the shell script needs to be owned by the user in which you wish to execute PHP as (in my case, my username). Also note that execwrap can live anywhere you specify <strong>when you compiled the script</strong>. In my case, I specified in the execwrap_config.h <em>/usr/lib/php5/bin</em> as the path where it will live. The shell script must also live under the same path.</p>

<h3>WordPress &amp; Gallery URLs</h3>

<p>Permalinks. The best thing since sliced bread. The applications work flawlessly with Apache (if you can use .htaccess in your setup) but take a little tweaking in lighttpd.</p>

<h4>WordPress</h4>

<p>I futzed around with this for several hours trying to get this to work properly. Trying to get my head around regular expressions and all the different possible links used in WordPress. And it all came back to to a really simple lighttpd setting (which oddly enough, doesn&#8217;t involve rewite at all).</p>

<pre><code>server.error-handler-404 = "/content/index.php?error=404"
</code></pre>

<p>That&#8217;s it. That and make sure your permalinks setting doesn&#8217;t contain the /index.php/.</p>

<p><strong>Update</strong>: The above solution to WordPress&#8217; permalinks might not be the best. The fact that it&#8217;s using the 404 handler might send a 404 response back to the browser. The other issue to worry about is whether or not this is sending a temporary redirect (301). If you have content indexed by a search engine, this will ruin your page ranking. <em>2008-06-06</em></p>

<h4>Gallery2</h4>

<p>Gallery was a bit more difficult. Well, not really. I ended up doing a little R&amp;D (i.e. Rob &amp; Duplicate) <a href="http://codex.gallery2.org/index.php/Gallery2:Lighttpd_URL_Rewrite">from the gallery2 codex</a>.</p>

<pre><code>url.rewrite = (
    "^/(.*)/Rewrite.txt$" =&gt; "/$1/Works.txt",
    "^/gallery/v/(\?.+|\ .)?$" =&gt; "/gallery/main.php?g2_view=core.ShowItem",
    "^/gallery/admin[/?]*(.*)$" =&gt; "/gallery/main.php?g2_view=core.SiteAdmin&amp;amp;$1",
    "^/gallery/d/([0-9]+)-([0-9]+)/([^\/]+)(\?|\ )?(.*)$" =&gt;
    "/gallery/main.php?g2_view=core.DownloadItem&amp;amp;g2_itemId=$1&amp;amp;g2_serialNumber=$2&amp;amp;$3",
    "^/gallery/v/([^?]+)/slideshow.html" =&gt;
    "/gallery/main.php?g2_view=slideshow.Slideshow&amp;amp;g2_path=$1",
    "^/gallery/v/([^?]+)(\?|\ )?(.*)$" =&gt;
    "/gallery/main.php?g2_view=core.ShowItem&amp;amp;g2_path=$1&amp;amp;$3",
    "^/gallery/c/add/([0-9]+).html" =&gt;
    "/gallery/main.php?g2_view=comment.AddComment&amp;amp;g2_itemId=$1",
    "^/gallery/c/view/([0-9]+).html" =&gt;
    "/gallery/main.php?g2_view=comment.ShowAllComments&amp;amp;g2_itemId=$1",
    "^/gallery/p/(.+)" =&gt;
    "/gallery/main.php?g2_controller=permalinks.Redirect&amp;amp;g2_filename=$1"
)
</code></pre>

<p>Make sure you change out the <code>^/gallery/</code> parts to where you have Gallery2 installed.</p>

<h2>Happy Little VPS</h2>

<p>All in all, the VPS is running really smoothly. I have everything I had (thechnology wise) at <a href="http://www.dreamhost.com/r.cgi?24650">Dreamhost</a> but with twice the performance. My site is defintely faster on the VPS than it was on Dreamhost.</p>

<p>Stay tuned for more HOWTOs on setting up these things on a <strike>Gentoo</strike> Arch Linux VPS. I plan on writing something for a Postfix/Courier virtual domain setup at some point. If you find this useful, pass it along. I&#8217;d be real interested in seeing how this server performs under a heavy load. Perhaps you <a href="http://digg.com/linux_unix/Lighttpd_ExecWrap_PHP_WordPress_Gallery2_On_A_Gentoo_VPS">Digg</a> it?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.randys.org/2006/08/27/lighttpd-execwrap-php-wordpress-gallery2-on-a-gentoo-vps/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
