Zero-downtime WordPress deployment with rsync, SSH and a symlink
Uploading a new theme version file by file means that for a few seconds the server has a mix of old and new files. A request in that moment loads a new template with an old functions file and ends in a fatal error. The fix is old and simple: upload the new version into a separate folder, then switch a symlink. The switch is atomic, every request sees either the old or the new version.
Problem: A theme is deployed with rsync directly into wp-content/themes/ha-theme. During the 20-second transfer, some visitors get white pages, and a broken deploy can only be undone by uploading the old version again.
Solution: Keep releases in releases/<timestamp> folders on the server, copy the previous release with hard links and sync the new build into it with rsync --link-dest, make wp-content/themes/ha-theme a symlink to the current release, switch it atomically with ln -sfn and mv -T, reset OPcache, and keep the last five releases for an instant rollback.
The layout on the server
/var/www/example.com/
├── public/ WordPress
│ └── wp-content/themes/ha-theme -> /var/www/example.com/theme/current
└── theme/
├── current -> releases/20250205-1412
└── releases/
├── 20250201-0930/
└── 20250205-1412/
WordPress only sees the symlink ha-theme. The uploads, plugins and the database are not part of the release; this is only for code that is built and versioned in git.
The deploy script
#!/usr/bin/env bash
# deploy.sh, runs on the developer machine or in CI
set -euo pipefail
HOST="deploy@example.com"
BASE="/var/www/example.com/theme"
RELEASE="$(date -u +%Y%m%d-%H%M%S)"
npm ci && npm run build # build assets locally, not on the server
ssh "$HOST" "mkdir -p $BASE/releases/$RELEASE"
rsync -az --delete \
--exclude-from=.deployignore \
--link-dest="$BASE/current/" \
./ "$HOST:$BASE/releases/$RELEASE/"
ssh "$HOST" bash -s <<EOF
set -euo pipefail
cd "$BASE"
ln -sfn "releases/$RELEASE" current.tmp
mv -T current.tmp current # atomic rename of the symlink
ls -1dt releases/* | tail -n +6 | xargs -r rm -rf # keep five releases
EOF
ssh "$HOST" "sudo systemctl reload php8.3-fpm"
--link-destcompares with the current release: unchanged files are hard-linked instead of transferred, so a release takes only the space and time of the changed files.ln -sfnon an existing symlink is two steps (delete, create) and not atomic. Creating a temporary link and renaming it withmv -Tis a singlerename()system call: there is no moment without a link..deployignorelists what does not belong on the server:node_modules/,.git/,src/, tests.
OPcache and the realpath cache
PHP caches compiled files and resolved paths. After the switch, it may keep running the old files for a while, because the path wp-content/themes/ha-theme/functions.php did not change. A reload of PHP-FPM clears OPcache and the realpath cache gracefully: running requests finish, new ones get fresh workers. On shared hosting without that option, a small script called over HTTP with opcache_reset() does the OPcache part, but the realpath cache stays until it expires (realpath_cache_ttl, 120 seconds by default).
Rollback
ssh deploy@example.com 'cd /var/www/example.com/theme && ln -sfn "$(ls -1dt releases/* | sed -n 2p)" current.tmp && mv -T current.tmp current && sudo systemctl reload php8.3-fpm'
The previous release is still on disk; switching back takes a second. Database changes are the exception: a release that migrated data cannot be rolled back with a symlink, so migrations should be backward compatible for one release.
NOTE: The same pattern works for a custom plugin. For a whole site with core, plugins and uploads, keep uploads in a shared folder outside the releases and symlink it into each one. Backups before a deploy: automate WordPress backups with a Bash script. The same steps in a CI pipeline: GitHub Actions CI/CD for WordPress plugins.