Introduction

So you have a brand new VM from DigitalOcean. You were on a budget so you opted for the 512MB instance. Now I assume you have quickly found out that navigating to your website quickly eats up all of your system memory, since you have no swap (and should not) your site is crashing.

This seems to be a very common issue for those of us who have a site with very little traffic and don’t need a large web server. The issue is that the defaults for apache and mysql assume you have a system with much more memory by default. I will assume you have already been able to install the lamp stack, there should be many tutorials. I am using ubuntu 12.04 for this example, for CentOS the config locations will be different.

Step 1 – Reduce Apache Worker Servers

Open up the apache2 configuration file

UBUNTU% vi /etc/apache2/apache2.conf
CENTOS% vi /etc/httpd/httpd.conf

The main issue with these droplets is that by default there are many server processes started up waiting for requests. The default for apache has been to use the prefork module, so you only need to adjust settings for this. The actual settings depend on how much memory each server process requires to load your site. You can access this information by running ‘top’ and checking what your usage is. In my case each apache process was using roughly 60MB.

Since we are also running MySQL for the wordpress installation we need to reserve some RAM for MySQL.
Assuming you want to reserve only half the space for apache the max clients should be:
512MB / 60MB * 0.5 = 4 max clients

Low Volume Prefork Settings

<ifmodule mpm_prefork_module>
StartServers 1
MinSpareServers 1
MaxSpareServers 3
MaxClients 4
MaxRequestsPerChild 1000
</ifmodule>

Step 2 – Enable Query Cache for MySQL

If you are hosting wordpress you may find the content rarely changes. Enabling the query cache can in many cases improve your web sites performance.

UBUNTU% vi /etc/mysql/my.cnf
CENTOS% vi /etc/my.cnf

Find the section which has the query_cache and add the following line to enable the query cache.

#
# * Query Cache Configuration
#
query_cache_limit       = 1M
query_cache_size        = 16M
query_cache_type        = 1

Restarting the services

Now that we have configured the config files lets restart the services and check our memory consumption.

service apache2 reload; # In centos this is called httpd
service mysql restart;  # service called mysqld on some systems.

Enjoy!