NGINX Synchronization
For this project, the cloud-hosted server is synchronized with a local server. The locally hosted server serves as the parent, while the cloud-hosted server serves as the child. This unifies change management for the production environment. It also allows specific web applications and data to be available only when the server is accessed from the local network. When following these steps, ensure you are SSHed into both the local server and cloud server.
Local Server
Create an SSH key:
ssh-keygen -t ed25519 -C "pi-to-linode-web-sync"
Display, then copy your public key:
cat ~/.ssh/id_ed25519.pub
Cloud Server
SSH into your cloud server, then open your authorized keys file:
nano ~/.ssh/authorized_keys
Paste the key you copied into a new line.
Press CTRL+O to save the file, then press CTRL+X to close.
Ensure permissions are set correctly:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Give your SSH user permission to access and change the HTML directory:
sudo chown -R user-name:www-data /var/www/html
sudo chmod -R 775 /var/www/html
Local Server
Test the SSH connection to your cloud server:
ssh -p port## user-name@your-linode-ip
Perform a manual test sync:
rsync -avz --delete -e 'ssh -p port##' \
/var/www/html/ \
user-name@your-linode-ip:/var/www/html/
Create a sync script:
sudo nano /usr/local/bin/sync-website-to-linode.sh
Add the below, modifying the appropriate values:
#!/bin/bash
rsync -avz --delete-delay \
-e 'ssh -p port##' \
--exclude ".git/" \
--exclude "*.bak" \
--exclude "*.tmp" \
/var/www/html/ \
youruser@your-linode-ip:/var/www/html/
Press CTRL+O to save the file, then press CTRL+X to close.
Make the script executable:
sudo chmod +x /usr/local/bin/sync-website-to-linode.sh
Schedule an automated sync:
sudo crontab -e
Add the below line, modifying the appropriate values (this example syncs every 12 hours):
0 */12 * * * /usr/local/bin/sync-website-to-linode.sh >> /var/log/website-sync.log 2>&1
Press CTRL+O to save the file, then press CTRL+X to close.
Create a shortcut to simplify manually running the script:
sudo ln -s /usr/local/bin/sync-website-to-linode.sh /usr/local/bin/shortcut-name
Manual syncing can now be completed using:
sudo shortcut-name
The sync log can be checked using:
sudo tail -f /var/log/website-sync.log