Deploy PHP

A deploy is a sequence of small, boring steps that can be repeated, and the page keeps them in that order. The two PHP programs below were run here; the two commands that belong to a whole project keep the output the page had, because this machine has no project and no database to deploy.

Deploy checklist

Calm deploys follow a written list: freeze merges, run tests and static analysis, tag the release, copy files, clear caches, smoke test the front page. Ticking boxes in order beats heroic memory every single release.

php tests/check.php

OK: all green, ready to tag

Env files

Secrets and host names live in env files, never in git. Each server keeps its own copy with database logins and keys, while the code reads values by name and falls back to safe local defaults.

<?php file_put_contents(__DIR__ . "/app.env", "DB_HOST=db.internal\nDB_NAME=shop\n"); $env = parse_ini_file(__DIR__ . "/app.env"); $dbHost = $env["DB_HOST"] ?? "localhost"; echo $dbHost . "\n";

php env.php

db.internal

The file holds the two values, and the program reads one of them by name. A missing key falls back to localhost, which keeps a fresh checkout working without a copy of the production file.

Run migrations

Schema changes ship as numbered migration scripts that run once in order, with each step adding a table or column inside a transaction. Back up the database first, then run the up command and verify the version marker moved.

php migrate.php --up

In the project this page was written for, that command answers migrated to version 14. The line is quoted here rather than in an output block, because the script and its database live in that project and this machine has neither.

Rollback plan

Every release keeps the previous folder one symlink away, so a bad deploy heals in seconds by pointing the link back. Practice the rollback on staging monthly so the night it matters feels routine.

<?php function rollbackTo(string $release): void { echo "restore " . $release . "\n"; } rollbackTo("release-41");

php rollback.php

restore release-41

The call names the folder to point the link at, which is the whole rollback when every release is its own directory. The function is a sketch of the step the deployment script performs.

Exercises

  1. Add a second key to the env file and print it with the same fallback.
  2. Delete the env file, run the program again and read the value it falls back to.
  3. Add a rollbackTo call with the previous release and read the line.
  4. Write the checklist as a shell script that stops at the first failing command.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Why do secrets belong in an env file and not in the repository?

    Answer

    Because everyone who clones the repository would hold the key

    A committed secret is copied to every clone and every backup, and removing it from history is hard.

  2. How does the env example keep a fresh checkout working?

    Answer

    It falls back to a local default when the key is missing

    The null coalescing operator gives localhost, so a missing file still yields a usable value.

  3. What makes a migration safe to run once?

    Answer

    It records the version it applied

    The version marker tells the runner which steps are done, so the next run starts after it.

  4. How does a symlink rollback restore the previous release?

    Answer

    It points the link at the previous folder

    Both folders are still on disk, so the switch is one link change and takes a moment.

  5. What does this program print, run in an empty folder? <?php file_put_contents(__DIR__ . "/app.env", "DB_HOST=db.internal\n"); $env = parse_ini_file(__DIR__ . "/app.env"); $dbHost = $env["DB_HOST"] ?? "localhost"; echo $dbHost;

    Answer

    db.internal

    The file is written first, and the key sits in it, so the fallback is not used.

  6. What does this program print? <?php $env = []; $dbHost = $env["DB_HOST"] ?? "localhost"; echo $dbHost;

    Answer

    localhost

    The array has no such key, so the null coalescing operator gives the default without a warning.

  7. What does this program print? <?php function rollbackTo(string $release): void { echo "restore " . $release . "\n"; } rollbackTo("release-41");

    Answer

    restore release-41

    The function prefixes the word and prints the whole line.

  8. What does this program print? <?php function rollbackTo(string $release): void { echo "restore " . $release . "\n"; } rollbackTo("release-41"); echo "done\n";

    Answer

    restore release-41 / done

    The call prints its line, then the script continues to the next statement.

  9. Why is a written checklist better than a remembered one?

    Answer

    A repeated step is easy to forget on a busy day, and the list does not forget

    The list is the same evidence every release, so a skipped step is visible before the users find it.

  10. What must happen before the up migration on a production database?

    Answer

    A backup of the database

    A schema change is hard to undo, and the backup is the only way back if the step goes wrong.

The examples call parse_ini_file(). The programs on this page were run with PHP 8.5.13 (php -v) with the commands shown above. The commands that run the test suite and the migrations belong to a project and a database this machine does not have, so their output is kept as written and not run here.

Article author: Arthur Isaev