Get Rewarded! We will reward you with up to €50 credit on your account for every tutorial that you write and we publish!

Using the Hetzner Load Balancer with WordPress and Plesk

profile picture
Author
Christian Hillenkötter
Published
2020-10-22
Time to read
6 minutes reading time

Introduction

The goal of this tutorial is to have two servers with Plesk up and running, including a page which is load balanced between the two servers. The WordPress setup will be linked to the database of one Plesk Server. This tutorial does not include a HA MySQL Setup, but this could be part of an additional tutorial in the future. Also files, e.g. uploaded from WordPress, are not synced. The tutorial is meant to demonstrate how the Hetzner Load Balancer could be used. You should check upfront if you have other requirements (e.g. shared files or sessions) which must be kept in sync.

Previous tutorials have covered how to install Plesk and configure WordPress.

Prerequisites

  • 2 servers that have Plesk installed with the extension "WordPress Toolkit" (see tutorials linked above)
  • Hetzner Load Balancer

Step 1 - Setup your website

You need two servers with Plesk and WordPress Toolkit. On one server, you should have a website, in our case a WordPress page.

In the next step, we will migrate this WordPress page to Server 2. If password authentication is disabled on server 1, you will need an SSH key for the migration:

  • On Server 2, generate a new SSH key

    ssh-keygen -t ed25519
  • On Server 1, save the public SSH key of server 2 in:

    ~/.ssh/authorized_keys

Step 2 - Migrate

Assuming we have our WordPress page up and running on Server 1, we are going to import it to Server 2 with the Plesk Migrator.

Therefore, login to your Plesk on Server 2.

  • Click on "Extensions" » "Extensions Catalog"
  • Search for the Plesk Migrator
  • Then, either install and open it, or open it directly if already installed.
  • Next click on "Stard a New Migration" and add your credentials of Server 1.
  • Click on "Prepare migration".
  • On the next page, select your domain from Server 1 and migrate it.

Step 3 - Share the database

As said in our introduction, we are connecting the WordPress installation of Server 2 to the same database as Server 1. Therefore, we need to connect to Server 1. In the my.cnf file we need to change the bind-address to 0.0.0.0.

You can usually find it in:

OS Location
CentOS /etc/my.cnf
Ubuntu / Debian /etc/mysql/my.cnf

Make the following change on Server 1:

bind-address = 0.0.0.0

After this, MySQL / MariaDB needs to be restarted.

  • Check if you have MySQL or MariaDB

    systemctl list-units | grep -E "mysql|mariadb"
  • Run one of these commands

    sudo systemctl restart mariadb
    sudo systemctl restart mysql
  • Check status

    sudo systemctl status mariadb
    sudo systemctl status mysql

Now we can link the WordPress installation on Server 2 to the MySQL Database on Server 1.

For this, you have to edit the file wp-config.php of your imported website on Server 2.

Connect to Server 2 and edit:

/var/www/vhosts/<your_domain>/httpdocs/wp-config.php

Change the entry where DB_HOST is defined. Replace 10.0.0.2 with the IP or domain (e.g. 'example.com:3306') of your Server 1:

/** MySQL hostname */
define( 'DB_HOST', '10.0.0.2:3306' );

Step 4 - Add page to LB

Now we need to configure the Hetzner Load Balancer. Therefore, log into your Hetzner Console account and select your existing project.

In the left menu bar, select Load Balancer and click "Create Load Balancer".

Description
Location For the location, it makes sense to select the same location where your servers are located, or from where you expect the most requests.
Targets Click on "Add targets" and select both of your servers.
Services
  • Leave the first one (http) as it is
  • Add a second one by clicking on "Add service" » "New service". As protocol - TCP, Source port and Destination port both set to 443.
Algorithm The algorithm can be left as it is.

Last but not least, we should give it a name. After clicking on "Create & Buy now", your Load Balancer is created.

Load Balancer

Step 5 - Change DNS

Now we need to change the IP in the DNS of our domain to that of the Load Balancer. If you are using the DNS of Plesk on Server 1, you can do it directly from there. How to connect Plesk with the Hetzner DNS Server is available as a separate tutorial. If you don't use your own Plesk Server as DNS, you have to change the IPs at the nameserver of your hoster / domain provider.

Step 6 - Test

To see if it really works, you could edit the index.php of your WordPress installation on your Server 1 and Server 2 and add this into the 2nd line of the file:

You should find the file in /var/www/vhosts/<your_domain>/httpdocs/index.php

echo gethostname();

It could be the case that your browser caches this page, so you need to force a reload of the website to see the change. In addition, DNS changes from Step 5 could take up to 24 hours.

Conclusion

You're now using the Hetzner Load Balancer with WordPress and Plesk. And this is it - enjoy your new Load Balancer setup!

License: MIT
Want to contribute?

Get Rewarded: Get up to €50 in credit! Be a part of the community and contribute. Do it for the money. Do it for the bragging rights. And do it to teach others!

Report Issue

Discover our

Load Balancer

Distribute traffic between multiple targets and avoid having a single point of failure.

Want to contribute?

Get Rewarded: Get up to €50 credit on your account for every tutorial you write and we publish!

Find out more