Automating Deployment of a Hugo Site to a DigitalOcean Droplet Using `rsync`
Deploying a static site generated by Hugo to a DigitalOcean droplet can be streamlined using rsync
. This guide walks you through automating the deployment process, ensuring your local changes are reflected on your live server efficiently.
Table of Contents
- Prerequisites
- Step 1: Set Up SSH Key-Based Authentication
- Step 2: Install and Configure Nginx on the Droplet
- Step 3: Build Your Hugo Site Locally
- Step 4: Create the Deployment Script
- Step 5: Adjust the
deploy.sh
Script for Environment Variables - Step 6: Install
rsync
on the Droplet - Step 7: Set Correct Ownership and Permissions
- Step 8: Automate Deployment with Git Hooks (Optional)
Prerequisites
- Local Machine:
- Hugo installed.
- A Hugo site set up locally.
rsync
installed.
- DigitalOcean Droplet:
- Debian-based OS.
- SSH access with root or a sudo-enabled user.
- Nginx installed.
- SSH Key-Based Authentication between your local machine and the droplet.
Step 1: Set Up SSH Key-Based Authentication
Generate an SSH key pair on your local machine and copy the public key to your droplet for password-less authentication.
Generate SSH Key Pair:
1ssh-keygen -t rsa -b 4096 -C "[email protected]"
Copy Public Key to Droplet:
1ssh-copy-id root@your_droplet_ip
Step 2: Install and Configure Nginx on the Droplet
Install Nginx:
1sudo apt update
2sudo apt install nginx
Configure Nginx Server Block:
Create a directory for your site:
1sudo mkdir -p /var/www/your_domain/html
Set permissions:
1sudo chown -R www-data:www-data /var/www/your_domain/html
2sudo chmod -R 755 /var/www/your_domain
Create a server block configuration:
1sudo nano /etc/nginx/sites-available/your_domain
Add the following content, replacing your_domain
with your actual domain:
1server {
2 listen 80;
3 server_name your_domain www.your_domain;
4
5 root /var/www/your_domain/html;
6 index index.html index.htm;
7
8 location / {
9 try_files $uri $uri/ =404;
10 }
11}
Enable the server block:
1sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
Test and reload Nginx:
1sudo nginx -t
2sudo systemctl reload nginx
Step 3: Build Your Hugo Site Locally
In your Hugo site directory, run:
1hugo
This command generates the static files in the public/
directory.
Step 4: Create the Deployment Script
Create a deploy.sh
script in your Hugo site directory:
1nano deploy.sh
Add the following content:
1#!/bin/bash
2
3export PATH="/usr/local/bin:$PATH"
4
5# Build the Hugo site
6hugo
7
8# Sync the public directory to your droplet with adjusted ownership and permissions
9rsync -avz --delete \
10 --chown=www-data:www-data \
11 --chmod=D755,F644 \
12 public/ root@your_droplet_ip:/var/www/your_domain/html
13
14echo "Deployment complete!"
Make the script executable:
1chmod +x deploy.sh
Step 5: Adjust the deploy.sh
Script for Environment Variables
Ensure the PATH
variable is correctly set so that rsync
is found when the script runs:
-
At the top of
deploy.sh
, include:1export PATH="/usr/local/bin:$PATH"
This adjustment ensures that rsync
and hugo
are found during script execution.
Step 6: Install rsync
on the Droplet
SSH into your droplet:
1ssh root@your_droplet_ip
Install rsync
:
1sudo apt update
2sudo apt install rsync
Verify installation:
1rsync --version
Step 7: Set Correct Ownership and Permissions
Ensure that the files are owned by www-data
and have the appropriate permissions.
- The
--chown
and--chmod
options in thersync
command withindeploy.sh
handle this.
Alternatively, adjust permissions on the droplet:
1sudo chown -R www-data:www-data /var/www/your_domain/html
2sudo find /var/www/your_domain/html -type d -exec chmod 755 {} \;
3sudo find /var/www/your_domain/html -type f -exec chmod 644 {} \;
Step 8: Automate Deployment with Git Hooks (Optional)
Automate the deployment process to run after each commit.
Create a Git post-commit
Hook:
In your .git/hooks/
directory, create or edit post-commit
:
1nano .git/hooks/post-commit
Add the following content:
1#!/bin/bash
2
3./deploy.sh
Make the hook executable:
1chmod +x .git/hooks/post-commit
Now, every time you commit changes, the site will automatically build and deploy.