Deploy with Rclone
Use these instructions to deploy your site to a web server with Rclone.
Deployment constraints
Deploying a Hugo site to a web server requires more than copying files. For example, typical FTP clients do not remove orphaned files from the server, and compare files by modification time and size.
Deployments must remove orphaned files. When you rename a page, change its permalink, or delete content, Hugo does not remove the corresponding files from your local
publicdirectory. Hugo also leaves a page’s files in place when the page becomes a draft, expires, or has its publication date changed to a future date. To remove these files, clear thepublicdirectory before you build, or build with the--cleanDestinationDirflag or thecleanDestinationDirconfiguration option enabled. Unless your deployment also removes them from the server, the old pages remain online.Deployments should compare files by checksum. Hugo updates the modification time of every file in the
publicdirectory on every build, so tools that compare modification times transfer your entire site each time. Tools that compare only file size can miss changed files, because editing text does not always change a file’s size.
Rclone meets both requirements.
Prerequisites
Please complete the following tasks before continuing:
- Create a Hugo project and test it with the
hugo servercommand. - Set the
baseURLin your project configuration to the URL of your production site. - Configure SSH key authentication to log in to your web server, then log in once with the
sshcommand to add your web server to yourknown_hostsfile. See your hosting provider’s documentation for details. - Load your private key into
ssh-agenton your local machine, typically with thessh-addcommand. Rclone authenticates through the agent and does not read your key files directly. See SSH authentication in the Rclone documentation for details. - Grant your SSH user write access to the document root, the directory from which your web server serves files.
- Install
rcloneon your local machine. You do not need to installrcloneon your web server. - Use a POSIX shell such as Bash or Zsh on Linux or macOS. On Windows, use Windows Subsystem for Linux (WSL).
Procedure
- Step 1
- Create a
deploy.shfile in the root of your project, adjusting the configuration values as needed.deploy.sh#!/usr/bin/env sh #------------------------------------------------------------------------------ # @file # Builds a Hugo project and deploys it to a web server with Rclone. #------------------------------------------------------------------------------ # Exit on error or undefined variables set -eu # Configuration SSH_USER="jsmith" SSH_HOST="example.org" SSH_PORT="22" SRC="public/" DEST="/var/www/html/" # Build hugo build \ --cleanDestinationDir \ --gc \ --minify # Deploy rclone sync \ --checksum \ --config "" \ --no-update-dir-modtime \ --stats 0 \ --verbose \ "${SRC}" ":sftp,host=${SSH_HOST},port=${SSH_PORT},user=${SSH_USER},known_hosts_file=~/.ssh/known_hosts:${DEST}"Set
SRCto the path of yourpublicdirectory. SetDESTto the path of the document root on your web server. A path that does not begin with a slash is relative to your home directory, which is common with shared hosting. - Step 2
- Make the script executable.
chmod +x deploy.sh - Step 3
- Run the script to build and deploy your site.
./deploy.shTo preview changes without modifying anything on your web server, add the
--dry-runflag to therclone synccommand.
Command-line flags
The hugo build command uses these flags:
--cleanDestinationDir- Removes orphaned files from the
publicdirectory. See configure build. --gc- Removes unused files from the file cache. See configure file caches.
--minify- Minifies the output. See configure minify.
The rclone sync command deletes files on your web server that do not exist in the local public directory, and uses these flags:
--checksum- Compares files by checksum instead of by modification time and size. To compare checksums, Rclone runs commands such as
md5sumon your web server. If your SSH user cannot run shell commands, Rclone compares files by size only. --config ""- Ignores the Rclone configuration file. The script defines the connection with an inline connection string instead.
--no-update-dir-modtime- Skips setting directory modification times on your web server.
--stats 0- Disables periodic transfer statistics.
--verbose- Lists each file that is transferred or deleted.
Related resources
For more information on deploying your site with Rclone, consult the official documentation:
