5 minute read

How To Run WordPress on a DigitalOcean 512MB VPS

blitz.io testing of ryanfrankel.com

Running WordPress on a Digital Ocean 512MB VPS is certainly possible.  This site is running on one currently and I have seen as many as 200 concurrent users in real-time and I have used blitz.io to successfully test up to ~800 concurrent requests before running out of CPU (not RAM).

On average, I am using between 100 and 230MB of RAM at any given time.

Blitz.io Testing

This blows my mind.  Not only that this is possible but that the limiting factor becomes the CPU and not the RAM.  This makes sense with the setup that I will describe below but it goes to show how well a small VPS can perform if care is taken in its setup.

The Root Cause of Problems

If you have tried to get WordPress running on a 512MB VPS before you probably have realized that you almost immediately run out of RAM with just a few concurrent users.  This is because Apache uses a non-negligible amount of RAM for each connection made.

The Goals

To get WordPress running smoothly we need to reduce the number of processes that we let Apache run while also being able to serve pages.  Ideally, we wouldn't touch Apache at all for front-end visitors to the site and only users on /wp-admin or posting comments would be making requests that hit Apache.  In short, our goals are to reduce the RAM usage as much as possible while still serving content to a large number of users.Blitz.io Testing

An Overview of The Structure

There is nothing crazy about the setup I am using.  It is a basic WordPress setup with Varnish running in front of it.

Web ===> Varnish ===> WordPress(PHP) ===> MySQL

The Steps

These are the basic steps that will be necessary to get our server up and running:

  1. Create a DigitalOcean 512MB VPS and Install WordPress (and its dependencies)
  2. Set up Varnish caching
  3. Adjust Apache and MySQL to reduce the number of allowed processes

1. Create a DigitalOcean 512MB VPS and Install WordPress (and its dependencies)

If you are reading this I have to assume that you can get to DigitalOcean and create a droplet.  You will want to create one that is 512MB in size ($5) and select to run some sort of Ubuntu flavor.  I usually select 12.04 for the LTS but it shouldn't matter too much.

If you haven't already installed WordPress and you need help doing it I have created another Post to cover this.  Please see my Installing WordPress on a Digital Ocean VPS post.

2. Set Up Varnish Caching

I have covered this step before in a previous Post too.  Please visit my Using Varnish with WordPress Post to enable Varnish caching.

3. Adjust Apache and MySQL to Reduce the Number of Allowed Processes

Now we get to the part of this Post that is probably the most interesting to you and why you are here.

The most important item to remember is that our goal is that every, or nearly every visitor gets a cached page via Varnish.  Since we have Varnish set up we don't need a lot of Apache processes running.  The only time Apache (or MySQL) needs to do anything is when we are warming the cache or we are working in the Admin area.

Reduce Apache Processes

The configuration settings for Apache can be found in /etc/apache/apache.conf. So, navigate yourself to that file and open it in your favorite text editor.

Currently I am using the settings below on this site and I have had no issues.  There are three 'sections' that you will be changing in the apache.conf file.  Namely, they are mpm_preform_modulempm_worker_module, and the mpm_event_module.

<IfModule mpm_prefork_module>    
    StartServers          1
    MinSpareServers       1
    MaxSpareServers       3
    MaxClients            6
    MaxRequestsPerChild   0

<IfModule mpm_worker_module>
    StartServers          1
    MinSpareThreads       5
    MaxSpareThreads      15
    ThreadLimit          25
    ThreadsPerChild       5
    MaxClients           25
    MaxRequestsPerChild   0

<IfModule mpm_event_module>
    StartServers          1
    MinSpareThreads       5
    MaxSpareThreads      15
    ThreadLimit          25
    ThreadsPerChild       5
    MaxClients           25
    MaxRequestsPerChild   0

Great, that was easy.  Now just restart Apache to get the new settings to take effect.

sudo service apache2 restart

Reconfigure MySQL (optional, and possibly unnecessary)

If you have successfully set up Varnish and reduced the Apache processes I have found that you shouldn't have to tweak the MySQL settings too much.  Since MySQL will not be hit too often (only during warming) all we need to do is reduce the number of processes MySQL keeps running when nothing is happening.

Open /etc/mysql/my.cnf and search for the line containing the thread_cache_size and set it to 2.

thread_cache_size = 2

Now just close the my.cnf file and restart MySQL.

sudo service mysql restart


That's all there is to it.  You now have set up WordPress on a 512MB server that will run safely and handle a reasonable amount of traffic.  There are some things to watch out for though.  Basically, anything that is served uncached from your site.

  1. Multiple Users in the Admin area
  2. Logged in Users visiting the front-end if you have Varnish set to pass this type of user
  3. If your Varnish is caching pages with query strings into separate caches.

If you want to have some fun run over to Blitz.io and do some testing on how many concurrent users you can serve.

Leave a Reply

Your email address will not be published.

  1. Thanks for the tutorial. I did a couple of fine-tuning (installed Varnish, add swap file, etc.) using your guide above. However, I’m using up Mem: 490 467

    That’s huge considering that it’s a new website with no traffic.

    Not quite sure what is eating up the RAM. Anyway, this is my first experience with cloud hosting so quite a bit of a learning curve for me.

    Thanks! Any input is greatly appreciated!

    • Hey Yan,

      You can check out what processes are using the memory with the ‘top’ command. I personally use htop that can be installed with:
      sudo apt-get install htop

      From there you will be able to see if it is apache or another process that is eating things up. If you are still having trouble once you identify the source let me know and I can try to help.