Load Balancer + Wordpress

Posted June 20, 2017 22.6k views
NginxWordPressMariaDBLoad BalancingUbuntu 16.04

I have a Droplet where i have a wordpress + MariaDB + Nginx instalation, i would like to implement load balancing . Any one can give a hint?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
6 answers


I’d be happy to help :-).

When it comes to load balancing, there’s more that comes in to play than just the load balancer. You need to think more about how you want to go about separating things out.

Ideally, in a cluster, you’d separate NGINX + PHP, Database, and Storage in to their own clusters. The reason for this is because you won’t be able to deploy a load balancer, add your existing Droplet + a new one and things just work – there’s some degree of manual work required.

Ideally, you’d setup a load balancer and X number of web servers which will have NGINX and PHP installed (PHP-FPM). We’ll start with 2 in this example, which would look something like:

- Load Balancer
- - Web Server 01 (NGINX + PHP-FPM)
- - Web Server 02 (NGINX + PHP-FPM)

The next cluster we’d need to configure would be storage as we need somewhere to store data so we aren’t having to replicate data across the web servers. For this, most will recommend GlusterFS.

GlusterFS is setup with a client and then Y number of storage servers. It all depends on what you’ll need for your installation. The client is the one you connect to and mount on the Web Servers – it then serves data from the storage servers in the cluster.

So our modified cluster might look something like:

- Load Balancer
- - Web Server 01 (NGINX + PHP-FPM)
- - Web Server 02 (NGINX + PHP-FPM)

- - - GlusterFS Client
- - - - GlusterFS Storage Node
- - - - GlusterFS Storage Node

You would, for example, mount the GlusterFS client on /home on the web servers and that’s how NGINX would pull data to serve requests. It works just like /home does now, except it’s being served from the GlusterFS cluster instead of the physical web server.

The last thing you’d want to setup is a database server. You could set MySQL/MariaDB up on one of the web servers, though it’s generally recommended that you setup a database only server and then connect over the private network from your application.

As you can see, it can get a little complex and this is certainly just one example, though it’s more so tailored to WordPress and database driven applications.

You could certainly run fewer GlusterFS servers, though generally you want to at least run one client and one storage node.

That said, whether or not I’d go this route depends on your traffic. If you’re not at least half way to the point of needing a load balanced solution, I’d start small and work your way up.

There’s a lot that can happen and you would need to be able to troubleshoot – or ask questions here in the community / submit a support ticket for guidance.

If you’re not sure how to go about setting everything up properly, I would definitely recommend hiring a sysadmin to do it for you as a load balanced solution won’t perform any better if it’s not configured properly. It could potentially perform worse or not at all.

Let me know if you have any other questions, always happy to help :-).

  • Is there a way for us to just use Digital Ocean spaces in this sort of setup for the uploads folder and rsync the rest of the wp-content folder between the web servers?

i have a very small company (just me hehe) i cant hire no one… but little by little i´ll make my way hehe

  • @angldavd

    I’d start small and work your way up :-). I’ve been there myself – I’ve actually ran a few one-man companies in the past, so I know starting out can be a struggle for sure. The last company I was bootstrapping started with $100 and a lot of hopes and dreams. I ran it for about 2 years and it was sold off to a larger company who took the reigns and was able to do more than I could on my own.

    As long as you’re motivated, you can learn everything I detailed in my first reply. I didn’t start out knowing how to do it – far from it. I struggled, tested, failed, and repeated it multiple times. Even after being a sysadmin for the better part of the last 10 years, there are still things I run in to that I have to check and test, or ask others to jump in on. So don’t get down, stay motivated :-).

    First step I’d recommend – separate the web server and database server. That’s probably the easiest thing to do with your current setup.

    You’d deploy a second droplet, install MySQL/MariaDB, port over your database(s) and then work out connecting to it instead of the installation on your current server.

    Once you’ve successfully connected, you can remove MySQL/MariaDB from the main server and keep the connection alive on the secondary server. If you can get to this step, the rest will start to fall in to place and become far easier – in the process, you’re learning.

    Load Balancing can be added anytime really, so don’t rush in to it when you may not need it. You will end up raising your costs when you don’t need to.

    To give you an idea of what I mean, I recently worked with a client that was spending $1,400 a month with a pretty large provider. They were doing about 5-8 million visitors a month, so traffic was pretty heavy.

    I took their site and migrated it to DigitalOcean and set them up on a single server with Apache and PHP (I would have went NGINX, though they relied on Apache), offloaded the database to a single server, and implemented a caching server as well. No load balancing in this setup, though there was a lot of fine-grain tweaking and tuning involved.

    Now they are serving the same amount of traffic on a setup that runs about $400 per month. It’s all about finding out what you really need and using it. Don’t overspend on what you don’t need right now.

    • thats is what i always do, i try to optimize with server side cache (im far from be an expert)… i normally use $5 Droplets with almost 0 problems BUT i want to go beyond with one of my sites for 2 reasons, first i want to learn and second this site is critical for me and i need absolute zero downtime.

      the aproach that you describe is what im thinking to do. would be really nice to keep in touch, a mentor would be a extremely good for improve the skills.

      • @angldavd

        A single load balancer and mini-cluster won’t be enough to prevent downtime. With a single load balancer, you’re single point of failure is the load balancer. If it fails, the rest of your stack will fail to respond to the incoming requests.

        You’d be looking in to a rather large, multiple data center cluster farm which is where things get quite a bit more complex.

        Even with a complex cluster, downtime can still be an issue and DigitalOcean does not currently provide the DNS functionality needed to handle higher level config via their current iteration/release.

        I know AWS does as I’ve tinkered with it in the past, and it’d be entirely possible to use AWS for DNS and DigitalOcean for the clusters, though it’s not as simple as spinning up a Droplet and installing a LEMP stack. There’s a lot of hands-on configuration in this type of setup which you most likely won’t find in guides (they’d be lengthy!).

        It can be a lot of trial and error :-). Hitting the docs for each piece of software becomes the norm in most cases, which can be helpful so you become more familiar with all the options you have.

        When you get in to complex clusters, you’d be load balancing the web server clusters as well as your database server clusters. NGINX has the capabilities to do TCP load balancing via their stream module, so load balancing database servers can be done pretty easily, though you need a way to keep them in sync as well, which is probably the more difficult part (overall).

        At this size and scale, offloading storage is the only way to go, so GlusterFS becomes very important (or software like it).

        There’s a lot of bits and pieces and this is by no means an all-inclusive setup, it all depends on how far you want to go with it, but if you need 100% uptime 100% of the time, you’re going to need a lot so that you can account for failures 100% of the time.

        • you are right, may be is too soon to make the thing more complicates, however i want to implement a little cluster to learn how to do it.

@jtittle , Could you contact me with ? or leave me your contact information?

I got a project and expect to be your client. Thanks a lot.

I have a question about the sticky session on load balancer. I have turned it on in my load balancer but when i logged in my wordpress site via loadbalancer ip, and then refresh the page, it will kick me out of admin dashboard (cause the request switch to other droplet), it does not keep my session like sticky session said. Is there any solution for this problem?

  • Did you ever find a solution to this problem? I’m dealing with exactly the same situation myself.

  • @craigmartin which load balancing software are you using – or is it the home-baked DO version? We use HAProxy 1.8.2 to load balance and have it set to round robin but have never had a problem staying logged in. I’ll be honest, I haven’t used the DO load balancer as I’m not sure it will perform the same as my LBs.

    I have three setup in a cluster with multiple IPs using KeepAliveD to maintain the IPs assigned to each LB. Three Apache Hosts to host WordPress, three Storage (synced with GlusterFS) machines that host the file systems in /var/www, and five DB servers (three active, one backup, one ghost) running MariaDB with Galera for clustering. All in all, 12 servers, but I’ve had 100% uptime and no complains from my clients for 2.5 years, and it’s hyper fast. The best part is that I can take one server out of the cluster at any time to upgrade offline if needed and then just bring it back up without losing service.

    • Thanks for reply. Issue come because i wrong in setting wordpress.
      Can I ask you a bit about GlusterFS?
      I had try to use GlusterFS but when one node disconnect
      I have a horrible ~42 second delay in using or accessing files on /var/www/html so client can’t access my website.
      How did you handle this?

How many nodes do you have with glusterfs? Are you running your cluster properly and is it config’d properly? I can shut one node down without any effect on my sites. I can shut two down and get a tiny bit of lag under heavy load but no delay at all still. What version of gluster are you running?