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.
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.