Got your dirt cheap VPS with only 64MB guaranteed memory on OpenVZ? Want to move your WordPress blog from some insanely oversold shared hosting to your shining low end box? If you follow the “traditional way” of setting up a Linux web server — installing Apache, MySQL and Mod_PHP — you will soon find that your site dying a horrible death, with pre-forked Apache processes killing themselves left and right. Or your entire Xen box descending into swapping hell before completely locking itself up.
So is it possible to host a low traffic PHP site on a low end VPS? Yes, but the first thing you need to do is getting rid of the A in LAMP.
While infinitely configurable, pre-forked Apache (which is required for mod_php) is not really scalable to suit low memory requirement. I won’t get into all the details, but there are several free/open source alternatives.
At LowEndBox, I am running Lighttpd + PHP in FastCGI mode. I am running 2x slave FastCGI process and it works quite well for 3x very low usage sites on this box, using less than 15MB of memory all together.
# ps -u www-data u USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND www-data 27509 0.0 3.1 6672 2092 ? S 11:50 0:00 /usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf www-data 27510 0.0 7.3 16656 4848 ? S 11:50 0:00 /usr/bin/php5-cgi www-data 27512 1.1 11.9 16652 7868 ? S 11:50 0:04 /usr/bin/php5-cgi
Installing them is easy –
# apt-get install lighttpd php5-cgi
Then just install other PHP modules (MySQL, GD, etc). Here is a comprehensive guide on running PHP on Lighttpd, but on Debian/Ubuntu, you usually just need do:
# ln -s /etc/lighttpd/conf-available/10-fastcgi.conf /etc/lighttpd/conf-enabled
Make sure you edit the 10-fastcgi.conf and (1) leave max-procs at 2 (2) reduce PHP_FCGI_CHILDREN to 1. After starting the daemon, you’ll see 2x php5-cgi process running. With PHP 5.2.x it would not spawn a manager process. However, if you intend to use PHP op-code cache/accelerator, then it would be a good idea to change max-procs to 1 and PHP_FCGI_CHILDREN to 2 so that your 2 worker processes can share the cache (but you’ll see a 3rd php5-cgi process running as manager/dispatcher).
While it’s lighter and faster, Lighttpd does not offer as much functionality as Apache, nor as configurable. For applications that depend on Apache’s mod_rewrite capability, there is usually hacks and work-around for Lighttpd. For WordPress to get clean URL, just add
server.error-handler-404 = "/index.php"
to your configuration file for your WP sites.
Another alternative web server that I’ve used for my other projects is Nginx. It uses slightly more memory (at least 7MB on 32bit Ubuntu) and does not support spawning CGI processes (so all FastCGI procs have to be external). A great piece software if you have very hight traffic site.
Comments 2
Nginx, I think, is much better and on my VPS consumes less memory than lighttpd. Also, with lighttpd there will be memory leaks.
Another good idea is to custom compile PHP and get rid as much extra stuff as possible. Also, don’t forget to install APC.
Some more tips at my blog (which btw is Drupal hosted on a 64MB RAM Xen VPS
Posted 14 Sep 2008 at 12:47 pm ¶http://dojimun.com/node/5671
@Duke — yes I agree that Nginx in many ways are superior than Lighttpd. It has a much consistent memory usage, whereas I found lighttpd can leak like crazy in highly loaded proxy/fastcgi set up. However in my particular case (Debian 4, Lighttpd 1.4.13, PHP via FastCGI), the memory foot print has been fairly stable at around 2.5MB RSS.
One advantage for Lighttpd is CGI support. So for some obscure scripts that only get run once in a blue moon, they don’t take up memory as fcgi processes when they aren’t used.
Posted 14 Sep 2008 at 1:16 pm ¶Post a Comment