How to setup wordpress replication?

many ways can be used, like rsync or scp . In this tutorial i prefer used tool called Lsyncd

Lsyncd is Live Synching Mirror Daemon, is based on rsync+ssh+daemon services.

  • You can easily sync local folder to remote folder in another server, triggering by inotify/fsevents.
  • You can write Lysncd configuration with Lua progamming language

Oke, let’s configure

Environment (same as a setup wordpress)

OS: Ubuntu 22.04 LTS

2 Server, with IP:

WP1: 10.10.10.176

WP2: 10.10.10.115

PHP 8.1

Install Lsyncd (Configure in WP1)
apt install lsyncd -y

You must install rsync in both server

apt install rsync -y
Setup Passwordless SSH

Because lsyncd using ssh to transfer file, we must configure passwordless ssh from WP1 to WP2

Generate Key

ssh-keygen -t ed25519 -C "wp"

Copy Public Key to WP2

ssh-copy-id root@wp2

Make sure your user is have access to nginx folder /var/www/wp or use root user to lsyncd, in this tutor i use root user

Lsyncd Configuration

Create lsyncd directory configuration

mkdir /etc/lsyncd

Create the configuration

vim /etc/lsyncd/lsyncd.conf.lua

In this lua configuration, you can add comment using — not # or //

--Set log file location
settings {
  logfile = "/var/log/lsyncd/lsyncd.log",
  statusFile = "/var/log/lsyncd/lsyncd-status.log",
  statusInterval = 10
}
--Define your local dir and remote dir, you can use ip/hostname(with manual adding in /etc/hosts)

sync {
  default.rsync,
  source="/etc/nginx/sites-available/",
  target="wp2:/etc/nginx/sites-available/",
}
sync {
  default.rsync,
  source="/var/www/wp/",
  target="wp2:/var/www/wp/",
}
sync {
  default.rsync,
  source="/etc/php/8.1/",
  target="wp2:/etc/php/8.1/",
}

-- Define default.rsync(you can custom this, like a same owner and group from source to remote dir, ssh port, key to accessing remote server, etc )
rsync = {
  compress = true,
  acls = true,
  verbose = true,
  owner = true,
  group = true,
  perms = true,
  rsh = "/usr/bin/ssh -p 22 -o StrictHostKeyChecking=no -i /root/.ssh/id_ed25519"
}

Start your lsyncd service

systemctl start lsyncd

Check log

tail -f /var/log/lsyncd/lsyncd.log
---
Fri Jul 21 08:25:19 2023 Normal: --- Startup, daemonizing ---
Fri Jul 21 08:25:19 2023 Normal: recursive startup rsync: /etc/nginx/sites-available/ -> wp2:/etc/nginx/sites-available/
Fri Jul 21 08:25:19 2023 Normal: recursive startup rsync: /var/www/wp/ -> wp2:/var/www/wp/
Fri Jul 21 08:25:19 2023 Normal: recursive startup rsync: /etc/php/8.1/ -> wp2:/etc/php/8.1/
Fri Jul 21 08:25:20 2023 Normal: Startup of /etc/php/8.1/ -> wp2:/etc/php/8.1/ finished.
Fri Jul 21 08:25:20 2023 Normal: Startup of /etc/nginx/sites-available/ -> wp2:/etc/nginx/sites-available/ finished.
Fri Jul 21 08:25:22 2023 Normal: Startup of /var/www/wp/ -> wp2:/var/www/wp/ finished.
---

Replication successful configured

By admin

Leave a Reply

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