Back to blog
Guides

Git push to deploy for PHP sites on your own server

Still dragging files over FTP? A Git-based deploy makes every release a known commit, repeatable and reversible. Here's how to set up deploy keys, verified webhooks, Composer installs, atomic releases and rollbacks on your own server, plus the traps that bite first.

S
ShadowPanel Team
9 min read

Most PHP sites still get deployed the way they did in 2008: someone opens an FTP client, drags a folder across, and hopes they didn't forget a file. It works until it doesn't. A half-uploaded vendor/ directory takes the site down for three minutes, a hotfix made directly on the server gets overwritten next week, and nobody can say with confidence what code is actually live. Deploying from Git fixes all of that. Every release becomes a known commit, the process is the same every time, and going back is a matter of pointing at an older commit. Here's how to build it properly on your own server.

Why deploy from Git at all

  • You always know what's live. "Production runs commit a3f9c21 on main" is a sentence you can actually say.
  • Repeatable. The same script runs every time. No forgotten files, no "it worked when I uploaded it".
  • Reversible. A bad release is undone by going back to a commit that worked.
  • Nobody needs production SFTP. Developers push to the repository; the server pulls. One fewer set of credentials floating around.

Step 1: a read-only deploy key per site

The server needs to read your private repository, and nothing more. Don't use your personal SSH key for that, and don't use a personal access token with write access. Generate a dedicated key as the user the site runs under:

sudo -u siteuser ssh-keygen -t ed25519 -C "deploy@example.com" \
  -f /var/www/siteuser/.ssh/deploy_ed25519 -N ""

Add the .pub file to the repository as a read-only deploy key (GitHub: Settings, Deploy keys; GitLab: Settings, Repository, Deploy keys). If the server is ever compromised, the attacker can read that one repository, but cannot push to it or touch any other project. GitHub won't even let you attach the same deploy key to two repositories, which nudges you toward the right habit: one key per site.

Then tell SSH to use it, in ~siteuser/.ssh/config:

Host github.com
  HostName ssh.github.com
  Port 443
  IdentityFile ~/.ssh/deploy_ed25519
  IdentitiesOnly yes

That ssh.github.com on port 443 line is a small trick worth knowing: some hosting providers and corporate networks block outbound port 22, and the result is an opaque timeout that looks exactly like a broken key. GitHub serves the same SSH endpoint on 443 for this reason.

One more gotcha: a deploy key is an SSH credential. If you configure the remote as https://github.com/acme/site.git, the key is never used and you get "Repository not found" on a private repo. Use the git@github.com:acme/site.git form.

Step 2: keep the checkout out of the web root

The classic mistake is running git clone directly into public_html. Now https://example.com/.git/config is downloadable, and with a bit of tooling an attacker can reconstruct your entire source tree, including any secret that was ever committed. Keep the working copy outside the document root and copy or link the result in. If you really must have .git under the web root, block it at the web server level:

location ~ /\.git {
    deny all;
}

Step 3: the deploy script

The simplest useful deploy for a Laravel or Composer-based app:

#!/usr/bin/env bash
set -euo pipefail
cd /var/www/siteuser/app
git fetch --depth=1 origin main
git reset --hard FETCH_HEAD
composer install --no-dev --optimize-autoloader --no-interaction
php artisan migrate --force
php artisan config:cache
sudo systemctl reload php8.3-fpm

A few details matter here. composer install (never update) installs exactly what composer.lock says, so production matches what you tested. --no-dev keeps test tooling off the server. Reloading PHP-FPM at the end clears OPcache so the new code is actually served. And the whole thing runs as the site's user, not root.

The weakness of this version is the window in the middle: for a few seconds, the code is new but vendor/ is still being installed. For a small site that's acceptable. For anything busier, use atomic releases.

Step 4: atomic releases with a symlink switch

The pattern used by Capistrano, Deployer and Envoyer is simple: build each release in its own directory, then switch a symlink in one atomic operation.

/var/www/siteuser/
  releases/20261103090000/
  releases/20261103143000/
  shared/.env
  shared/storage/
  current -> releases/20261103143000
APP=/var/www/siteuser
REL=$APP/releases/$(date +%Y%m%d%H%M%S)
git clone --depth=1 -b main git@github.com:acme/site.git "$REL"
ln -s $APP/shared/.env "$REL/.env"
rm -rf "$REL/storage" && ln -s $APP/shared/storage "$REL/storage"
(cd "$REL" && composer install --no-dev --optimize-autoloader --no-interaction)
(cd "$REL" && php artisan migrate --force && php artisan config:cache)
ln -sfn "$REL" $APP/current.tmp && mv -Tf $APP/current.tmp $APP/current
sudo systemctl reload php8.3-fpm
ls -1dt $APP/releases/* | tail -n +6 | xargs -r rm -rf

The web server's root points at current/public. Everything slow happens in a directory nobody is serving yet, and mv -T swaps the link atomically. The last line keeps five releases around.

Two things people miss. First, anything that must survive between releases (the .env file, user uploads, logs, sessions) lives in shared/ and is linked in, otherwise every deploy wipes it. Second, PHP caches resolved paths: in your Nginx config, pass $realpath_root instead of $document_root to SCRIPT_FILENAME, and reload PHP-FPM after the switch, or some requests keep running the old release.

Step 5: rollbacks

With release directories, a rollback is the same symlink switch pointed at the previous release: instant. The catch is the database. Code rolls back; migrations don't. Write migrations in an "expand, then contract" style: add the new column in one release, start using it in the next, drop the old one later. Then any release can run against the current schema, and a rollback never meets a database it doesn't understand. And take a backup before any migration that destroys data (more on that in encrypted website backups to S3).

Step 6: push to deploy with a webhook

The last piece is triggering the deploy automatically when you push. GitHub, GitLab and most other hosts can POST to a URL on every push. Three rules for the receiving end:

  • Verify the sender. GitHub signs the body with HMAC-SHA256 in X-Hub-Signature-256; GitLab sends a shared token in X-Gitlab-Token. Compare in constant time and reject anything that doesn't match.
  • Never put the secret in the URL. URLs end up in access logs, proxy logs and monitoring tools. Secrets belong in headers.
  • Answer fast and deploy in the background. Respond 202 immediately, run the deploy asynchronously, and wrap it in a lock (flock /tmp/deploy-site.lock ./deploy.sh) so two quick pushes can't run two deploys on top of each other.

The mistakes that bite first

  • .git inside the web root. Your source and history, downloadable.
  • .env committed to the repository. Rotate every secret in it, then add it to .gitignore. It belongs on the server only.
  • Deploying as root. Files end up owned by root, PHP can't write its cache, and someone "fixes" it with chmod 777.
  • composer update on production. You deploy dependency versions nobody tested.
  • A sync that deletes user uploads. rsync --delete or git clean happily removes uploads/ if it isn't in the repository. Keep runtime data outside the deployed tree.
  • Deploying straight to production. Push to a staging copy first; see setting up a staging environment.

How ShadowPanel handles Git deploys

ShadowPanel, a self-hosted hosting control panel, includes Git Deploy on the Pro and Agency plans. Here's exactly what it does, and what it doesn't:

  • A deploy key per site, generated for you. The panel creates an ed25519 key as the site's own Linux user and shows you the public half to paste into GitHub or GitLab as a read-only deploy key.
  • The common connection traps handled. GitHub is reached over ssh.github.com:443 automatically, and a private repository entered as an https:// URL is rewritten to the SSH form and retried, instead of failing with "Repository not found".
  • Checkout outside the web root. The repository is shallow-cloned into a repo/ directory next to public_html, owned by the site user. Each deploy fetches the configured branch, checks it out, and syncs it into public_html, leaving .git, .env and node_modules out.
  • Deploy button or push-to-deploy. Trigger deploys manually, or switch on auto-deploy and get a webhook whose secret is checked from a header (GitHub's HMAC signature, GitLab's token, or a bearer header for other senders), never from the URL. The last deploy log is kept in the panel.

What it is not is a release-directory system. There is no Composer or build step and no one-click rollback: the sync mirrors your repository, removing files in public_html that aren't in it (except .env, .git and node_modules), so keep uploads and other runtime data outside the synced tree. Roll back with git revert and push. If you need vendor/ or built assets, build them in CI and push the result to a deploy branch the panel tracks. Pair it with the panel's backups and staging, and you have a clean, auditable pipeline without writing any of the plumbing yourself.

The bottom line

Git-based deploys turn releases from a ritual into a process: a read-only deploy key, a checkout outside the web root, a script that runs the same way every time, and a verified webhook to trigger it. Add release directories and a symlink switch when downtime and instant rollbacks start to matter. ShadowPanel gives every site the key, the checkout, the sync and the secure webhook out of the box, so pushing to main is all a deploy takes.

curl -fsSL https://shadowpanel.de/install.sh | bash

See pricing and download →

We use cookies

We use cookies to improve your experience and remember your preferences. You can manage your choices below. Learn more.

ShadowPanel

Manage your cookie preferences below. Essential cookies are always active as they are required for the website to function.

Essential

Required for the site to work. These include session authentication and CSRF protection. Cannot be disabled.

Always on

Functional

Remembers your language preference so you do not need to reselect it on every visit.

Analytics

Helps us understand how visitors use the site so we can improve it. No personal data is sold.

Marketing

Used by Google Ads to measure ad conversions and improve campaign targeting. No personal data is sold.