Guide PHP & Development

How to deploy your website with rsync

The rsync flags that matter, a dry run before the real one, and how to stop it overwriting files it should have left alone.

Updated 7 min read Intermediate

rsync copies files efficiently by transferring only what has actually changed between the source and the destination, rather than re-sending everything on every deploy. For a website of any real size, that difference in behaviour is what makes rsync a genuinely practical deployment tool rather than something that just happens to move files — a small text change to one file does not mean re-uploading the whole site.

Before anything else: SSH access

rsync's usual mode for deploying to hosting runs over SSH, so a working key-based connection needs to exist first — see setting up SSH keys if you have not done this yet. Running rsync over a passwordless key connection also means it can run unattended from a script or a CI pipeline, without anyone typing a password into it.

A basic deploy command

rsync -avz --delete -e ssh ./local-site/ youraccount@yourserver.example.com:~/public_html/

Read the flags individually, because each does something distinct:

FlagDoes
-aArchive mode — preserves permissions, timestamps and symbolic links, and recurses into subdirectories. The flag you almost always want as a baseline.
-vVerbose — lists the files being transferred, useful for confirming what actually changed.
-zCompresses data during transfer, which speeds things up over a slower connection.
--deleteRemoves files on the destination that no longer exist in the source. Powerful and genuinely dangerous if the source path is wrong — see the warning below.
-e sshTells rsync to transfer over SSH rather than an older, unencrypted rsync daemon protocol.
The trailing slash on the source path changes the result

local-site/ with a trailing slash copies the contents of the folder into the destination. local-site without one copies the folder itself, nesting everything one level deeper than you probably intended. This single character is the most common rsync mistake, and it is worth double-checking every time rather than trusting habit.

Always dry-run a command that includes --delete

Add --dry-run (or the shorter -n) to see exactly what rsync would do without actually doing it:

rsync -avzn --delete -e ssh ./local-site/ youraccount@yourserver.example.com:~/public_html/

Read the output carefully, particularly any line prefixed deleting, before removing the flag and running the real transfer. A misconfigured source path combined with --delete can wipe out live files that exist only on the server, and a dry run is the one habit that reliably catches that before it happens rather than after.

Excluding files that should never be overwritten

A deploy should almost never overwrite .env, uploaded media, or anything else that only exists correctly on the live server. Exclude those paths explicitly:

rsync -avz --delete -e ssh \
  --exclude '.env' \
  --exclude 'storage/' \
  --exclude 'uploads/' \
  ./local-site/ youraccount@yourserver.example.com:~/public_html/

For a longer list, keep the exclusions in a file instead of a growing command line:

rsync -avz --delete -e ssh --exclude-from='deploy-excludes.txt' ./local-site/ youraccount@yourserver.example.com:~/public_html/

Deploying in the other direction

The same command works in reverse to pull a copy of the live site down — useful for setting up a staging environment from what is actually running in production rather than an out-of-date local copy:

rsync -avz -e ssh youraccount@yourserver.example.com:~/public_html/ ./local-copy/

Source and destination simply swap places; every other flag behaves the same way.

Common problems and what they mean

  • "Permission denied" partway through a transfer. The connecting account does not have write access to that specific file or folder on the server — check ownership, or that you are connecting as the correct account rather than a different one on the same server.
  • rsync: command not found, on the destination. A small number of restricted hosting environments do not include rsync on the server side, even where SSH itself is available. If this happens consistently, ask your host whether it can be added, or fall back to deploying from Git instead.
  • Files appear to transfer but nothing changes on the site. Check the destination path is actually the live document root and not an old or duplicate folder left over from a previous setup — a subtly wrong path is the most common cause of an rsync run that reports success but changes nothing visible.
  • The transfer is far slower than expected. Confirm -z is present for compression, and check whether a very large number of small files, rather than total data size, is the bottleneck — rsync's per-file overhead adds up on a directory with tens of thousands of entries even when the total size is modest.

Turning this into a repeatable deploy script

Wrapping the command in a short shell script, with the exclusions and destination path written once rather than retyped, removes the two most common mistakes — a forgotten exclusion and a mistyped path — from every future deploy:

#!/bin/sh
rsync -avz --delete -e ssh \
  --exclude-from='deploy-excludes.txt' \
  ./local-site/ youraccount@yourserver.example.com:~/public_html/

Run that script the same way every time, and a deploy stops being something you have to remember the exact flags for.

Related reading