Deploy scripts with rsync (patterns and traps)
deployrsyncgit-archivebashversion-stamp
Most of Justin's sites deploy with a small deploy.sh that builds locally and rsyncs to the
server docroot over SSH. The pattern is simple, but the details (what gets deleted, what gets
shipped, permissions, labels) decide whether a deploy is safe. These are the variants that
worked and the traps they guard against.
Building blocks
rsync -az --delete local/ server:/var/www/html/site/for a directory the deploy fully owns.- Exclude server-only files from
--delete: secrets (config.php,config.local.php,s3_conf.php,mysql_conf.php,jwt_config.php),vendor/when composer runs on the server,.well-known/(certbot), andcomposer.lockif the server resolves its own. - No
--deletewhen the docroot holds things the repo does not (other apps, server-only files, legacy dirs). justinahampton.com and hampton.website deploy without--deleteso server-only content is never removed; stale build bundles are swept explicitly instead. - Scoped rsync for partial deploys while the working tree has unrelated uncommitted work:
rsync -a --relative public_html/./fun/index.html public_html/./fun/x server:/docroot/. The/./marks where the relative path starts. - Finish with
restorecon -Ron the target (see SELinux file contexts and booleans on web servers) and a curl check of a known URL returning 200.
Ship committed state, not the working tree
git archive HEAD | tar -x -C "$STAGE" then rsync the stage. Uncommitted changes can never ship
by accident, which matters when experiments live in the same tree. hampton.website's API and the
standalone Forge API deploy this way. The web build can instead refuse to run if source files
are dirty (git diff --quiet -- src angular.json package*.json || exit 1).
Version stamps
Write {"commit": "<sha>", "deployed": "<date>"} to version.json in each target during
deploy. Then curl https://site/version.json answers "what is live" in one request. Webpack
style content-hash filenames are also build fingerprints: identical hash means identical
content.
Staging-directory permission trap
mktemp -d creates the directory with mode 700. rsync with -a copies that mode onto the
served directory, Apache (a different user) can no longer traverse it, and every URL returns
403. Fix: chmod 755 "$STAGE" right after mktemp. If a deploy suddenly 403s everything, check
ls -ld on the docroot.
Batching many server changes
When one change touches many files on the server (several vhosts, configs, scripts), send them
in one transfer, not one scp per file:
- Stage everything in a single local directory that mirrors the destination layout, then send it
with one rsync -a stage/ server:/tmp/stage/ and install from there in one SSH session
(sudo cp, restorecon, reload). On 2026-10-30, while moving six sites to new hostnames, a
run of back-to-back scp calls stalled the SSH connection for about 15 minutes; one rsync of
the same files went straight through. Each scp is a fresh SSH handshake, and a burst of them
can trip connection rate limits or simply hang on a 1-vCPU server.
- Back up what you are about to replace first (cp -a /etc/httpd/conf.d /etc/nginx/conf.d into a
dated directory such as /root/vhost-backup-YYYYMMDD/), so a bad batch is one copy away from
undone.
- Gate every reload on both config tests: apachectl configtest && nginx -t && systemctl reload
httpd nginx. Read the warnings nginx -t prints even when it succeeds: "conflicting server
name ... ignored" means two files claim the same host and the first one loaded keeps winning
(see nginx in front of Apache (TLS proxy on :8008)).
Pre-deploy checks
- Run the project's tests or smoke script first and abort on failure (plumb runs
tests/smoke.sh). - With no local PHP, lint on the server:
ssh server "php -l" < api/public/index.php. - Delete temporary test pages (e.g. a
test-smoke.htmlharness) before deploying, since rsync ships the whole directory.
Lessons from Run's deploy.sh (Flutter web + APK + PHP API)
- Never hot-fix files on the server. Run API changes once made with
scpdirectly on the server (ghost upload support, preference endpoints, leaderboard fields) were silently wiped by the nextdeploy.sh, which rsyncs the localapi/over the server copy. Score submission broke. Edit locally, commit, deploy. - Run checks before bumping the version. Run's script originally committed the version
bump before analyze/build and never ran tests, so a failed build left a bumped, broken
version. The order now is analyzer +
flutter test(+php -lwhen available) -> bump -> build -> deploy. rsync --deletestraight into the live directory has no rollback. See Release-directory deploys with an atomic symlink swap for the release-directory alternative.- Generated changelogs inherit your commit messages. Run's deploy builds the user-facing changelog from commit subjects since the last "Bump version" commit, so write those subjects for users.
- Deploying files is not running migrations. The script rsyncs
api/migrations/but the SQL must be applied by hand (see Nightly MySQL/MariaDB backups with mysqldump and cron). - Split APKs by ABI: a fat 3-ABI Flutter APK was 90 MB (about 66 MB native libraries);
--target-platform android-arm,android-arm64(or--split-per-abi) cuts it sharply. See Flutter Android release builds, app size, and store rollout. - Verify live afterward:
curl https://site/version.jsonand one API GET returning 200.
Building legacy front ends
Old webpack 4 toolchains (Angular 11 here) fail on Node 17 and later with OpenSSL errors. Build
with NODE_OPTIONS=--openssl-legacy-provider npx ng build --prod; Angular 11 built fine on
Node 22 that way. Sweep stale hashed bundles from the target explicitly when deploying without
--delete.
Retiring an old Flutter web app
When replacing a Flutter web app with anything else, ship a self-destructing
flutter_service_worker.js at the same URL that unregisters itself and clears caches.
Otherwise returning visitors keep getting the old app from the service worker cache.
See also
- nginx in front of Apache (TLS proxy on :8008)
- SELinux file contexts and booleans on web servers
- Apache vhost gotchas (icons alias, manifests, SPA fallback, redirects)
Linked from
- Apache vhost gotchas (icons alias, manifests, SPA fallback, redirects)
- Flutter Android release builds, app size, and store rollout
- Flutter web (renderers, SEO, performance, when not to use it)
- Heredoc, stdin and scripted-edit pitfalls
- Hosting one static site per subdomain with a wildcard vhost
- justinahampton.com and its subdomains
- Keeping a web app and an Android app identical without a framework
- Moving a website to a new server without downtime
- Release-directory deploys with an atomic symlink swap
- Run (runrunrunrun.run), Justin's speedrun platformer
- SELinux file contexts and booleans on web servers