What happened

On a production PHP site (Symfony-based), I ran composer update through our deploy script as the deploy account (ec2-user). The package replacement itself succeeded — yet the script judged the run a "failure" and kicked off a rollback. The rollback then stopped halfway, leaving the site stuck at 500. Running it again gave the same result.

When I looked inside vendor, my heart sank: there was a directory called vendor/vendor. The rollback had failed to fully delete vendor, then copied the backup into what was left of it.

The cause

This server had a very common mixed-ownership layout:

  • The web server (apache) owns var/ and vendor/ — it needs to write caches and logs at runtime
  • The deploy account (ec2-user) owns composer.json / composer.lock

Here's what happens when you run composer update as ec2-user in that setup.

1. The failure wasn't composer itself — it was the post-update script. The package swap succeeded. Then cache:clear in post-update-cmd (auto-scripts) tripped over write permissions on var/log and exited 1. But there's only one exit code, so from the script's point of view, "composer update failed."

2. The rollback's rm -rf vendor stopped partway. Deleting apache-owned directories as ec2-user hits Permission denied. vendor was left half-deleted.

3. cp -a backup/vendor vendor created the nesting. Since the destination vendor still existed, cp did exactly what its semantics say: it created vendor/vendor. The autoloader stayed broken; the site stayed at 500.

Worse, the backup itself had been taken as ec2-user, so the ownership information had already been rewritten. "We have a backup, so we're safe" only holds when the account doing the restore matches the ownership of what's being restored.

The fix

I restructured the procedure so that each step explicitly names the account that runs it.

# 1. Package replacement only (deploy account, no scripts) composer update --no-scripts # 2. Run the auto-scripts manually as apache, which has write permission sudo -u apache php bin/console cache:clear --no-warmup sudo -u apache php bin/console cache:warmup --no-optional-warmers sudo -u apache php bin/console assets:install --symlink --relative html # 3. Give apache write access back on the freshly installed vendor chgrp -R apache vendor && chmod -R g+w vendor 

Backup and rollback now run entirely as root, sidestepping the ownership problem at the source — passed as a heredoc to sudo bash -s:

sudo bash -s <<'EOS' set -e rm -rf /path/to/app/vendor && cp -a /path/to/backup/vendor /path/to/app/vendor EOS 

The key point is chaining rm -rf and cp -a with && into a single flow. If the delete fails, the copy never runs — so the "copy into the wreckage" intermediate state from this incident can't occur.

One small extra: this was a tiny instance with 446MB of free memory, so I added a line to systemctl stop php-fpm during the update, freeing about 300MB. Composer is memory-hungry; on small servers this matters.

On the third attempt, I turned the maintenance page on first and ran the new procedure: 61 seconds to recovery, with all seven key pages returning 200.

Takeaways

  • Operational change scripts should state, per step, which account runs it. On a mixed-ownership server, leaving that implicit is how permission errors get baked in
  • composer update's exit code can't distinguish a composer failure from a post-script failure. Separating them with --no-scripts makes the failure point obvious
  • Exercise your rollback path before production needs it. In this incident, the rollback was only truly "verified" by failing
  • A backup merely existing isn't enough. It's only safe once you've confirmed the restoring account can bring back the destination's ownership along with the files

A recovery mechanism I trusted — "we have a backup" — turned out to only half-work because of one ownership mismatch. The smaller the production server, the more these structural quirks come back as real outages.