
WordPress is a user-friendly Content Management System (CMS) that allows you to create high-performance web applications that serve global audiences. Usually, WordPress site users come from different regions attracting performance bottlenecks in some locations.
GeoDNS (Geographical domain name system) is a traffic distribution method that returns specific DNS records depending on a client’s location. By integrating WordPress with GeoDNS, different servers deliver your WordPress site to different audiences depending on the user's location.
This article explains how you can bring WordPress to the edge with Vultr and GeoDNS by leveraging multiple server locations offered by Vultr. You are to deploy your WordPress site to multiple locations and serve users with the same copy of your site with different resource accessibility speeds.
Before you begin:
Migrate your domain name servers to a GeoDNS-compatible provider such as CloudDNSor Cloudflare
Deploy the main WordPress server using the Vultr Marketplace application
If you have an existing WordPress server, mark it as the main server to synchronize other servers participating in your GeoDNS network. This article uses the main server address
192.0.2.1, replace it with your actual WordPress server IP address.
Deploy child Ubuntu servers in different locations
This article uses 9 Ubuntu servers in the following Vultr locations:
Replace the example IP address class
192.0.2.0with your actual public Server IP Addresses
Using SSH, access each of the servers in separate terminal sessions
On each child server:
Create the same non-root user with sudo privileges
Switch to the new sudo user
This article uses
sysadmin, replace the user with your actual sudo user account
Install the LEMP stack
The main WordPress server works as the major point of connection for all site visitors. To enable all child servers to work in the WordPress GeoDNS cluster, perform the following actions on the main WordPress server.
3306Navigate to your WordPress web root files directory
View the wp-config.php file and note the WordPress database name
Output:
Navigate to the MySQL configuration files directory
Using a text editor such as Nano, edit the main configuration file
Add the following configurations to the [mysqld] section
Save and close the file.
The above configuration sets the MySQL database server replication ID to 1, keeps log files in the /var/log/mysql directory, sets the wordpressdb database as the replication database, and enables the deletion of binary log files after every 5 days.
Navigate to the mysql.conf.d or mariadb.conf.d directory
Edit the 50-server.cnf file
Find the following section:
Change the bind-address value from 127.0.0.1 to 0.0.0.0 to enable connections from all server interfaces
Save and close the file.
The above address declaration allows child servers to access the main MySQL database server using the public IP Address.
Create the MySQL log files directory
Grant the mysql user full privileges to the directory
Restart the MySQL database server
Backup the MySQL WordPress database
Move the .sql database file to the WordPress webroot directory
Log in to the MySQL console
Create a new WordPress replication user. For example wpreplica with a strong password
Switch to the WordPress database
Grant the user replication privileges on the database
Refresh MySQL privileges
View the database server replication status
Output:
Keep note of the File and Position to correctly set MySQL database replication on the child servers
To synchronize WordPress files from the main server to the child servers, create a secure SSH connection to each of the servers using SSH keys. When enabled, use a file transfer protocol such as Rsync to transfer directory changes every 20 seconds to the destination WordPress web root directory as described below.
Generate a new SSH key
When prompted, press Enter to accept all default options and keep the passphrase field empty to disable password authentication
Register the public SSH key to your child WordPress server
When prompted, accept the remote server fingerprint, then enter the server password to enable your SSH key on the child server. When successful, your output should look like the one below:
Repeat the above process for each child WordPress server to ensure connectivity from the main server.
Establish an SSH connection to the child WordPress server to test your SSH key
Verify that the SSH connection starts without password authentication.
Create the WordPress webroot directory
Grant the web server full privileges to the directory
Change the non-root user's main group to the web server group www-data. Replace sysadmin with your actual user account
Grant the www-data group full privileges to the WordPress webroot directory
Long list the parent directory to view the user and group permissions
Output:
Verify that the group has write, read, and execute privileges on the directory.
Create a new Nginx SSL directory to store SSL certificates
Grant the web server full privileges to the directory
Grant the www-data group full privileges to the directory
Close the SSH connection
Using Rsync, transfer files from the main WordPress server to the child WordPress server
When successful, your transfer summary should look like the one below:
To automate the Rsync process to run every 20 seconds and keep WordPress files updated, create a new bash script in a system-wide directory such as /opt/
Add the following contents to the file
Save and close the file.
The above configuration sets the Rsync command to execute using a while loop every 20 seconds. This updates your WordPress files on the child server each time new file changes are available in the main server directory.
Grant execute privileges on the script
Create a new system service to automate the script
Add the following contents to the file. Replace sysadmin with your actual user account available on the child WordPress server
Save and close the file
The above configuration runs the Rsync script as a system service and transfers files using the set user and group.
Restart the systemd daemon
Enable the service to start at boot time
Start the service
View the service status and verify that it’s actively running without any error
Output:
You have set up Rsync to synchronize your WordPress webroot files from the main server to the child servers. Repeat the synchronization process for each child server and verify that every server has an Rsync record in the automated script to transfer WordPress files.
By default, the Uncomplicated Firewall (UFW) is available on Vultr servers. Configure Firewall rules to allow trusted access to all necessary server ports as described below.
Verify the firewall status
When active, the firewall table should display. When inactive, allow the SSH port 22 and enable the firewall
Allow access to the MySQL port 3306 from trusted child WordPress servers
Allow the HTTP port 80 through the firewall
Allow the HTTPS port 443
Restart the firewall to save changes
WordPress requires a valid SSL certificate to correctly handle HTTPS requests. Generate Let’s Encrypt SSL certificates on the main server and synchronize them for transfer to the child servers to use the same SSL certificate as described below.
Install the Certbot Let’s Encrypt Client Tool
Generate a new SSL Certificate. Replace example.com with your actual domain name
When prompted, enter your email address to deploy a new SSL certificate to your Nginx configuration file and save it to the /etc/letsencrypt/live/ directory
List files in the /etc/letsencrypt/live/example.com/ directory
Output:
Verify that the private key and public key .pem files are available in the directory
Allow read permissions on the /etc/letsencrypt/archive directory
The /etc/letsencrypt/live directory references the archive directory, to avoid any file transfer errors, the files must be readable
Edit the Rsync synchronization script
Add the following directive within the while { loop before the sleep directive
Save and close the file
The above configuration checks for changes in the SSL certificate and transfers updated .pem files to the child WordPress server SSL directory
To enable the child WordPress servers to use the synchronized files, prepare the MySQL database, and configure the web server as described in the steps below.
Switch to the WordPress webroot directory
Long list files and verify that all WordPress files are available in the web root directory
Output:
Verify that your MySQL database backup file wpdb.sql is also available in the directory
View the wp-config.php file
Keep note of the WordPress database user and password:
Log in to the MySQL database console
Recreate the WordPress database
Create the WordPress database user with the same password listed in the wp-config.php file. For example wpuser
Grant the user full privileges to the WordPress database
Refresh MySQL privileges
Close the MySQL console
Restore your MySQL database from the wpdb.sql file to the WordPress database
When successful, delete the MySQL backup file from your WordPress files directory
Navigate to the MySQL configuration files directory
Edit the my.cnf main configuration file
Add the following configurations to the [mysqld] section
Save and close the file
The above configuration sets the child replication ID to 2 different from the main database server ID 1. Every database server in the replication process must use a unique ID value. wordpressdb is the destination replication database and the binary logs expire every 5 days
Create the MySQL logs directory
Grant the MySQL user full privileges to the directory
Restart the MySQL database server
Access the MySQL console
Run the following SQL statements. Replace mysql-bin.00, and 766 with the actual file and position details you generated earlier on the main MySQL database server
Replace 192.0.2.1 with your main WordPress server IP address
Start the replication process
Verify the replication status
Output:
Verify that the Slave_IO_State field returns a Waiting for master to send event notice. If the state is connecting, view the Last_SQL_Error: field and fix the displayed error to start replicating database records from the main WordPress server.
Navigate to the Nginx configuration files directory
List files in the SSL directory and verify that all WordPress SSL certificates are available
Output:
Deactivate the default Nginx host configuration file
Create a new Nginx host configuration file in the sites-available directory
Add the following configurations to the file. Replace example.com with your actual domain name
Save and close the file
The above configuration points to the WordPress webroot directory, and accepts HTTPS connections using your SSL certificates in the /etc/nginx/ssl/ directory
Activate the WordPress configuration file
Test the Nginx configuration for errors
Restart Nginx to save changes
Verify the UFW firewall status
Output:
Allow the HTTP port 80 through the firewall
Allow the HTTPS port 443 through the firewall
Restart the Firewall to save the new rules
Repeat the above process on all child WordPress servers to synchronize the latest files and MySQL database from the main server.
To enable GeoDNS on your WordPress site, configure multiple domain A records to redirect users in different locations to the closest server. In this section, use ClouDNS to configure your domain records as described in the steps below.
Access your ClouDNS account
In your DNS Zones section, click to open your target WordPress domain name records
Click DNS Records
On the records page, navigate to A
Verify that a host record with the Default location tag is available on the list and points to your main WordPress server
Click + Add new record within the A section
In the Add New Record pop-up, click the Location drop-down and select your first target location. For example Europe
Navigate to the Points to: field and enter the WordPress server IP address in your target location. For example 192.0.2.104 for the Paris (France) Vultr server.
Click Save to apply your new domain A record
Repeat the above process for each domain A record and verify that every Vultr Server IP Address points to the correct target location
To verify that your WordPress GeoDNS configuration works well, verify that the MySQL database replication works, and your domain returns a different IP address depending on the user location as described below.
To test the database replication process, access the MySQL console on the main WordPress server
Switch to the WordPress database
Create a new table test with the following columns
Close the MySQL console
Access the MySQL console on any of the configured WordPress child servers
Switch to the WordPress database
Show tables and verify that the new test table is available
Output:
When successful, the main WordPress server replicates all MySQL database changes to the child server database in real-time
In a web browser such as Chrome, visit your WordPress site and verify that it loads correctly
Enable a VPN on your device, and access the WordPress site again to simulate a new location. Verify that it displays correctly without any error
Using a DNS propagation checker such as WhatsMyDNS, query your domain name records to view the active IP address per location
To further test your WordPress GeoDNS configuration, use a site speed testing tool with different locations to detect the performance differences between your WordPress site. The closest server to a user’s location handles the user request which improves your site performance with no change in the functionality across all regions.
When deploying Nginx configuration records on child WordPress servers, the web server displays a critical error 500 due to a misconfiguration in your files. Fix the error as described in the steps below.
View the WordPress host error log to view the source of the error
Output:
Depending on the source of the error displayed in the log, apply steps to fix it. For example, to fix the permission error displayed in the above output, delete files in the WordPress webroot directory to synchronize new files with correct permissions from the main WordPress server after 20 seconds
List files in the directory after 20 seconds and verify that all WordPress files are available with the correct www-data group permissions
Restart Nginx to apply changes
Revisit your WordPress site in the target location and verify that the error does not show up again
View the installed PHP version
Output:
Verify that the installed PHP-FPM version is active and running
View your WordPress host configuration file
Verify that the fastcgi_pass directive points to the correct PHP-FPM version
If the referenced PHP-FPM version is wrong, edit the file
Change the factcgi_pass directive to the correct version
Restart Nginx to apply configuration changes
Access the child MySQL database console
View the Child database replication status and view the error returned in the Last_SQL_Error: column
Output:
For example:
Error executing row event: 'Table 'wordpressdb.wp_options' doesn't exist'
Verify that you imported the WordPress database to the child server. If not, navigate to the WordPress webroot directory
Verify that the .sql database backup file is available
Import the WordPress database to your child MySQL database
Last_SQL_Error: Could not execute Write_rows_v1 event on table wordpressdb.
The MySQL database is unable to write some changes that could result from data corruption. Exclude the error and restart the replication process by running the following SQL statement
Last_IO_Error: error connecting to master 'wpreplica@192.0.2.1:3306' - retry-time: 60 maximum-retries: 100000
Verify that the child server address has access to the MySQL port 3306 through the main WordPress server firewall
Verify that you entered the correct wpreplica user password in your child database configuration. Stop the database replication process on the child server
Set the main database details using the following SQL statement
Verify that the database user and password details are correct. Replace 192.0.2.1 with the main server address to replicate database changes.
Start the WordPress database replication again
Could not execute Write_rows_v1 event error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql-bin.000003, end_log_pos
Access the Main WordPress server database
View the database replication status and take note of the file and position
Output:
Exit the MySQL console
Access your child database server
Stop database replication
Change the replication information with the correct MASTER_LOG_FILE and MASTER_LOG_POS fields as displayed on the main database server
Start the database replication process
View the replication status and verify that the state changes to Waiting for master to send event
Output:
You have implemented GeoDNS to work with your WordPress site by leveraging multiple Vultr locations. This is important when handling user requests from multiple regions across the world. Instead of a single server receiving requests, multiple servers enable redundancy which improves your resource usage while handling WordPress user requests.
To implement more solutions on your WordPress site, visit the following resources:
0 Comments
Be the first to comment and share your perspective with the community.