Using Varnish with WordPress
5 minute read
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.
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.
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.
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.
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
These are the basic steps that will be necessary to get our server up and running:
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.
I have covered this step before in a previous Post too. Please visit my Using Varnish with WordPress Post to enable Varnish caching.
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.
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_module, mpm_worker_module, and the mpm_event_module.
<IfModule mpm_prefork_module> StartServers 1 MinSpareServers 1 MaxSpareServers 3 MaxClients 6 MaxRequestsPerChild 0 </IfModule> <IfModule mpm_worker_module> StartServers 1 MinSpareThreads 5 MaxSpareThreads 15 ThreadLimit 25 ThreadsPerChild 5 MaxClients 25 MaxRequestsPerChild 0 </IfModule> <IfModule mpm_event_module> StartServers 1 MinSpareThreads 5 MaxSpareThreads 15 ThreadLimit 25 ThreadsPerChild 5 MaxClients 25 MaxRequestsPerChild 0 </IfModule>
Great, that was easy. Now just restart Apache to get the new settings to take effect.
sudo service apache2 restart
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.
If you want to have some fun run over to Blitz.io and do some testing on how many concurrent users you can serve.