The first deployment of most projects is a person on SSH running git pull, composer install and a few more commands, then crossing their fingers. It works until the day a command fails halfway, the site is broken for ten minutes, and nobody can say what state the server is in.

A VPS does not need Kubernetes to deploy safely. It needs a release layout, a handful of habits and one script. This article describes a pattern for a Laravel API and a Next.js front end on one Ubuntu server, with Nginx in front: each deploy is a new folder, one atomic switch makes it live, and rolling back is the same switch pointed the other way.

The layout

/srv/app
  releases/
    20261022-1400/        <- a complete copy of the code, built
    20261022-1730/
  shared/
    .env                  <- secrets and settings, never in git
    storage/              <- uploads, logs, cache: survive every release
  current -> releases/20261022-1730

Nginx and the services always point at /srv/app/current. A deploy builds a new folder next to the old one, and only when it is complete and checked does current move. Until that moment, visitors are served by the old release, untouched.

Deploying the Laravel API

For each release, in order:

  1. Put the code in a new folder, from CI's artefact or git archive of the commit you are releasing. Never edit files in a live release.
  2. Install dependencies: composer install --no-dev --optimize-autoloader.
  3. Link what is shared: the release's .env and storage become links to /srv/app/shared.
  4. Run the migrations: php artisan migrate --force. (More on making this safe below.)
  5. Warm the caches: php artisan config:cache, route:cache, view:cache, event:cache.
  6. Switch atomically. Create a temporary link and rename it over current, because a rename is a single operation and a plain ln -sfn on an existing link is not:
ln -s /srv/app/releases/20261022-1730 /srv/app/current.tmp
mv -T /srv/app/current.tmp /srv/app/current
  1. Reload PHP-FPM and restart the workers: systemctl reload php8.4-fpm (use your version) and php artisan queue:restart. The reload clears the opcode cache so the new code is what runs, and queue workers, which are long-lived processes, pick up the new release when they restart.
  2. Check health: request a health route and fail the deploy if it is not a clean 200.

One Nginx detail avoids a classic bug in which PHP keeps running the old files after the switch. Use the resolved path:

root /srv/app/current/public;

location ~ \.php$ {
    include fastcgi_params;
    fastcgi_pass unix:/run/php/php8.4-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    fastcgi_param DOCUMENT_ROOT   $realpath_root;
}

Deploying the Next.js front end

Next.js is a long-running Node process, so the switch has one more step. A simple pattern that keeps the site up is two ports:

  1. Build the new release (npm ci, npm run build) in its folder.
  2. Start it on the port that is not serving traffic, for example 3002 while 3001 is live. A systemd template unit makes this tidy: site@3001.service and site@3002.service run from /srv/app/current.
  3. Wait until it answers a health request.
  4. Point Nginx's upstream at the new port and reload Nginx. nginx -s reload is graceful: it finishes requests in flight on the old workers.
  5. Stop the old instance.
upstream site {
    include /etc/nginx/upstream-site.conf;   # holds one line: server 127.0.0.1:3002;
}

The deploy script rewrites that one line, runs nginx -t, and reloads. If the new instance never becomes healthy, the script stops before touching Nginx, and the old site never noticed.

Set the API address and other build-time values in the release's environment, since a Next.js build can bake some of them into the output.

Migrations that cannot hurt

During a deploy there is a moment when the old code is still running against the new database. A migration that renames or drops a column breaks the old code in that moment. The fix is to split risky changes across releases, a pattern often called expand and contract:

Release Change
1: expand Add the new column (nullable), and write to both old and new
2: migrate Backfill old rows; switch reads to the new column
3: contract Remove the old column, once nothing uses it

For every migration, ask three questions: does the old code still work after it? How long will it lock the table on production-sized data? And what do we do if it fails halfway? Run it first on a copy of real data, and take a database backup before every release that has one.

Rolling back

Rollback is the part people never rehearse. With this layout it is short:

ln -s /srv/app/releases/20261022-1400 /srv/app/current.tmp
mv -T /srv/app/current.tmp /srv/app/current
systemctl reload php8.4-fpm
php artisan queue:restart

For Next.js, start the previous release on the idle port and switch the upstream back. Keep the last five releases on disk to make this possible.

The caveat is the database. Code rolls back in seconds, but a migration that has already run may not. That is the reason for expand and contract: if the database is always compatible with the release before it, rolling back code is always safe.

Habits that make it boring

  • Deploy from CI, not from a laptop. Run the tests, then deploy exactly the commit that passed.
  • Use a deploy user with only the rights it needs, and a key that can run only the deploy command.
  • One deploy at a time. A lock file stops two releases from overlapping.
  • Keep secrets out of the repository. .env lives in shared, with strict permissions.
  • Log every deploy: who, which commit, when, and whether it passed.
  • Back up the database and test a restore. A backup that was never restored is a hope.
  • Monitor after the switch. Watch errors for the first minutes, and make rollback one command.

What it does not need

You do not need containers, an orchestrator or a pipeline with twelve stages to deploy without downtime on a single server. You need releases in folders, an atomic switch, migrations that are safe to run early, and a script you trust because you have used it a hundred times. Add complexity when a real problem demands it.

If your product still deploys with git pull and prayer, or you are about to launch one and want the release process set up properly from the start, talk to us. Deployment and QA are two of the six disciplines BENAA works in.