Slashdot My Linode
From LinodeWiki
You likely purchased your Linode because you thought, "Hey this ish looks cool, it's cheap, I get to pick my flava linux, and I get to be r00t. Now I can host my blog, my forums, and show everyone my rawking pics." Everything was going along peachy, a few friends visited your site and it was fast and everyone's all happy. Then one day--the trial by fire day--your site becomes popular and everyone wants to visit, will your site survive?
Contents |
[edit] Choosing the right AMP in LAMP for your Linode
While everyone loves LAMP (Linux Apache MySQL PHP) there are somethings you need to keep on your mind when it comes to the AMP of your Linode.
[edit] A -- Apache, Is It Right For You?
Apache--the defacto standard for serving up web requests--is equipped to tackle any size website you can throw at it. Also you get the added comfort of being able to pick your favorite medium to find support for usage and configuration. But is it right for the sites hosted on your Linode? The short answer is, it can be. It is just a matter of learning how to tune it, for instance knowing the appropriate sizes for directives like the Min and Max clients/threads/servers, the basics are found at Apache's httpd site.
[edit] MPM
The way Apache handles multiple requests is via its Multi-Processing-Modules, there are different modules for platforms and your needs
- Prefork is what you need to use mod_php, it spawns a certain amount of apache processes (with php in memory) at startup, it expands the amount of processes to meet demand to a specified max, and kills off idle processes to a specified min. It's required for mod_php because of the isolation it provides a request as PHP is not thread safe. The downside of this approach is that the defaults set by your distribution are often higher than small sites need or want. To prevent too much memory usage you can lower StartServers/MinSpareServers/MaxSpareServers, I generally use 1/1/3 respectively but YMMV. These may not be high enough to keep up with the demand of your site.
- Worker can also be used, you can't use mod_php though you must use FastCGI module to have access for PHP on your sites. Worker keeps your number of apache processes lower, thus decreasing that overhead but you'll also have php processes for the fastcgi sockets.
[edit] Lighttpd
lighttpd is a popular alternative to Apache, it has an event driven design that generally comes with lower overhead. Some sane values for using PHP with lighttpd if you're concerned about memory usage (how many php processes lighttpd will spawn):
server.modules += ( "mod_fastcgi" )
## Start an FastCGI server for php (needs the php5-cgi package)
fastcgi.server = ( ".php" =>
((
"bin-path" => "/usr/bin/php-cgi",
"socket" => "/tmp/php.socket",
"max-procs" => 1,
"idle-timeout" => 20,
"bin-environment" => (
"PHP_FCGI_CHILDREN" => "4",
"PHP_FCGI_MAX_REQUESTS" => "10000"
),
"bin-copy-environment" => (
"PATH", "SHELL", "USER"
),
"broken-scriptfilename" => "enable"
))
)
[edit] Other HTTP Servers
However, you should also be aware that there are plenty of other httpd servers designed for small footprint (low memory usage) that are readily available, well suited to the task, and easily configurable. A small incomplete list would be:
[edit] M -- (My)SQL the IO Hog
A large percentage of the website packages you want to run on your Linode have a MySQL or other database backend. Yet, the nature of the beast is you'll use lots of IO making lots of queries, or if you try and push everything into memory you'll start thrashing swap (and therefore eat your IO) because you don't have that much memory. So, can you run a MySQL server on your Linode? Again, the short answer is yes, you can. And again, the subanswer is it's also about knowing how to configure MySQL to your low memory setup. Here is a sample configuration
One of the easiest and convenient ways to ease the burden from MySQL is to purchase a second linode in the same DC as your current node and move your MySQL server there. Turn on the private network on both nodes and communicate on the private network (over which bandwidth is not counted and extremely fast). Make sure you follow sane procedures to secure it, like listening only on the private ip address, and configure your firewall to only allow your other linode.
See also: Cached (Static) Content vs Dynamic
[edit] P -- PHP Tunage
To Be Added: For now google for tune php memory usage.
If you are using lighttpd or another FastCGI interface to PHP you may want to limit the number of php processes spawned, the same way you would limit the Min and Max clients/servers/threads for Apache
Turck MMcache is very useful; I use it extensively on my Linode. –Martinultima 14:46, 9 Jun 2006 (EDT)
If you want an updated and under-development solution, I would recommend eAccelerator, or APC
Another PHP accelerator to consider is XCache - developed by the same folks as the lighttpd web server.
[edit] Cached/Static vs. Dynamic
This applies to pretty much any web application you would want to serve up (for instance a blog or a CMS) on your IO limited Linode. One of the best ways to ensure your Linode will stay alive during high traffic is to utilize some sort of cache feature for the application. This means you are serving up static content versus dynamically generating it from a database backend. The former has a significantly less amount of IO calls versus the latter which may have difficult or multiple SQL queries and lots of IO overhead.
There are cache plugins (sometimes called static plugins) for Mambo, TikiWiki, and WordPress, and most likely many other web applications. MediaWiki has a setting called $wgUseFileCache that, when enabled, will cache static pages after they are generated and serve them to users that are not logged in.
If you are unable to find a cache/static plugin or setting for your particular application, there are a couple of methods you could try to stem the tide of IO calls. One method would be to use Squid in HTTPD Accelerator Mode or a properly configured Varnish Cache.
Another way would be to manually create a static version of your site when something changes. This would essentially mean going to each page and grabbing the html and serving it instead of the dynamic content. Utilities that can assist you in doing that include Webby, Jekyll, and webgen, all of which will take specially formatted files and output static files that you can serve. Those utilities are very good if you're making a simple blog and/or website that doesn't necessarily need to be dynamically generated each and every request.
[edit] Caching the monster
I started using the traditional Apache/MySQL focus (on Debian Etch), and as soon as I started getting traffic I found out it was the wrong way, suffering random OOMs. I tried both Cherokee and nginx at the moment, but I was just not happy. I use too many Apache features and I'm too lazy to port/find out how to do them in other web servers.
Therefore, I just wanted to cache the monster. I used nginx and memcached to solve a part of the problem (the web server) -- nginx now listens on TCP port 80, uses memcached to cache requests and passes requests to Apache, which listens in a different, local port. This way, Apache gets less hits since most things are cached. nginx is quite negligible in terms of resource usage, and my memcached still uses less resources than apache2. Using that stack my site was able to survive some unusual loads.
Some resources:
- Nginx and Memcached - a 400% boost!
- Using Nginx, SSI and Memcache to Make Your Web Applications Faster
- Nginx's Official Reference
Regarding MySQL, I used the minimal configuration cited in this document, just changing/adding some parameters that might be Debian-based, namely:
[mysql and mysqld] ... socket = /var/run/mysqld/mysqld.sock ... [mysqld] basedir = /usr datadir = /var/lib/mysql tmpdir = /tmp language = /usr/share/mysql/english ... !includedir /etc/mysql/conf.d/ <EOF>
I'm currently testing this MySQL configuration. It seems to be more CPU hungry but uses less memory.
