Lower memory usage on OpenVZ VPS

I recently migrated CompleteFusion to a new hosting provider, which meant switching from a Xen-based VPS to OpenVZ. Although OpenVZ VPS hosting is generally much less expensive, it often lacks swap space and requires special consideration of memory usage. Running OpenSSH, MySQL, and Apache2 with mod_php5 caused out of memory problems, even with 512MB RAM.

First, I replaced OpenSSH with Dropbear, which uses about 50% less memory while providing very similar SSH2 server capabilities. On Debian, installing dropbear was simple:

apt-get install dropbear
sed -i 's/NO_START=0/NO_START=1/g' /etc/default/dropbear
/etc/init.d/ssh stop && /etc/init.d/dropbear start

Although I could have made various configuration changes to tune Apache2 to use less memory, switching to a stock lighttpd configuration with fastcgi was enough to reduce memory usage to acceptable levels. I installed lighttpd on Debian as follows:

apt-get install lighttpd
apt-get install php5-cgi
echo "cgi.fix_pathinfo = 1" >> /etc/php5/cgi/php.ini
lighttpd-enable-mod fastcgil
/etc/init.d/lighttpd force-reload

On my OpenVZ VPS, I ran into a problem with starting lighttpd, where it reported that port 80 was in use, even though it wasn't. As it turns out, it was apparently because my container lacked IPv6 support. I resolved it by commenting out the following line in /etc/lighttpd/lighttpd.conf:

#include_shell "/usr/share/lighttpd/use-ipv6.pl"

These changes should lower your memory usage enough to run well inside of 512MB RAM, but additional configuration will probably be needed if you only have 256MB or less.

Leave a Reply

Your email address will not be published. Required fields are marked *