How to best load balance a WordPress/WooCommerce e-commerce site?

February 21, 2018 2.8k views
WordPress Load Balancing Security Ubuntu 16.04

I have been running my WordPress/WooCommerce e-commerce and membership site happily for a couple of years now all on one server. But my traffic and membership is such that I want to transition to a more fault tolerant setup. I was thinking of the following:

  • 1 DB server
  • 2 WordPress servers (setup using easyengine with NGINX, PHP7)
  • 1 Load Balancer

The above setup should be all I need for now, but I have a few questions:

  1. SSL passthrough to each of the WP servers or not? What happens when someone makes a purchase if SSL is terminated on the Load Balancer? Does this all just work seamlessly with authorize.net (or other gateways) or are there some issues there?

  2. Are there specific nginx config or session gotchas in this scenario?

  3. What about server naming? What are the server names in each of the WP servers if they are sharing the same DB? how do I make them different? DO i need to make them different?

  4. Anything else I should be aware of?

Thanks!

3 Answers

Well, in case anyone was interested, here is what I ended up doing:

After reading up on GlusterFS, I had a ton of reservations about performance issues for my needs. In short, it is apparently horrible for the multiple small files scenario that my wordpress sites represent, and much better suited to medium to large media files. So instead, I set up lsyncd from wp1 to wp2, and although it is a few seconds delay to copy/sync, it is more than adequate for my needs.

For setting up the load balancer:

  1. I first pointed my domain to wp1 IP
  2. I then generated a letsencrypt (cert only) so that I would have the cert info I needed.
  3. Then I added this info manually from the files certbot generated to the load balancer
  4. Then I pointed my domain to the load balancer, force all traffic to SSL, and connect to port 80 of my droplets behind the load balancer

For naming my wordpress servers:

  1. I called one of them wp1.mydomain.com and the other wp2.mydomain.com
  2. I manually added entries in the otherwise identical wp-config.php file on each server with the following: define('WP_HOME','https://wp2.mydomain.com'); define('WP_SITEURL','https://wp2.mydomain.com');
  3. I exclude wp-config.php from my lsyncd setup btw, and sync everything else in /var/www/html

For the DB, both wp1 and wp2 point to the same (third) server (db1.mydomain.com) and serve the identical info.

The only issue may be having to clear the cache manually from time to time on each server individually, but I am looking into creating a button/plugin in the wp admin that will clear both servers' nginx cache at the same time/on the same click. Other than that, I am super happy with the setup! Anyone have any thoughts or critiques of this approach?

  • I need to add a very important change that I needed here. If I wanted to use permalinks, this completely BROKE the load balancer with the above setup. I had to do the following:

    1. Change wp-config on both sites to use: define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST']); define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST']);
    2. Change nginx config to include both the address of my load balancer as well as my wp1 and wp2 addresses.

This is a great place to start for a scalable WP installation.
https://www.digitalocean.com/community/tutorials/automating-the-deployment-of-a-scalable-wordpress-site

A few caveats I have run into / answers to a couple questions

A note DO does setup PTR records for droplets with names that resolve to their IP address - for example u.x.t1.lb-1.domain.com - add a sub-domain for u.x.t1.lb-1 that points to the domain.com droplet. Something to take into consideration.

Each of your WordPress/Application(s) can access the same DB Server. However in higher traffic situations your DB will become taxed. You could add a Redis Cache Server to your setup to help with this or use use something like the WordPress Plugin HyperDB to spread the database across multiple nodes, or setup a DB cluster - a few choices or combination of choices here.
A start - https://docs.jelastic.com/wordpress-cluster

Moving ahead to a more fault tolerant architecture you might want to look at this for your load balancer
https://www.digitalocean.com/community/tutorials/how-to-create-a-high-availability-setup-with-heartbeat-and-floating-ips-on-ubuntu-14-04

Another caveat I ran into is keeping each application/wordpress installation in sync with each with assets, plugin/theme versions etc...

The asset(s) aspect could be solved by moving this into DO Spaces - There are many WP Plugins available to help link the space and your media folder up.

As for the application server(s) being in sync, you could move your WP installation into a docker container and store the files on a shared network volume.

Hope this helps

by Ryan Quinn
Create and deploy a scalable WordPress instance consisting of a MySQL database server, a GlusterFS distributed filesystem, Nginx web servers and an Nginx load balancer. By using user-data and droplet meta-data we will automate the deployment of our site.
  • Thanks for the great advice. Still not sure I am understanding the naming of the servers underneath the load balancer though...how do I make sure that even though my users are accessing example.com (which gets load balanced between the two web servers) I have a way to to access the wordpress interface of wp1.example.com and wp2.example.com separately? How does the load balancer present each wp admin installation if they are named differently? And if they are sharing the db, don't they need to have the same site url in the options table?

    Or should I just forget about all of this and just go with a Gluster FS as indicated in one of your links? I assume that way each web server is just serving a shared identical filesystem...

    • I personally would go with the GlusterFS / Shared filesystem with docker swarm - This way you are running the same files.

      • thanks but last question (or I didn't understand your answer completely): What is the home url for each wordpress host? the address of the load balancer?

These are very useful tips! Thanks!
I've just finished setting eCommerce website by using Woocommerce theme.
This resource https://studioblackbelt.com was very useful too. It provides the latest trends and all the info needed to implement them

Have another answer? Share your knowledge.