configuring loadbalancer wordpress

Jump toUpdate content

Setting up a load balanced Wordpress

Published on 08 April 2019
  • compute
  • server
  • Cloud
  • Wordpress
  • Load-Balancer

Load Balancer Overview

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.

In this tutorial you learn how to setup a Scaleway managed Load Balancer with two Wordpress Instances and one Database Instance running MariaDB. The final setup is visible in this schema:

Identity and Access Management (IAM):

If you have activated IAM, you may need certain IAM permissions to carry out some actions described on this page. This means:

  • you are the Owner of the Scaleway Organization in which the actions will be carried out, or
  • you are an IAM user of the Organization, with a policy granting you the necessary permission sets

Prework - Setting up the Backend Instances

During the tutorial the following IPs are used:

  • for the first Wordpress instance
  • for the second Wordpress instance
  • for the MariaDB instance
  • for 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.
  1. Follow this tutorial to start an Ubuntu Instances and to install WordPress with LEMP on both of them.
  2. Set up a third instance with a MariaDB database as explained in this tutorial.

Configuring a Load Balancer

  1. Click Load Balancer in the menu on the left, to enter the Load Balancer section, then click + Create a Load Balancer:

  2. 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.1. Click Load Balancer in the menu on the left, to enter the Load Balancer section, then click + Create a Load Balancer:

  3. 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.

  4. 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:

    Backend NameA name for the backend rule (e.g. wordpress-backend-rule)
    ProtocolThe protocol to use. Set this value to HTTP, to have access to HTTP specific features of the Load Balancer
    PortThe 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.
    ProxyThis 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 TypeThe health check type to use. To check the health of a web application, set this to HTTP.
    URIThe uniform resource identifier (URI) to check, to check if the Nginx web server is up, enter / in the form.
    CodeThe 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 SessionEnables 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 IPsAdd the two Wordpress Instances ( and to the list of backend servers

    The final backend rule should look similar to the following example:

  5. Click Create a Load Balancer to deploy the Load Balancer.

Checking if the Load Balancer works

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.


Retrieve your organization ID and generate API Key from your management console before you continue.

  1. 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)
  2. 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
  3. The API will provide a JSON formatted response like the following example:

    "backend_servers_stats": [
    "ip": "",
    "backend_id": "cd64b4e0-2c23-484a-80b9-602bf934245e",
    "instance_id": "4d2294bf-4e6d-499c-922a-ca08f58ed66d",
    "server_state_changed_at": "2019-04-10T13:01:27Z",
    "server_state": "stopped",
    "last_health_check_status": "failed"
    "server_state": "stopped",
    "server_state_changed_at": "2019-04-10T13:01:27Z",
    "instance_id": "4d2294bf-4e6d-499c-922a-ca08f58ed66d",
    "ip": "",
    "backend_id": "cd64b4e0-2c23-484a-80b9-602bf934245e",
    "last_health_check_status": "unknown"
    "instance_id": "bd4b6e5f-2cfb-40da-afd8-933e7afbbd61",
    "server_state_changed_at": "2019-04-10T13:01:27Z",
    "server_state": "running",
    "backend_id": "cd64b4e0-2c23-484a-80b9-602bf934245e",
    "ip": "",
    "last_health_check_status": "passed"
    "ip": "",
    "backend_id": "cd64b4e0-2c23-484a-80b9-602bf934245e",
    "instance_id": "bd4b6e5f-2cfb-40da-afd8-933e7afbbd61",
    "server_state_changed_at": "2019-04-10T13:01:27Z",
    "server_state": "running",
    "last_health_check_status": "passed"

    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 running instance, the health check status (last_health_check_status) has 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.

  4. Connect to the first Wordpress instance ( and stop the web server application running on it:

    systemctl stop nginx.service
  5. Re-run the command from Step 3.

  6. In the JSON list, the last_health_check_status changed to failed, as the web server does no longer replies on requests:

    "last_health_check_status": "failed",
    "server_state": "stopped",
    "instance_id": "bd4b6e5f-2cfb-40da-afd8-933e7afbbd61",
    "ip": "",
    "server_state_changed_at": "2019-04-10T13:32:18Z",
    "backend_id": "cd64b4e0-2c23-484a-80b9-602bf934245e"

    When you navigate to the load balanced IP ( in your browser, your Wordpress displays. The Load Balancer has automatically detected that the Nginx server on the first instance ( is not running any more and redirects all traffic to the second instance (

  7. Restart the web server application and re-run the command mentioned in step 3. The last_health_check_status will change again into passed and requests are load balanced again between the two Instances.

Configuring Wordpress

Update the configuration of each instance now, so they will use the database on the dedicated MariaDB instance instead of using a local database.

  1. Connect to the instance via SSH as root

  2. Open the file /var/www/wp-config.php in 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 */
    define('DB_NAME', 'wordpressdb');

    /** MySQL database username */
    define('DB_USER', 'wordpress');

    /** MySQL database password */
    define('DB_PASSWORD', '<yoursecretpassword>');

    /** MySQL hostname */
    define('DB_HOST', ''); #IP of the MariaDB instance

    /** Database Charset to use in creating database tables. */
    define('DB_CHARSET', 'utf8');

    /** The Database Collate type. Don't change this if in doubt. */
    define('DB_COLLATE', '');

    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.

  3. Log into the Wordpress Admin interface and click Settings to configure Wordpress.

    • Enter the IP of one of your Wordpress Instances ( in the field WordPress Address (URL)
    • Enter the Load Balanced IP ( or your domain name in the field Site Address (URL)

    Save the form.

  4. Redo this step on the second instance.

  5. 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.