NavigationContentFooter
Jump toSuggest an edit

Setting up a load balanced WordPress

Reviewed on 13 May 2024Published on 08 April 2019
  • WordPress
  • Load-Balancer

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 spreading 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 Balancer’s backend. This allows you to scale your infrastructure without any downtime or delays whilst waiting for DNS zones to be updated.

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

Before you start

To complete the actions presented below, you must have:

  • A Scaleway account logged into the console
  • Owner status or IAM permissions allowing you to perform actions in the intended Organization
  • An SSH key

Prework - Setting up the backend Instances

During the tutorial the following IPs are used:

  • 10.45.2.3 for the first WordPress instance
  • 10.45.2.4 for the second WordPress instance
  • 10.46.5.6 for the MariaDB instance
  • 51.51.51.51 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), and 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), and 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:

    ParameterDescription
    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 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.
    TLS encryptionEnable Transport Layer Security (TLS) to encrypt connections between the Load Balancer and the backend server(s).
    Health Check TypeThe health check type to use. To check the health of a web application, set this to HTTP.
    Server IPsAdd the two WordPress Instances (10.45.2.3 and 10.45.2.3) 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 with information about the status of the Load Balancer and if the health check was successful.

Important

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:

    LB_ID="<LOAD_BALANCER_ID>"
    TOKEN="<API_SECRET_KEY>"
    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 an 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": "10.45.2.3",
    "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": "10.45.2.4",
    "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": "10.45.2.3",
    "last_health_check_status": "passed"
    },
    {
    "ip": "10.45.2.4",
    "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 to 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 (10.45.2.3) 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 no longer replies to requests:

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

    When you navigate to the load balanced IP (http://51.51.51.51) 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 anymore and redirects all traffic to the second instance (10.45.2.3).

  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 follows:

    // ** 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', '10.46.5.6'); #IP of the MariaDB instance
    /** Database Charset to use in creating database tables. */
    define('DB_CHARSET', 'utf8');
    /** The Database Collate type. Do not 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 (10.45.2.3) in the field WordPress Address (URL)
    • Enter the Load Balanced IP (51.51.51.51) 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, and 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.

Docs APIScaleway consoleDedibox consoleScaleway LearningScaleway.comPricingBlogCarreer
© 2023-2024 – Scaleway