Jump toUpdate content
Setting up a load balanced Wordpress
The capacity of a single server is limited. Once a website gains more and more attraction the instance serving the site comes to a point where it can not handle any more users. The website starts to slow down or even become unavailable as the server goes down from the traffic.
This is the point where a Load Balancer enters the game. It allows to spread the “load” that all those visitors and their requests create to be “balanced” over a series of different Instances.
In case of increasing load on a setup, capacity can easily be increased by adding more Instances to the load balancers backend. This allows to scale your infrastructure without any downtime or delays whilst waiting for DNS zones to be updated.
During the tutorial the following IPs are used:
10.45.2.3for the first Wordpress instance
10.45.2.4for the second Wordpress instance
10.46.5.6for the MariaDB instance
188.8.131.52for the Load Balancer front-end IP Load Balancer supports private IPs of Scaleway Instances for backend servers, allowing you to deploy Instances without public IPv4.
Follow this tutorial to start an Ubuntu Instances and to install WordPress with LEMP on both of them.
Set up a third instance with a MariaDB database as explained in this tutorial.
Click Load Balancer in the menu on the left, to enter the Load Balancer section, then click + Create a Load Balancer:
Enter the Name of the Load Balancer, optionally you can enter a description and tags to simplify the management of them. Choose the Region for the Load Balancer (it should be the same region as the geographical region of your Instances), a new IP address is allocated automatically.
Create a frontend rule for the Load Balancer. This rule specifies on which external port the service will listen on the load balanced IP address. As Wordpress is a web based application, specify the Port 80 and a name for the rule:
Configure a backend rule, this rule defines the backend infrastructure that will be load balanced.
The following parameters should be configured in the backend rule:
Parameter Description Backend Name A name for the backend rule (e.g.
Protocol The protocol to use. Set this value to
HTTP, to have access to HTTP specific features of the Load Balancer
Port The port on which the backend application listens, with a standard configuration it is port 80 for a web application. It is also possible to use SSL to encrypt backend connections, in this case set the port to 443. Proxy This enables or disables PROXY protocol version 2 (must be supported by backend servers). It is not required for this tutorial, keep it off. Health Check Type The health check type to use. To check the health of a web application, set this to
URI The uniform resource identifier (URI) to check, to check if the Nginx web server is up, enter
/in the form.
Code The nginx web server sends a status code with with each response. The HTTP status code for successful requests is
200. Set this value in the form.
Sticky Session Enables the Load Balancer to bind a user’s session to a specific instance. This ensures that all subsequent sessions from the user are sent to the same instance, while there is at least one active session. Keep this feature disabled for this setup. Server IPs Add the two Wordpress Instances (
10.45.2.3) to the list of backend servers
The final backend rule should look similar to the following example:
Click Create a Load Balancer to deploy the Load Balancer.
It is possible to check the status of the Load Balancer with an API call. It will provide you information to status of the Load Balancer and if the health check was successful.
Set the required variables to make the API call easier:
REGION="<REGION>" # (can be either fr-par or nl-ams, depending on the load balancers region)
When the Load Balancer is configured and running, type the following command to request its status. Make sure that jsonpp is installed on the machine running the above command to get a easily readable JSON output:
curl -X GET "https://api.scaleway.com/lb/v1/regions/$REGION/lbs/$LB_ID/stats" -H "X-Auth-Token: $SECRET_KEY" | json_pp
The API will provide a JSON formatted response like the following example:
As two Instances are configured in the backend, the JSON list contains four entries. This is due to the high availability feature of the Load Balancer. Should the master instance experience a failure, it switches automatically to the backup one.
In the status of the
runninginstance, the health check status (
passed. This means that the backend instance replied well on the request sent to it in the health check. Requests to Wordpress are load balanced between the two Instances.
Connect to the first Wordpress instance (
10.45.2.3) and stop the web server application running on it:
systemctl nginx stop
Re-run the command from Step 3.
In the JSON list, the
failed, as the web server does no longer replies on requests:
When you navigate to the load balanced IP (
http://184.108.40.206) in your browser, your Wordpress displays. The Load Balancer has automatically detected that the Nginx server on the first instance (
10.45.2.3) is not running any more and redirects all traffic to the second instance (
Restart the web server application and re-run the command mentioned in step 3. The
last_health_check_statuswill change again into
passedand requests are load balanced again between the two Instances.
Update the configuration of each instance now, so they will use the database on the dedicated MariaDB instance instead of using a local database.
Connect to the instance via SSH as
Open the file
/var/www/wp-config.phpin a text editor and edit the database section as following:
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
/** MySQL database username */
/** MySQL database password */
/** MySQL hostname */
define('DB_HOST', '10.46.5.6'); #IP of the MariaDB instance
/** Database Charset to use in creating database tables. */
/** The Database Collate type. Don't change this if in doubt. */
Once edited, save the file and exit the text editor.
When connecting to the instance from a web browser, the content is taken from the database on the MariaDB server.
Log into the Wordpress Admin interface and click Settings to configure Wordpress.
- Enter the IP of one of your Wordpress Instances (
10.45.2.3) in the field WordPress Address (URL)
- Enter the Load Balanced IP (
220.127.116.11) or your domain name in the field Site Address (URL)
Save the form.
- Enter the IP of one of your Wordpress Instances (
Redo this step on the second instance.
Type the Load Balanced IP or your domain name in your browser, Wordpress will appear on the load balanced IP :
The Load Balancer is now automatically distributing the load between your Instances. To increase the computing power of the Load Balancer, simply snapshot an instance and spin up a new one.