Implementing High Availability (HA) for WordPress

High Availability (HA) ensures that your WordPress site remains accessible and operational even during hardware failures, traffic spikes, or other issues. Setting up HA for WordPress involves several components, including load balancing, database replication, and failover mechanisms. Here’s a step-by-step guide to achieving HA for your WordPress site.

1. Load Balancing

Load balancing distributes incoming traffic across multiple web servers, preventing any single server from becoming overwhelmed.

Using Nginx as a Load Balancer:

  1. Install Nginx:
sudo apt update sudo apt install nginx
  1. Configure Nginx as a Load Balancer: Edit the Nginx configuration file (/etc/nginx/nginx.conf):
http {     
  upstream backend {         
    server webserver1.example.com;         
    server webserver2.example.com;     
}      
server {         
  listen 80;         
  server_name example.com;          
  location / {             
    proxy_pass http://backend;             
    proxy_set_header Host $host;             
    proxy_set_header X-Real-IP $remote_addr;             
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;             
    proxy_set_header X-Forwarded-Proto $scheme;         
    }     
  } 
}
  1. Restart Nginx:
sudo systemctl restart nginx

2. Database Replication

To ensure database high availability, you need to set up a primary-secondary (master-slave) replication.

Using MySQL for Database Replication:

  1. Primary Server Configuration (Master):
  2. Edit the MySQL configuration file (/etc/mysql/mysql.conf.d/mysqld.cnf):
[mysqld] server-id = 1 log_bin = /var/log/mysql/mysql-bin.log binlog_do_db = your_database_name
  1. Restart MySQL:
sudo systemctl restart mysql
  1. Create a replication user:
CREATE USER 'replica'@'%' IDENTIFIED BY 'password'; GRANT REPLICATION SLAVE ON *.* TO 'replica'@'%'; FLUSH PRIVILEGES;
  1. Obtain the binary log file name and position:
SHOW MASTER STATUS;
  1. Secondary Server Configuration (Slave):
  2. Edit the MySQL configuration file (/etc/mysql/mysql.conf.d/mysqld.cnf):
[mysqld] server-id = 2
  1. Restart MySQL:
sudo systemctl restart mysql
  1. Set up the slave to replicate from the master:
CHANGE MASTER TO MASTER_HOST='primary_server_ip', 
MASTER_USER='replica', MASTER_PASSWORD='password', 
MASTER_LOG_FILE='mysql-bin.000001', 
MASTER_LOG_POS=  154; START SLAVE;
  1. Check the slave status:
SHOW SLAVE STATUS\G

3. File Synchronization

Ensure that all web servers have the same WordPress files. Use a tool like rsync to keep files synchronized across servers.

Using rsync for File Synchronization:

  1. Initial Sync:
rsync -avz /var/www/html/ webserver2:/var/www/html/
  1. Automate Sync with Cron: Add a cron job to automate file synchronization:
crontab -e
  1. Add the following line to sync files every hour:
0 * * * * rsync -avz /var/www/html/ webserver2:/var/www/html/

4. Shared Storage

For uploads and other dynamic content, consider using a shared storage solution like NFS or a cloud storage service like Amazon S3.

Using NFS for Shared Storage:

  1. Setup NFS Server:
sudo apt install nfs-kernel-server sudo mkdir -p /var/nfs/general 
sudo chown nobody:nogroup /var/nfs/general sudo nano /etc/exports
  1. Add the following line to /etc/exports:
/var/nfs/general webserver1(rw,sync,no_subtree_check) webserver2(rw,sync,no_subtree_check)
  1. Export the shared directory:
sudo exportfs -a sudo systemctl restart nfs-kernel-server
  1. Mount NFS on Web Servers:
sudo apt install nfs-common sudo mkdir -p /var/www/html/wp-content/uploads 
sudo mount nfs_server_ip:/var/nfs/general /var/www/html/wp-content/uploads
  1. Add the following line to /etc/fstab to mount the NFS share at boot:
nfs_server_ip:/var/nfs/general /var/www/html/wp-content/uploads nfs defaults 0 0

5. Health Checks and Failover

Implement health checks and automatic failover to ensure high availability.

Using HAProxy for Health Checks and Failover:

  1. Install HAProxy:
sudo apt update sudo apt install haproxy
  1. Configure HAProxy: Edit the HAProxy configuration file (/etc/haproxy/haproxy.cfg):
global     
log /dev/log local0     
log /dev/log local1 notice     
chroot /var/lib/haproxy     
stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners     
stats timeout 30s     
user haproxy     
group haproxy     
daemon  defaults     
log global     
mode http     
option httplog     
option dontlognull     
timeout connect 5000     
timeout client  50000     
timeout server  50000  
frontend http_front     
bind *:80     
stats uri /haproxy?stats     
default_backend http_back  backend http_back     
balance roundrobin     
server webserver1 webserver1_ip:80 check     
server webserver2 webserver2_ip:80 check
  1. Restart HAProxy:
sudo systemctl restart haproxy

Conclusion

Implementing High Availability for your WordPress site involves setting up load balancing, database replication, file synchronization, shared storage, and health checks with failover mechanisms. By following these steps, you can ensure your WordPress site remains accessible and performs optimally even during failures or high traffic periods.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top
en_USEnglish