Deploy Hugo
Deploy a Hugo website anywhere
Prerequisites
Before diving into the deployment, ensure you have the following:
- Hugo installed on your local machine.
- A VPS running a Linux distribution (e.g., Ubuntu 22.04 or later).
- A custom domain name.
- Familiarity with using the terminal and SSH.
Step 1: Prepare Your VPS Environment
- Update the System:
sudo apt update && sudo apt upgrade -y
- Install Necessary Tools:
sudo apt install -y curl wget nano
- Ensure Docker is Installed: Check if Docker is installed by running:
docker --version
If not installed, you can set it up as follows:
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
Verify Docker is working:
sudo docker run hello-world
Step 2: Create a Dockerfile for Hugo
- Prepare Your Hugo Site: On your local machine, build your Hugo site:
hugo -D
This generates a public/
directory containing your site’s static files.
- Upload the public/ folder to your VPS:
scp -r public/ your-username@your-vps-ip:/var/www/html/hugo-site
Replace your-username with your VPS username and your-vps-ip with your server's IP address. This uploads the public/ folder to the VPS's /var/www/html/hugo-site directory.
- Create a Dockerfile:
On your VPS or local machine, create a
Dockerfile
for Hugo:
nano Dockerfile
Add the following content:
FROM nginx:alpine
LABEL maintainer="Your Name <youremail@example.com>"
# Remove the default Nginx HTML files
RUN rm -rf /usr/share/nginx/html/*
# Copy Hugo's public directory to Nginx
COPY public/ /usr/share/nginx/html
# Expose port 80
EXPOSE 80
# Start Nginx
CMD ["nginx", "-g", "daemon off;"]
- Build the Docker Image: Build the image for your Hugo site:
sudo docker build -t hugo-site .
- Run the Docker Container: Run the container to serve your Hugo site:
sudo docker run -d \
--name hugo-site \
-p 80:80 \
hugo-site
- Verify the Site: Visit your VPS IP address in a browser to ensure the site is running.
Step 3: Configure Caddy
Caddy is an excellent web server that automatically manages HTTPS certificates for your custom domain. We’ll use Docker to run Caddy.
- Create a Caddyfile: On your VPS, create a Caddy configuration file:
sudo nano /var/www/html/Caddyfile
Add the following configuration:
yourdomain.com {
reverse_proxy localhost:80
}
Replace yourdomain.com
with your custom domain.
- Run Caddy in Docker: Launch Caddy using Docker:
sudo docker run -d \
--name caddy \
-p 80:80 -p 443:443 \
-v /var/www/html/Caddyfile:/etc/caddy/Caddyfile \
-v caddy_data:/data \
-v caddy_config:/config \
caddy
- Check Logs: Confirm that Caddy is running and serving your site:
sudo docker logs caddy
Look for messages indicating that HTTPS certificates have been successfully issued.
Step 4: Point Your Domain to the VPS
Update your domain’s DNS settings to point to your VPS’s IP address:
- Log in to your domain registrar’s control panel.
- Create an A record with the following values:
- Name: @
- Type: A
- Value: Your VPS IP address
- TTL: 1 Hour or Default
Allow some time for DNS propagation. You can verify the DNS setup using:
nslookup yourdomain.com
Step 5: Secure Your VPS
-
Configure a Firewall: Allow only essential traffic to your VPS:
bash sudo ufw allow OpenSSH sudo ufw allow http sudo ufw allow https sudo ufw enable
-
Enable Automatic Updates: Keep your system updated to avoid vulnerabilities:
bash sudo apt install unattended-upgrades sudo dpkg-reconfigure --priority=low unattended-upgrades
-
Limit Docker Access: Ensure Docker is only accessible by authorized users:
bash sudo usermod -aG docker user
Step 6: Test Your Deployment
-
Access Your Site: Open your browser and visit
https://yourdomain.com
. You should see your Hugo site live. -
Check HTTPS: Verify the padlock icon in the address bar, indicating HTTPS is active.
-
Monitor Logs: Continuously monitor Caddy logs for any issues:
bash sudo docker logs -f caddy
Troubleshooting
-
DNS Propagation Delays: If your domain doesn’t resolve, use a DNS checker like
https://dnschecker.org
to verify propagation. -
Caddy Errors: Check the Caddyfile syntax and ensure ports 80 and 443 are open.
-
Docker Issues: Ensure the Docker container is running:
bash sudo docker ps