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:
- Install Nginx:
sudo apt update sudo apt install nginx
- 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;
}
}
}
- 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:
- Primary Server Configuration (Master):
- 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
- Restart MySQL:
sudo systemctl restart mysql
- Create a replication user:
CREATE USER 'replica'@'%' IDENTIFIED BY 'password'; GRANT REPLICATION SLAVE ON *.* TO 'replica'@'%'; FLUSH PRIVILEGES;
- Obtain the binary log file name and position:
SHOW MASTER STATUS;
- Secondary Server Configuration (Slave):
- Edit the MySQL configuration file (
/etc/mysql/mysql.conf.d/mysqld.cnf
):
[mysqld] server-id = 2
- Restart MySQL:
sudo systemctl restart mysql
- 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;
- 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:
- Initial Sync:
rsync -avz /var/www/html/ webserver2:/var/www/html/
- Automate Sync with Cron: Add a cron job to automate file synchronization:
crontab -e
- 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:
- 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
- Add the following line to
/etc/exports
:
/var/nfs/general webserver1(rw,sync,no_subtree_check) webserver2(rw,sync,no_subtree_check)
- Export the shared directory:
sudo exportfs -a sudo systemctl restart nfs-kernel-server
- 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
- 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:
- Install HAProxy:
sudo apt update sudo apt install haproxy
- 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
- 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.