<?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/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>Stephen Foskett, Pack Rat &#187; Pingdom Archives  &#8211; Stephen Foskett, Pack Rat</title>
	<atom:link href="http://blog.fosketts.net/tag/pingdom/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.fosketts.net</link>
	<description>Understanding the accumulation of data</description>
	<lastBuildDate>Fri, 10 Feb 2012 17:40:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com" />
	<atom:link rel="hub" href="http://superfeedr.com/hubbub" />
			<item>
		<title>Tuning Lighttpd For Linux</title>
		<link>http://blog.fosketts.net/2009/06/29/tuning-lighttpd-linux/</link>
		<comments>http://blog.fosketts.net/2009/06/29/tuning-lighttpd-linux/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 15:17:41 +0000</pubDate>
		<dc:creator>Stephen</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[lighttpd]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[PAM]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[Pingdom]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[UNIX]]></category>

		<guid isPermaLink="false">http://blog.fosketts.net/?p=2126</guid>
		<description><![CDATA[As I mentioned on Friday, I&#8217;ve recently built a multi-server web hosting environment around lighttpd, MySQL, and Ubuntu Linux. Ironically, my lighttpd web server slowed to a crawl that very evening! It turns out that I had not properly tuned lighttpd to function in a Linux environment. I was surprised to find that the Ubuntu [...]]]></description>
			<content:encoded><![CDATA[<p>As I mentioned on Friday, I&#8217;ve recently built a <a href="http://blog.fosketts.net/2009/06/26/multiserver-web-host-environment/"  target="_blank">multi-server web hosting environment around lighttpd, MySQL, and Ubuntu Linux</a>. Ironically, my lighttpd web server slowed to a crawl that very evening! It turns out that I had not properly tuned lighttpd to function in a Linux environment. I was surprised to find that the Ubuntu package did not include basic Linux settings! I referred to the <a href="http://redmine.lighttpd.net/wiki/lighttpd/Docs:Performance"  target="_blank">lighttpd performance documentation</a> for help.<span id="more-2126"></span></p>
<h3>File Descriptors</h3>
<p>Anyone familiar with its internals will tell you that <strong>everything is a file to a UNIX operating system</strong>. It&#8217;s the philosophy behind the system: Network connections, storage systems, system parameters, and processes all have file interfaces, and each of these pseudo-files needs a unique file descriptor.</p>
<p>What does this mean for lighttpd? Well, every time a visitor accesses a page, lighttpd uses three file descriptors: An IP socket to the client, a fastCGI process socket, and a filehandle for the document accessed. Lighttpd stops accepting new connections when 90% of the available sockets are in use, restarting again when usage has fallen to 80%. With the default setting of 1024 file descriptors, <strong>lighttpd can handle a maximum of 307 connections</strong>. This is a lot. But it is possible to exceed this number under times of high load.</p>
<p>To prevent this from happening, we can double the limit without any trouble. <strong>Simply set &#8220;server.max-fds&#8221; to 2048 in /etc/lighttpd/lighttpd.conf</strong>.</p>
<p>Contrary to much of the advice I found on the Internet, lighttpd spawned by root does not appear to use the &#8220;nofile&#8221; limits set in /etc/security/limits.conf, since these are for PAM and only apply to full interactive logins. There is a system-wide limit that can be set in /etc/sysctl.conf, however. Check your default with &#8220;<strong>cat /proc/sys/fs/file-max</strong>&#8221; and make sure it&#8217;s over 10,000. Mine was set to 12640 so I left that alone.</p>
<h3>HTTP Keep-Alive</h3>
<p>One reason that file descriptors get used up so quickly is HTTP keep-alive. To improve performance, modern web servers keep client connections alive to handle multiple requests instead of building up and tearing down connections for each item in a page. <strong>Keep-alive is tremendously beneficial to performance, but tends to keep unnecessary connections alive, too</strong>. By default, lighttpd allows 16 keep-alive requests per connection, allows idle sessions to remain alive for 5 seconds, and gives reads and writes 1 minute and 6 minutes to complete, respectively.</p>
<p>Although <strong>lighttpd has pretty aggressive defaults</strong> (especially compared to Apache), a period of heavy traffic and a few slow clients could see many unused connections sticking around. The <strong>server.max-keep-alive-idle</strong> setting default of 5 seconds can be reduced to as low as 2, if you assume your clients are reasonably quick about requesting data, but a value of 3 or 4 is probably realistic. You may want to <em>increase</em> the <strong>server.max-keep-alive-requests</strong> value from the default of 16, but you probably don&#8217;t need to. The <strong>server.max-read-idle</strong> and <strong>server.max-write-idle</strong> settings are tempting targets, but these situations are usually fairly rare so let&#8217;s not monkey with them.</p>
<h3>Mechanics: Polling and Sending</h3>
<p>The best bang for your lighttpd buck is to tune the server to use better kernel resources to check for file changes and write data to the network. There are three critical items here, each of which is set to a conservative universal setting by default.</p>
<p>One of the major areas of UNIX development over the last decade was how to handle the tens of thousands of connections experienced by Internet servers. This &#8220;<a href="http://www.kegel.com/c10k.html"  target="_blank">C10K Problem</a>&#8221; is documented in excruciating detail if you&#8217;re interested, but the net of it is that <strong>each version of UNIX has an advanced mechanism to handle I/O events</strong>. Since kernel version 2.6, Linux has sys_epoll, a so-called edge-triggered polling mechanism which scales linearly with the number of connections. But lighttpd runs on many different flavors of UNIX, so it has to default to the older and less-scalable &#8220;level-triggered&#8221; poll system. To remedy this, <strong>set &#8220;server.event-handler&#8221; to &#8220;linux-sysepoll&#8221;</strong>.</p>
<p>Another mechanism that varies widely across UNIX systems is how to actually <strong>read and write data from the disk to the network</strong>. All systems include basic read() and write() calls, which transfer data into and out of system memory. Lighttpd defaults to using these to move data around. But Linux includes a more advanced call, sendfile, which can move data around without copying it into memory. We can enable this by setting <strong>&#8220;server.network-backend&#8221; to &#8220;linux-sendfile&#8221;</strong>, which ought to <a href="http://www.lighttpd.net/2007/2/3/raw-io-performance"  target="_blank">improve performance</a> for larger (multi-megabyte) files without impacting smaller ones.</p>
<p>Lighttpd attempts to improve performance further by caching the output of the UNIX stat() command. It includes a basic (&#8220;simple&#8221;) cache which keeps the result of file system calls in memory for one second. But many Linux distributions include more advanced accelerators: FAM was the original, and a lighter-weight workalike called Gamin is now included by default in Ubuntu&#8217;s lighttpd install. Therefore, we can improve stat calls simply by allowing lighttpd to use Gamin: Set<strong> &#8220;server.stat-cache-engine&#8221; to &#8220;fam&#8221;</strong> and you&#8217;re rolling!</p>
<p>One more useful tweak to consider, although it&#8217;s not included in the official lighttpd performance document, is not updating the &#8220;atime&#8221; parameter on served pages. This is a bit of a religious issue among some UNIX administrators, but I feel safe in saying that since my web server logs all accesses and I&#8217;m not using any kind of hierarchical storage system to store them, <strong>I don&#8217;t care when each php, html, and png file was last accessed</strong>. We can stop writing atime values by mounting the entire filesystem with &#8220;noatime&#8221;, but I like the more granular approach offered by lighttpd: Simply <strong>set &#8220;server.use-noatime&#8221; to &#8220;enable&#8221;</strong> and it won&#8217;t bother keeping this updated for the files it accesses. Everything else will continue as it always has but with reduced disk I/O.</p>
<h3>Summary</h3>
<p>Lighttpd has pretty good default settings, but a few might be tweaked if we need to respond to higher server loads. The more important area of tuning is simply enabling the advanced features of the 2.6.x Linux kernel and Ubuntu system we are using: <strong>Enable sys_epoll, sendfile, and Gamin and disable atime updates</strong>.</p>
<p>I&#8217;ll post more information as I stumble across it. I&#8217;m still learning, but my server performance as improved dramatically: Pingdom tools <a href="http://tools.pingdom.com/?url=http://blog.fosketts.net&amp;treeview=0&amp;column=objectID&amp;order=1&amp;type=0&amp;save=true"  target="_blank">reports</a> that <strong>it used to take upwards of half a minute to load my blog&#8217;s home page and it now loads in under seven seconds</strong>! That&#8217;s progress!</p>
<pre><code># Maximum number of file descriptors, default = 1024
server.max-fds = 2048
# Maximum number of request within a keep-alive session before the server terminates the connection, default = 16
server.max-keep-alive-requests = 16
# Maximum number of seconds until an idling keep-alive connection is dropped, default = 5
server.max-keep-alive-idle = 4
# Maximum number of seconds until a waiting, non keep-alive read times out and closes the connection, default = 60
server.max-read-idle = 60
# Maximum number of seconds until a waiting write call times out and closes the connection, default = 360
server.max-write-idle = 360
# Which event handler to use, default = poll
server.event-handler = "linux-sysepoll"
# How to handle network writes, default = writev
server.network-backend = "linux-sendfile"
# Requires FAM or Gamin to be installed, default = simple
server.stat-cache-engine = "fam"
# Whether to update the atime setting on file access, default = disable
server.use-noatime = "enable"
</code></pre>
<div id="crp_related"><h3>You might also want to read these other posts...</h3><ul><li><a href="http://blog.fosketts.net/2010/07/30/high-performance-memory-apache-php-virtual-private-server/"  rel="bookmark" class="crp_title">A High-Performance, Low-Memory Apache/PHP Virtual Private Server</a></li><li><a href="http://blog.fosketts.net/2009/06/26/multiserver-web-host-environment/"  rel="bookmark" class="crp_title">Setting Up a Multi-Server Web Hosting Environment</a></li><li><a href="http://blog.fosketts.net/2010/08/01/force-apache-redirect-canonical-hostnames-serveralias-friend/"  rel="bookmark" class="crp_title">How To Force Apache To Redirect To Canonical Hostnames, or ServerAlias Is Not Your Friend</a></li><li><a href="http://blog.fosketts.net/2009/02/07/apologies-404s/"  rel="bookmark" class="crp_title">Apologies For The 404s!</a></li><li><a href="http://blog.fosketts.net/2010/03/25/robocopy-multi-threaded/"  rel="bookmark" class="crp_title">Robocopy: Better, Faster, Stronger</a></li></ul></div><script src="http://feeds.feedburner.com/~s/sfoskett?i=http://blog.fosketts.net/2009/06/29/tuning-lighttpd-linux/" type="text/javascript" charset="utf-8"></script><hr />
<p><small>© sfoskett for <a href="http://blog.fosketts.net">Stephen Foskett, Pack Rat</a>, 2009. |
<a href="http://blog.fosketts.net/2009/06/29/tuning-lighttpd-linux/">Tuning Lighttpd For Linux</a>
<br/>
This post was categorized as <a href="http://blog.fosketts.net/category/everything/personal/" title="View all posts in Personal" rel="category tag">Personal</a>. Each of my categories has its own feed if you'd like to filter out or focus on posts like this.<br/>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://blog.fosketts.net/2009/06/29/tuning-lighttpd-linux/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<series:name><![CDATA[Web Hosting]]></series:name>
	</item>
	</channel>
</rss>

