<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Luiz Schons – Twelve factors in practice</title><link>https://luizschons.com/series/twelve-factors-in-practice/</link><description>Recent content in Twelve factors in practice on Luiz Schons</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Mon, 30 Nov 2026 00:00:00 -0300</lastBuildDate><atom:link href="https://luizschons.com/series/twelve-factors-in-practice/index.xml" rel="self" type="application/rss+xml"/><item><title>12 factors: one codebase, many deploys</title><link>https://luizschons.com/posts/twelve-factor-codebase/</link><pubDate>Tue, 17 Nov 2026 00:00:00 -0300</pubDate><guid>https://luizschons.com/posts/twelve-factor-codebase/</guid><description>
&lt;p&gt;Almost every project goes through this moment: it works on the machine of whoever wrote it, but breaks on the server. Sometimes it&amp;rsquo;s a password that only exists in a forgotten file, sometimes it&amp;rsquo;s a file saved in a folder that disappears on the next deploy, sometimes it&amp;rsquo;s a fix someone made straight in production that nobody can find.&lt;/p&gt;
&lt;p&gt;The &lt;a href="https://12factor.net/" target="_blank" rel="noopener"&gt;Twelve-Factor App&lt;/a&gt; is a list of 12 practices to avoid exactly these problems. It was written by engineers at &lt;a href="https://www.heroku.com/" target="_blank" rel="noopener"&gt;Heroku&lt;/a&gt;, a platform that hosts applications in the cloud, based on what they saw across hundreds of thousands of apps running there. The text is short, more than ten years old, and still current, because it describes problems everyone still has.&lt;/p&gt;
&lt;p&gt;This is the first article in the &lt;strong&gt;Twelve factors in practice&lt;/strong&gt; series. The idea is simple: one article per factor, always with plain PHP examples and, when a framework helps, &lt;a href="https://hyperf.wiki/" target="_blank" rel="noopener"&gt;Hyperf&lt;/a&gt;, a PHP framework that runs on &lt;a href="https://wiki.swoole.com/en/" target="_blank" rel="noopener"&gt;Swoole&lt;/a&gt;, an extension that turns PHP into an always-on server. In this first one, I give a quick introduction to the methodology and then dive into factor I, &lt;strong&gt;codebase&lt;/strong&gt;.&lt;/p&gt;
&lt;h2 data-hextra-search-id="before-we-start-three-words"&gt;Before we start: three words&lt;span class="hx:absolute hx:-mt-20" id="before-we-start-three-words"&gt;&lt;/span&gt;
&lt;a href="#before-we-start-three-words" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Deploy:&lt;/strong&gt; an installation of the app running somewhere. Production is a deploy, staging is another, your machine is another.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Environment:&lt;/strong&gt; the place where a deploy runs, with its own passwords, addresses and settings.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Repository:&lt;/strong&gt; the place where the code and its whole history of changes are stored, like a project in &lt;a href="https://git-scm.com/" target="_blank" rel="noopener"&gt;Git&lt;/a&gt;, the most widely used version control system today.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 data-hextra-search-id="the-core-idea-of-the-12-factors"&gt;The core idea of the 12 factors&lt;span class="hx:absolute hx:-mt-20" id="the-core-idea-of-the-12-factors"&gt;&lt;/span&gt;
&lt;a href="#the-core-idea-of-the-12-factors" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;The 12 factors revolve around one separation: &lt;strong&gt;the code is the same everywhere, and what changes between environments lives outside it&lt;/strong&gt;. If the same code runs on your machine, in staging and in production, with only the configuration changing, it becomes easy to start one more copy, switch servers or bring someone new onto the project.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://luizschons.com/images/posts/twelve-factor-codebase/overview-en.svg" alt="Diagram: a single Git repository produces one build. The same build becomes three deploys (local, staging and production), each with its own config in environment variables and its own backing services, such as a database and a queue" loading="lazy" /&gt;&lt;/p&gt;
&lt;h2 data-hextra-search-id="the-series-map"&gt;The series map&lt;span class="hx:absolute hx:-mt-20" id="the-series-map"&gt;&lt;/span&gt;
&lt;a href="#the-series-map" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;Each row below will become an article. For now, one sentence per factor is enough:&lt;/p&gt;
&lt;div class="article-table-wrap"&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Factor&lt;/th&gt;
&lt;th&gt;In one sentence&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;I. Codebase&lt;/td&gt;
&lt;td&gt;One repository per app, many deploys&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;II. Dependencies&lt;/td&gt;
&lt;td&gt;Everything the app needs is declared, nothing is assumed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;III. Config&lt;/td&gt;
&lt;td&gt;What changes between environments lives in the environment, not in the code&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IV. Backing services&lt;/td&gt;
&lt;td&gt;Database, queue and cache are addresses you can swap&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;V. Build, release, run&lt;/td&gt;
&lt;td&gt;Build, release and run are separate stages&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;VI. Processes&lt;/td&gt;
&lt;td&gt;The app runs as processes that keep no state&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;VII. Port binding&lt;/td&gt;
&lt;td&gt;The app exposes itself by listening on a port&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;VIII. Concurrency&lt;/td&gt;
&lt;td&gt;More load is handled with more processes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IX. Disposability&lt;/td&gt;
&lt;td&gt;Starts fast and shuts down without losing work&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;X. Dev/prod parity&lt;/td&gt;
&lt;td&gt;Your machine looks like production&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;XI. Logs&lt;/td&gt;
&lt;td&gt;The app writes events to its output and doesn&amp;rsquo;t manage files&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;XII. Admin processes&lt;/td&gt;
&lt;td&gt;One-off tasks run with the same code and config&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;h2 data-hextra-search-id="factor-i-one-codebase-many-deploys"&gt;Factor I: one codebase, many deploys&lt;span class="hx:absolute hx:-mt-20" id="factor-i-one-codebase-many-deploys"&gt;&lt;/span&gt;
&lt;a href="#factor-i-one-codebase-many-deploys" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;The original text sums up the factor as: &lt;strong&gt;one codebase tracked in revision control, many deploys&lt;/strong&gt;. That&amp;rsquo;s two rules in one sentence:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Every app lives in a version control system&lt;/strong&gt;, such as Git. No code that only exists in a folder on the server or in a ZIP sent by email.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;There is a one-to-one relationship between app and codebase.&lt;/strong&gt; An app has one repository. A repository holds one app.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Then comes the part that sounds contradictory but isn&amp;rsquo;t: one codebase, &lt;strong&gt;many deploys&lt;/strong&gt;. Production, staging and every team member&amp;rsquo;s machine are deploys of the same repository. They can be on different versions at the same time, and that&amp;rsquo;s normal.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://luizschons.com/images/posts/twelve-factor-codebase/codebase-en.svg" alt="Two-sided diagram. On the left, the right way: a repository with versions v1.3, v1.4, v1.5 and main on a single line, and each deploy runs one of them (production on v1.4, staging on v1.5, local on main with changes). On the right, the problem: one copy of the system per client (shop-client-a, shop-client-b, shop-client-c), each with its own history, and a fix made in one never reaches the others" loading="lazy" /&gt;&lt;/p&gt;
&lt;h3 data-hextra-search-id="different-versions-are-not-different-codebases"&gt;Different versions are not different codebases&lt;span class="hx:absolute hx:-mt-20" id="different-versions-are-not-different-codebases"&gt;&lt;/span&gt;
&lt;a href="#different-versions-are-not-different-codebases" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;Picture an online shop built in PHP. Today:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;production runs the &lt;code&gt;v1.4&lt;/code&gt; tag;&lt;/li&gt;
&lt;li&gt;staging runs &lt;code&gt;v1.5&lt;/code&gt;, which the team is testing;&lt;/li&gt;
&lt;li&gt;your machine is on &lt;code&gt;main&lt;/code&gt;, with a half-finished feature.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Those are three different versions, but they all sit on the same timeline of the same repository. Any of them can be reproduced from a commit. That&amp;rsquo;s what makes the three environments &amp;ldquo;deploys of the same app&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;A practical way to see this day to day is for each deploy to be able to say which version it&amp;rsquo;s running. At build time, you can store the commit in an environment variable:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# at build time: find the current version of the repository&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;APP_VERSION&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$(&lt;/span&gt;git describe --tags --always&lt;span class="k"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$APP_VERSION&lt;/span&gt; &lt;span class="c1"&gt;# v1.4, or v1.4-3-g9f2c1ab if there are commits after the tag&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In plain PHP, the app&amp;rsquo;s entry point just needs to answer a simple URL (or show the version in the admin panel footer):&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-php" data-lang="php"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// public/index.php
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parse_url&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$_SERVER&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;REQUEST_URI&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;PHP_URL_PATH&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;/version&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nx"&gt;getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;APP_VERSION&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;dev&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In Hyperf, it&amp;rsquo;s a route:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-php" data-lang="php"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// config/routes.php
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nx"&gt;Hyperf\HttpServer\Router\Router&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;Router&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;/version&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;APP_VERSION&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;dev&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;If someone asks &amp;ldquo;what&amp;rsquo;s in production?&amp;rdquo;, the answer is a commit, not &amp;ldquo;I think it&amp;rsquo;s the latest version&amp;rdquo;.&lt;/p&gt;
&lt;h2 data-hextra-search-id="where-factor-i-usually-breaks"&gt;Where factor I usually breaks&lt;span class="hx:absolute hx:-mt-20" id="where-factor-i-usually-breaks"&gt;&lt;/span&gt;
&lt;a href="#where-factor-i-usually-breaks" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;The two mistakes the original text points out show up a lot in PHP projects.&lt;/p&gt;
&lt;h3 data-hextra-search-id="1-one-app-many-codebases-the-copy-per-client"&gt;1. One app, many codebases: the copy per client&lt;span class="hx:absolute hx:-mt-20" id="1-one-app-many-codebases-the-copy-per-client"&gt;&lt;/span&gt;
&lt;a href="#1-one-app-many-codebases-the-copy-per-client" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;The classic scenario: the system is sold to several clients and, for each one, someone copies the folder (or forks the repository) and adjusts what&amp;rsquo;s needed. At first it feels practical. A few months later:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a security fix made in client A&amp;rsquo;s copy never reaches client B;&lt;/li&gt;
&lt;li&gt;client C is stuck on an old version, because nobody dares to update it;&lt;/li&gt;
&lt;li&gt;nobody can say which copy is the &amp;ldquo;official&amp;rdquo; one.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That&amp;rsquo;s exactly the right side of the diagram: several codebases for the same app. The way out is to go back to a single codebase and move the differences out of it:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;what changes from client to client (name, logo, URLs, API keys) becomes &lt;strong&gt;configuration&lt;/strong&gt;, which is the subject of factor III;&lt;/li&gt;
&lt;li&gt;features that only some clients have become &lt;strong&gt;feature flags&lt;/strong&gt;: switched on and off through configuration, with the same code for everyone.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To get started, you don&amp;rsquo;t even need a library. An &lt;code&gt;if&lt;/code&gt; reading an environment variable is already a flag:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-php" data-lang="php"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;FEATURE_CASHBACK&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;on&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nv"&gt;$total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;applyCashback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$total&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Client A runs with &lt;code&gt;FEATURE_CASHBACK=on&lt;/code&gt;, client B without it, and both run exactly the same code.&lt;/p&gt;
&lt;h3 data-hextra-search-id="2-many-apps-one-codebase-copied-and-pasted-code"&gt;2. Many apps, one codebase: copied and pasted code&lt;span class="hx:absolute hx:-mt-20" id="2-many-apps-one-codebase-copied-and-pasted-code"&gt;&lt;/span&gt;
&lt;a href="#2-many-apps-one-codebase-copied-and-pasted-code" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;The opposite mistake: two different applications (the shop website and an internal admin panel, for example) need the same shipping calculation rule. Someone copies the class from one project to the other. Each copy evolves its own way, and one day the shipping cost on the website differs from the one in the panel.&lt;/p&gt;
&lt;p&gt;The original text is direct here: shared code becomes a &lt;strong&gt;library&lt;/strong&gt;, included through the dependency manager. In PHP, that&amp;rsquo;s a package for &lt;a href="https://getcomposer.org/" target="_blank" rel="noopener"&gt;Composer&lt;/a&gt;, PHP&amp;rsquo;s dependency manager. The package can live in a private repository, without going to &lt;a href="https://packagist.org/" target="_blank" rel="noopener"&gt;Packagist&lt;/a&gt;, the public package repository:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-json" data-lang="json"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;repositories&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nt"&gt;&amp;#34;type&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;vcs&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;&amp;#34;url&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;git@github.com:my-company/shipping-calculator.git&amp;#34;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;],&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;require&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;my-company/shipping-calculator&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;^2.1&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now the website and the panel use the same rule, each on a declared version. Fixing the shipping cost means releasing &lt;code&gt;2.1.1&lt;/code&gt; of the package and updating both apps. How to declare and pin those versions is exactly factor II, dependencies.&lt;/p&gt;
&lt;h3 data-hextra-search-id="bonus-the-fix-made-straight-on-the-server"&gt;Bonus: the fix made straight on the server&lt;span class="hx:absolute hx:-mt-20" id="bonus-the-fix-made-straight-on-the-server"&gt;&lt;/span&gt;
&lt;a href="#bonus-the-fix-made-straight-on-the-server" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;This isn&amp;rsquo;t one of the two cases in the text, but it breaks factor I just the same. Someone opens FTP, fixes a file in production and walks away. From then on, the code running in production doesn&amp;rsquo;t exist in any commit. The next deploy wipes out the fix, and the bug comes back.&lt;/p&gt;
&lt;p&gt;A quick test to find out if this happened: on servers where the deploy is a &lt;code&gt;git pull&lt;/code&gt;, run &lt;code&gt;git status&lt;/code&gt; in the app folder. Any modified file there is code that isn&amp;rsquo;t in the codebase.&lt;/p&gt;
&lt;h2 data-hextra-search-id="what-about-monorepos"&gt;What about monorepos?&lt;span class="hx:absolute hx:-mt-20" id="what-about-monorepos"&gt;&lt;/span&gt;
&lt;a href="#what-about-monorepos" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;A &lt;strong&gt;monorepo&lt;/strong&gt; is a single repository that holds several apps, like an API and an admin panel side by side. It seems to contradict &amp;ldquo;one app, one repository&amp;rdquo;, and the original text, which predates the popularity of this practice, doesn&amp;rsquo;t mention it.&lt;/p&gt;
&lt;p&gt;In practice, the spirit of the factor still holds: each app inside the monorepo has a clear folder, its own build, and deploys that come from a known commit. The problem the factor wants to avoid isn&amp;rsquo;t having many files in a repository, it&amp;rsquo;s not being able to say which code is running where.&lt;/p&gt;
&lt;h2 data-hextra-search-id="how-to-tell-if-your-project-follows-factor-i"&gt;How to tell if your project follows factor I&lt;span class="hx:absolute hx:-mt-20" id="how-to-tell-if-your-project-follows-factor-i"&gt;&lt;/span&gt;
&lt;a href="#how-to-tell-if-your-project-follows-factor-i" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;A few quick questions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Does the code in production match a specific commit in the repository?&lt;/li&gt;
&lt;li&gt;Is there any copy of the system &amp;ldquo;just for one client&amp;rdquo;?&lt;/li&gt;
&lt;li&gt;Was any class copied from another project instead of coming from a package?&lt;/li&gt;
&lt;li&gt;Has anyone ever fixed something straight on the server?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If the answer to the first one is &amp;ldquo;I don&amp;rsquo;t know&amp;rdquo;, or any of the others is &amp;ldquo;yes&amp;rdquo;, this is a good place to start. It&amp;rsquo;s also a matter of ownership: when nobody can say which code is in production, nobody really owns it.&lt;/p&gt;
&lt;h2 data-hextra-search-id="next-article"&gt;Next article&lt;span class="hx:absolute hx:-mt-20" id="next-article"&gt;&lt;/span&gt;
&lt;a href="#next-article" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;The next article in the series covers factor II, &lt;strong&gt;dependencies&lt;/strong&gt;: why the app should never assume something is &amp;ldquo;already installed&amp;rdquo; on the server, and how &lt;code&gt;composer.json&lt;/code&gt;, &lt;code&gt;composer.lock&lt;/code&gt; and PHP extensions fit into that story.&lt;/p&gt;
&lt;h2 data-hextra-search-id="glossary"&gt;Glossary&lt;span class="hx:absolute hx:-mt-20" id="glossary"&gt;&lt;/span&gt;
&lt;a href="#glossary" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Version control:&lt;/strong&gt; a system that stores every change to the code, with who made it and when, and lets you go back to any point in the history.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Commit:&lt;/strong&gt; a recorded change in the repository, identified by a unique code (the &lt;em&gt;hash&lt;/em&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tag:&lt;/strong&gt; a fixed name given to a commit, usually a version, like &lt;code&gt;v1.4&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fork:&lt;/strong&gt; a copy of a repository that takes on a life of its own.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Feature flag:&lt;/strong&gt; a switch in the configuration that turns a feature on or off without changing the code.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 data-hextra-search-id="sources"&gt;Sources&lt;span class="hx:absolute hx:-mt-20" id="sources"&gt;&lt;/span&gt;
&lt;a href="#sources" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;a href="https://12factor.net/" target="_blank" rel="noopener"&gt;The Twelve-Factor App, in English&lt;/a&gt; and &lt;a href="https://12factor.net/pt_br/" target="_blank" rel="noopener"&gt;in Portuguese&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://12factor.net/codebase" target="_blank" rel="noopener"&gt;I. Codebase&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://getcomposer.org/doc/05-repositories.md#vcs" target="_blank" rel="noopener"&gt;Composer: VCS repositories&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://hyperf.wiki/3.1/#/en/router" target="_blank" rel="noopener"&gt;Hyperf: routing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://git-scm.com/docs/git-describe" target="_blank" rel="noopener"&gt;Git: git describe&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>12 factors: dependencies declared, nothing assumed</title><link>https://luizschons.com/posts/twelve-factor-dependencies/</link><pubDate>Mon, 23 Nov 2026 00:00:00 -0300</pubDate><guid>https://luizschons.com/posts/twelve-factor-dependencies/</guid><description>
&lt;p&gt;&amp;ldquo;It works on my machine.&amp;rdquo; Almost always, that sentence hides a dependency nobody declared: a library installed by hand, a PHP extension that only exists on the laptop of whoever wrote the code, a package version different from the one on the server.&lt;/p&gt;
&lt;p&gt;This is the second article in the &lt;strong&gt;Twelve factors in practice&lt;/strong&gt; series. The &lt;a href="https://luizschons.com/posts/twelve-factor-codebase/"&gt;first one&lt;/a&gt; covered factor I, codebase. Now it&amp;rsquo;s factor II, &lt;strong&gt;dependencies&lt;/strong&gt;, with examples in &lt;a href="https://getcomposer.org/" target="_blank" rel="noopener"&gt;Composer&lt;/a&gt;, PHP&amp;rsquo;s package manager, and a comparison with what other languages use.&lt;/p&gt;
&lt;h2 data-hextra-search-id="before-we-start-three-words"&gt;Before we start: three words&lt;span class="hx:absolute hx:-mt-20" id="before-we-start-three-words"&gt;&lt;/span&gt;
&lt;a href="#before-we-start-three-words" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Dependency:&lt;/strong&gt; any code or tool your app needs to work that you didn&amp;rsquo;t write. A library to generate UUIDs, an HTTP client, a PHP extension.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Package:&lt;/strong&gt; a dependency bundled so a tool can download and install it, with a name and a version.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Package manager:&lt;/strong&gt; the tool that reads the project&amp;rsquo;s list of dependencies, works out which versions to use and downloads everything. In PHP it&amp;rsquo;s Composer, in JavaScript it&amp;rsquo;s &lt;a href="https://www.npmjs.com/" target="_blank" rel="noopener"&gt;npm&lt;/a&gt;, in Python it&amp;rsquo;s &lt;a href="https://pip.pypa.io/" target="_blank" rel="noopener"&gt;pip&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 data-hextra-search-id="what-factor-ii-says"&gt;What factor II says&lt;span class="hx:absolute hx:-mt-20" id="what-factor-ii-says"&gt;&lt;/span&gt;
&lt;a href="#what-factor-ii-says" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;The original text fits in two rules:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Declare everything.&lt;/strong&gt; The app lists, in a file inside its own repository, every dependency it needs, with versions. That file is called the &lt;strong&gt;manifest&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Isolate everything.&lt;/strong&gt; The app doesn&amp;rsquo;t use anything that &amp;ldquo;happens to be&amp;rdquo; installed on the machine. Dependencies live in a project folder, separate from other projects on the same server.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The practical consequence is the most important part: a new person on the team clones the repository, runs &lt;strong&gt;one command&lt;/strong&gt; and has the app working. No list of steps in a forgotten document, no &amp;ldquo;oh, first you need to install this other thing&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;An analogy helps. Think of a cake recipe. A bad recipe says &amp;ldquo;use flour&amp;rdquo;. A good recipe says &amp;ldquo;500 g of all-purpose wheat flour&amp;rdquo;. The manifest is the good recipe: it leaves nothing to the luck of whoever is cooking.&lt;/p&gt;
&lt;h2 data-hextra-search-id="the-manifest-in-php-composerjson"&gt;The manifest in PHP: composer.json&lt;span class="hx:absolute hx:-mt-20" id="the-manifest-in-php-composerjson"&gt;&lt;/span&gt;
&lt;a href="#the-manifest-in-php-composerjson" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;In Composer, the manifest is &lt;code&gt;composer.json&lt;/code&gt;. This article&amp;rsquo;s lab uses a minimal app that generates a UUID with the &lt;a href="https://uuid.ramsey.dev/" target="_blank" rel="noopener"&gt;ramsey/uuid&lt;/a&gt; library:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-json" data-lang="json"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;name&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;lab/twelve-factor-dependencies&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;require&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;php&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;&amp;gt;=8.2&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;ext-redis&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;*&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;ramsey/uuid&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;^4.0&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;require-dev&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;symfony/var-dumper&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;^7.0&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Each line has a job:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;&amp;quot;php&amp;quot;: &amp;quot;&amp;gt;=8.2&amp;quot;&lt;/code&gt;: the minimum version of PHP itself.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;quot;ext-redis&amp;quot;: &amp;quot;*&amp;quot;&lt;/code&gt;: a PHP &lt;strong&gt;extension&lt;/strong&gt;, a module written in C that must be installed on the server. It&amp;rsquo;s a dependency too, and it needs to be declared too.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;quot;ramsey/uuid&amp;quot;: &amp;quot;^4.0&amp;quot;&lt;/code&gt;: a library, accepting any 4.x version.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;require-dev&lt;/code&gt;: things only needed during development, like debugging and testing tools. They shouldn&amp;rsquo;t go to production.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;code&gt;^4.0&lt;/code&gt; is a &lt;strong&gt;version range&lt;/strong&gt;: &amp;ldquo;any 4.something, as long as it&amp;rsquo;s at least 4.0&amp;rdquo;. It&amp;rsquo;s useful for getting fixes, but it creates a problem: two people installing on different days can get different versions.&lt;/p&gt;
&lt;h2 data-hextra-search-id="the-lock-composerlock"&gt;The lock: composer.lock&lt;span class="hx:absolute hx:-mt-20" id="the-lock-composerlock"&gt;&lt;/span&gt;
&lt;a href="#the-lock-composerlock" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;That&amp;rsquo;s where &lt;code&gt;composer.lock&lt;/code&gt; comes in. &lt;code&gt;composer.json&lt;/code&gt; says what the app &lt;strong&gt;accepts&lt;/strong&gt;. &lt;code&gt;composer.lock&lt;/code&gt; says what was &lt;strong&gt;chosen&lt;/strong&gt;, with the exact version of every package, including the dependencies of the dependencies.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://luizschons.com/images/posts/twelve-factor-dependencies/flow-en.svg" alt="Diagram: composer.json, with the range ^4.0, becomes composer.lock through composer update, which stores the exact version 4.10.0. composer.lock becomes the vendor/ folder through composer install. composer.json and composer.lock go into Git; the vendor/ folder does not, it is rebuilt on every machine from the lock" loading="lazy" /&gt;&lt;/p&gt;
&lt;p&gt;In the lab, the first &lt;code&gt;composer update&lt;/code&gt; resolved the range and wrote the lock:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ composer update
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Updating dependencies
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Lock file operations: 6 installs, 0 updates, 0 removals
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - Locking brick/math (1.0.0)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - Locking ramsey/collection (2.1.1)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - Locking ramsey/uuid (4.10.0)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ...
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Writing lock file&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;code&gt;composer.json&lt;/code&gt; still says &lt;code&gt;^4.0&lt;/code&gt;, but the lock now says &lt;code&gt;4.10.0&lt;/code&gt;. Notice also that one package was requested and six came in: &lt;code&gt;ramsey/uuid&lt;/code&gt; depends on &lt;code&gt;brick/math&lt;/code&gt; and &lt;code&gt;ramsey/collection&lt;/code&gt;, and the lock keeps all of them.&lt;/p&gt;
&lt;p&gt;Then the test deleted the &lt;code&gt;vendor/&lt;/code&gt; folder and ran &lt;code&gt;composer install&lt;/code&gt;:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ rm -rf vendor &amp;amp;&amp;amp; composer install
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Installing dependencies from lock file (including require-dev)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Verifying lock file contents can be installed on current platform.
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Package operations: 6 installs, 0 updates, 0 removals
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - Installing ramsey/collection (2.1.1): Extracting archive
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - Installing brick/math (1.0.0): Extracting archive
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - Installing ramsey/uuid (4.10.0): Extracting archive&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first line is the one that matters: &lt;strong&gt;&amp;ldquo;from lock file&amp;rdquo;&lt;/strong&gt;. &lt;code&gt;install&lt;/code&gt; doesn&amp;rsquo;t choose versions, it reproduces what&amp;rsquo;s in the lock. So the rule is simple:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;composer update&lt;/code&gt;: picks new versions and rewrites the lock. Run it when you &lt;strong&gt;want&lt;/strong&gt; to update.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;composer install&lt;/code&gt;: installs exactly what&amp;rsquo;s in the lock. Run it everywhere else: on a newcomer&amp;rsquo;s machine, in CI and in the deploy.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Both files, &lt;code&gt;composer.json&lt;/code&gt; and &lt;code&gt;composer.lock&lt;/code&gt;, go into the repository. The &lt;code&gt;vendor/&lt;/code&gt; folder goes into &lt;code&gt;.gitignore&lt;/code&gt;.&lt;/p&gt;
&lt;h3 data-hextra-search-id="when-the-lock-and-the-manifest-disagree"&gt;When the lock and the manifest disagree&lt;span class="hx:absolute hx:-mt-20" id="when-the-lock-and-the-manifest-disagree"&gt;&lt;/span&gt;
&lt;a href="#when-the-lock-and-the-manifest-disagree" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;The lab also tested the most common mistake: someone edits &lt;code&gt;composer.json&lt;/code&gt; by hand, adding &lt;code&gt;&amp;quot;monolog/monolog&amp;quot;: &amp;quot;^3.0&amp;quot;&lt;/code&gt;, and forgets to update the lock. &lt;code&gt;composer install&lt;/code&gt; warned and stopped:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Warning: The lock file is not up to date with the latest changes in composer.json.
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;- Required package &amp;#34;monolog/monolog&amp;#34; is not present in the lock file.
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;This usually happens when composer files are incorrectly merged or the
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;composer.json file is manually edited.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The fix is to use &lt;code&gt;composer require monolog/monolog&lt;/code&gt;, which updates both files together, instead of editing the JSON by hand.&lt;/p&gt;
&lt;h2 data-hextra-search-id="php-extensions-are-dependencies-too"&gt;PHP extensions are dependencies too&lt;span class="hx:absolute hx:-mt-20" id="php-extensions-are-dependencies-too"&gt;&lt;/span&gt;
&lt;a href="#php-extensions-are-dependencies-too" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;This is the point PHP projects miss most often. An app that uses Redis through the &lt;code&gt;redis&lt;/code&gt; extension works on the laptop that has the extension and breaks on the server that doesn&amp;rsquo;t. If the extension isn&amp;rsquo;t in &lt;code&gt;composer.json&lt;/code&gt;, nobody finds out until the error shows up in production.&lt;/p&gt;
&lt;p&gt;In the lab, &lt;code&gt;composer.json&lt;/code&gt; declared &lt;code&gt;ext-redis&lt;/code&gt;, which wasn&amp;rsquo;t installed on the machine. &lt;code&gt;composer install&lt;/code&gt; refused right away:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Problem 1
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - Root composer.json requires PHP extension ext-redis * but it is missing
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; from your system. Install or enable PHP&amp;#39;s redis extension.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;And the &lt;code&gt;composer check-platform-reqs&lt;/code&gt; command shows the state of everything at once:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ composer check-platform-reqs
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;ext-iconv 8.4.27 success
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;ext-redis n/a lab/twelve-factor-dependencies requires ext-redis (*) missing
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;php 8.4.27 success&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;code&gt;ext-iconv&lt;/code&gt; shows up without being requested because one of the locked packages needs it. This command is a good step for CI or for the start of a deploy: if something is missing, the process stops before the version goes live.&lt;/p&gt;
&lt;p&gt;One caveat: Composer &lt;strong&gt;checks&lt;/strong&gt; extensions, but doesn&amp;rsquo;t &lt;strong&gt;install&lt;/strong&gt; them. The image or the server does that. In &lt;a href="https://www.docker.com/" target="_blank" rel="noopener"&gt;Docker&lt;/a&gt;, a tool that packages the app with everything it needs into an image, this is written in the &lt;code&gt;Dockerfile&lt;/code&gt;:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-dockerfile" data-lang="dockerfile"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;php:8.4-cli&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c"&gt;# the extension declared in composer.json, installed in the image&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;RUN&lt;/span&gt; apt-get update &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; apt-get install -y --no-install-recommends &lt;span class="nv"&gt;$PHPIZE_DEPS&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; pecl install redis &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; docker-php-ext-enable redis &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; apt-get purge -y --auto-remove &lt;span class="nv"&gt;$PHPIZE_DEPS&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; rm -rf /var/lib/apt/lists/*&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; --from&lt;span class="o"&gt;=&lt;/span&gt;composer:2 /usr/bin/composer /usr/bin/composer&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;/app&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c"&gt;# manifest and lock first: install exactly what&amp;#39;s in the lock&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; composer.json composer.lock ./&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;RUN&lt;/span&gt; composer install --no-dev --optimize-autoloader --no-interaction&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; public ./public&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;CMD&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;php&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;-S&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;0.0.0.0:8080&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;-t&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;public&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This way, the full list of what the app needs lives in the repository: the libraries in &lt;code&gt;composer.json&lt;/code&gt;, the exact versions in &lt;code&gt;composer.lock&lt;/code&gt;, and what goes on the system in the &lt;code&gt;Dockerfile&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The same goes for tools the app calls from the outside, like &lt;code&gt;curl&lt;/code&gt; or &lt;a href="https://imagemagick.org/" target="_blank" rel="noopener"&gt;ImageMagick&lt;/a&gt; to resize images. The original text mentions exactly this case: if the app depends on a system program, that program has to come along, declared.&lt;/p&gt;
&lt;h2 data-hextra-search-id="production-without-the-development-tools"&gt;Production without the development tools&lt;span class="hx:absolute hx:-mt-20" id="production-without-the-development-tools"&gt;&lt;/span&gt;
&lt;a href="#production-without-the-development-tools" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;&lt;code&gt;require-dev&lt;/code&gt; exists so development tools don&amp;rsquo;t go to production. In the lab, with &lt;code&gt;symfony/var-dumper&lt;/code&gt; installed, the test ran:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ composer install --no-dev --optimize-autoloader
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Installing dependencies from lock file
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Package operations: 0 installs, 0 updates, 3 removals
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - Removing symfony/var-dumper (v7.4.22)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - Removing symfony/polyfill-mbstring (v1.43.3)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - Removing symfony/deprecation-contracts (v3.8.1)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Generating optimized autoload files&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;code&gt;--no-dev&lt;/code&gt; removed the development package and the dependencies only it used. &lt;code&gt;--optimize-autoloader&lt;/code&gt; builds a map of the classes ahead of time, so PHP doesn&amp;rsquo;t have to search file by file on every request.&lt;/p&gt;
&lt;h2 data-hextra-search-id="what-about-other-languages"&gt;What about other languages?&lt;span class="hx:absolute hx:-mt-20" id="what-about-other-languages"&gt;&lt;/span&gt;
&lt;a href="#what-about-other-languages" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;The problem is the same in any language, and so is the solution: a manifest, a lock and an isolated folder. Only the names change.&lt;/p&gt;
&lt;div class="article-table-wrap"&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Language&lt;/th&gt;
&lt;th&gt;Manager&lt;/th&gt;
&lt;th&gt;Manifest&lt;/th&gt;
&lt;th&gt;Lock&lt;/th&gt;
&lt;th&gt;Install exactly what&amp;rsquo;s locked&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;PHP&lt;/td&gt;
&lt;td&gt;&lt;a href="https://getcomposer.org/" target="_blank" rel="noopener"&gt;Composer&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;composer.json&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;composer.lock&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;composer install&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;JavaScript&lt;/td&gt;
&lt;td&gt;&lt;a href="https://www.npmjs.com/" target="_blank" rel="noopener"&gt;npm&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;package.json&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;package-lock.json&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;npm ci&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Python&lt;/td&gt;
&lt;td&gt;&lt;a href="https://pip.pypa.io/" target="_blank" rel="noopener"&gt;pip&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;requirements.txt&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;the same file, with pinned versions (&lt;code&gt;==&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;pip install -r requirements.txt&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Python&lt;/td&gt;
&lt;td&gt;&lt;a href="https://docs.astral.sh/uv/" target="_blank" rel="noopener"&gt;uv&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;pyproject.toml&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;uv.lock&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;uv sync --locked&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ruby&lt;/td&gt;
&lt;td&gt;&lt;a href="https://bundler.io/" target="_blank" rel="noopener"&gt;Bundler&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Gemfile&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Gemfile.lock&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;bundle install&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Rust&lt;/td&gt;
&lt;td&gt;&lt;a href="https://doc.rust-lang.org/cargo/" target="_blank" rel="noopener"&gt;Cargo&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Cargo.toml&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Cargo.lock&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;cargo build --locked&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Go&lt;/td&gt;
&lt;td&gt;&lt;a href="https://go.dev/ref/mod" target="_blank" rel="noopener"&gt;Go modules&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;go.mod&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;go.sum&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;go mod download&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;A few differences worth knowing:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;npm:&lt;/strong&gt; &lt;code&gt;npm install&lt;/code&gt; can update &lt;code&gt;package-lock.json&lt;/code&gt;. For CI and deploys, the right command is &lt;code&gt;npm ci&lt;/code&gt;, which follows the lock strictly and fails if it doesn&amp;rsquo;t match &lt;code&gt;package.json&lt;/code&gt;. It&amp;rsquo;s the direct equivalent of &lt;code&gt;composer install&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;pip:&lt;/strong&gt; pip doesn&amp;rsquo;t have a separate lock. The classic approach is to pin versions in &lt;code&gt;requirements.txt&lt;/code&gt; (&lt;code&gt;requests==2.32.3&lt;/code&gt;) and isolate dependencies in a &lt;strong&gt;virtual environment&lt;/strong&gt; (&lt;code&gt;python -m venv&lt;/code&gt;), which plays the role of the &lt;code&gt;vendor/&lt;/code&gt; folder. Newer tools like uv and &lt;a href="https://python-poetry.org/" target="_blank" rel="noopener"&gt;Poetry&lt;/a&gt; bring a real lock.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Go:&lt;/strong&gt; &lt;code&gt;go.sum&lt;/code&gt; stores verification codes (&lt;em&gt;checksums&lt;/em&gt;) for each version, to guarantee the downloaded code is the same as before.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you already use one of these, Composer will feel familiar, and the other way around too.&lt;/p&gt;
&lt;h2 data-hextra-search-id="how-to-tell-if-your-project-follows-factor-ii"&gt;How to tell if your project follows factor II&lt;span class="hx:absolute hx:-mt-20" id="how-to-tell-if-your-project-follows-factor-ii"&gt;&lt;/span&gt;
&lt;a href="#how-to-tell-if-your-project-follows-factor-ii" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;A few quick questions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Is &lt;code&gt;composer.lock&lt;/code&gt; in the repository?&lt;/li&gt;
&lt;li&gt;Does the deploy run &lt;code&gt;composer install&lt;/code&gt;, not &lt;code&gt;composer update&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;Are the PHP extensions the app uses in the &lt;code&gt;require&lt;/code&gt; section of &lt;code&gt;composer.json&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;Can a newcomer run the project with one command, without a separate step-by-step guide?&lt;/li&gt;
&lt;li&gt;Does production install with &lt;code&gt;--no-dev&lt;/code&gt;?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If any answer is &amp;ldquo;no&amp;rdquo;, start there. It&amp;rsquo;s also a matter of ownership: when the dependency list is written down, anyone on the team can understand, install and update the app, and it stops depending on someone&amp;rsquo;s memory.&lt;/p&gt;
&lt;h2 data-hextra-search-id="next-article"&gt;Next article&lt;span class="hx:absolute hx:-mt-20" id="next-article"&gt;&lt;/span&gt;
&lt;a href="#next-article" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;In the next article in the series, the topic is factor III, &lt;strong&gt;config&lt;/strong&gt;: why passwords, addresses and keys stay out of the code, in environment variables, and how to read that config in PHP.&lt;/p&gt;
&lt;h2 data-hextra-search-id="glossary"&gt;Glossary&lt;span class="hx:absolute hx:-mt-20" id="glossary"&gt;&lt;/span&gt;
&lt;a href="#glossary" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Manifest:&lt;/strong&gt; the file that lists the project&amp;rsquo;s dependencies and the accepted versions, like &lt;code&gt;composer.json&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lock:&lt;/strong&gt; the file that stores the exact version chosen for each dependency, like &lt;code&gt;composer.lock&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Version range:&lt;/strong&gt; a rule like &lt;code&gt;^4.0&lt;/code&gt; that accepts several versions instead of just one.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PHP extension:&lt;/strong&gt; a module, usually written in C, that adds functions to PHP and must be installed on the server, like &lt;code&gt;redis&lt;/code&gt; or &lt;code&gt;intl&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Autoloader:&lt;/strong&gt; the code that loads each PHP class automatically when it&amp;rsquo;s used, without a manual &lt;code&gt;require&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Virtual environment:&lt;/strong&gt; in Python, an isolated folder with a project&amp;rsquo;s dependencies, separate from the system&amp;rsquo;s.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 data-hextra-search-id="sources"&gt;Sources&lt;span class="hx:absolute hx:-mt-20" id="sources"&gt;&lt;/span&gt;
&lt;a href="#sources" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;a href="https://12factor.net/dependencies" target="_blank" rel="noopener"&gt;The Twelve-Factor App: II. Dependencies&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://getcomposer.org/doc/01-basic-usage.md" target="_blank" rel="noopener"&gt;Composer: basic usage, composer.lock and install&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://getcomposer.org/doc/01-basic-usage.md#platform-packages" target="_blank" rel="noopener"&gt;Composer: platform packages (php and ext-*)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://getcomposer.org/doc/03-cli.md#check-platform-reqs" target="_blank" rel="noopener"&gt;Composer: check-platform-reqs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://hub.docker.com/_/php" target="_blank" rel="noopener"&gt;Official PHP image on Docker Hub: installing extensions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.npmjs.com/cli/commands/npm-ci" target="_blank" rel="noopener"&gt;npm: npm ci&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pip.pypa.io/en/stable/reference/requirements-file-format/" target="_blank" rel="noopener"&gt;pip: requirements file format&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.astral.sh/uv/concepts/projects/sync/" target="_blank" rel="noopener"&gt;uv: projects and lockfile&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://bundler.io/guides/rationale.html" target="_blank" rel="noopener"&gt;Bundler: Gemfile.lock&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html" target="_blank" rel="noopener"&gt;Cargo: Cargo.toml vs Cargo.lock&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://go.dev/ref/mod#go-sum-files" target="_blank" rel="noopener"&gt;Go modules: go.sum&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>12 factors: config outside the code</title><link>https://luizschons.com/posts/twelve-factor-config/</link><pubDate>Mon, 30 Nov 2026 00:00:00 -0300</pubDate><guid>https://luizschons.com/posts/twelve-factor-config/</guid><description>
&lt;p&gt;Every team has seen a commit with the message &amp;ldquo;remove password&amp;rdquo;. The problem is that Git does not forget: the password is still in the history, within reach of anyone who clones the repository. And it almost always got there for the same reason: someone wrote the config straight into the code.&lt;/p&gt;
&lt;p&gt;This is the third article in the &lt;strong&gt;Twelve factors in practice&lt;/strong&gt; series. The &lt;a href="https://luizschons.com/posts/twelve-factor-codebase/"&gt;first one&lt;/a&gt; covered factor I, codebase. The &lt;a href="https://luizschons.com/posts/twelve-factor-dependencies/"&gt;second one&lt;/a&gt; covered factor II, dependencies. Now it is time for factor III, &lt;strong&gt;config&lt;/strong&gt;: what it is, where it should live and how to read it in plain PHP, with no framework.&lt;/p&gt;
&lt;h2 data-hextra-search-id="before-we-start-three-words"&gt;Before we start: three words&lt;span class="hx:absolute hx:-mt-20" id="before-we-start-three-words"&gt;&lt;/span&gt;
&lt;a href="#before-we-start-three-words" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Config:&lt;/strong&gt; everything that changes from one deploy to another. The database address, the database password, the key for a payment API, the log level.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Environment variable:&lt;/strong&gt; a name and value pair, like &lt;code&gt;DB_HOST=localhost&lt;/code&gt;, that the operating system hands to each program when it starts. Every process has its own, and they do not live in any file of the project.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Secret:&lt;/strong&gt; a piece of config that must not leak, such as passwords, tokens and private keys.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 data-hextra-search-id="what-factor-iii-says"&gt;What factor III says&lt;span class="hx:absolute hx:-mt-20" id="what-factor-iii-says"&gt;&lt;/span&gt;
&lt;a href="#what-factor-iii-says" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;The original text is direct: the app keeps its config &lt;strong&gt;separate from the code&lt;/strong&gt;, in &lt;strong&gt;environment variables&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The idea behind it is that the code is the same in every deploy. Production, staging and the developer&amp;rsquo;s machine run the same code. What changes between them is only the config. If the config is inside the code, each deploy needs different code, and factor I stops holding.&lt;/p&gt;
&lt;p&gt;An analogy helps. Think of the phone charger you take on a trip. The charger is the same in any country. What changes is the plug adapter. Nobody opens the charger to solder a new plug on every trip: they swap the adapter. The code is the charger, the config is the adapter.&lt;/p&gt;
&lt;h2 data-hextra-search-id="what-is-config-and-what-is-not"&gt;What is config, and what is not&lt;span class="hx:absolute hx:-mt-20" id="what-is-config-and-what-is-not"&gt;&lt;/span&gt;
&lt;a href="#what-is-config-and-what-is-not" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;Not everything in a file called &lt;code&gt;config&lt;/code&gt; is config in the factor III sense. The question that tells them apart is: &lt;strong&gt;does this change from one deploy to another?&lt;/strong&gt;&lt;/p&gt;
&lt;div class="article-table-wrap"&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Config (changes per deploy)&lt;/th&gt;
&lt;th&gt;Not config (same in every deploy)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Database address and password&lt;/td&gt;
&lt;td&gt;The app routes (&lt;code&gt;/login&lt;/code&gt; calls &lt;code&gt;LoginController&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Payment API key&lt;/td&gt;
&lt;td&gt;Which class implements which interface&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mail server address&lt;/td&gt;
&lt;td&gt;Validation rules for a form&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Log level, cache time&lt;/td&gt;
&lt;td&gt;Database table names&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;The right side is &lt;strong&gt;internal&lt;/strong&gt; app config. It can and should stay in the code, versioned in Git, because it is part of how the app works. If the routes changed between production and staging, you would be testing a different app from the one that goes live.&lt;/p&gt;
&lt;h2 data-hextra-search-id="the-open-source-test"&gt;The open source test&lt;span class="hx:absolute hx:-mt-20" id="the-open-source-test"&gt;&lt;/span&gt;
&lt;a href="#the-open-source-test" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;The original text suggests a simple test: &lt;strong&gt;could the code be made open source right now, without leaking any credentials?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If the answer is &amp;ldquo;no&amp;rdquo;, some config is in the wrong place. The lab for this article has an example of the wrong way, a file with the password written in the code:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-php" data-lang="php"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="o"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="nx"&gt;php&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// Wrong way: config written in the code.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;$config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s1"&gt;&amp;#39;db_host&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;localhost&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s1"&gt;&amp;#39;db_password&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;super-secret-123&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s1"&gt;&amp;#39;mail_from&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;noreply@example.com&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;];&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The lab also has a &lt;code&gt;.env&lt;/code&gt; file with another password, only for local development (more on it later). The test searched for both passwords in everything Git tracks, with &lt;code&gt;git grep&lt;/code&gt;:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ git grep -n -E &amp;#39;super-secret-123|local-only-pass&amp;#39;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;src/01-hardcoded.php:5: &amp;#39;db_password&amp;#39; =&amp;gt; &amp;#39;super-secret-123&amp;#39;,&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The password written in the code showed up. The one in &lt;code&gt;.env&lt;/code&gt; did not, because that file is in &lt;code&gt;.gitignore&lt;/code&gt; and never enters the repository. The goal is for this search to find nothing at all. Tools like &lt;a href="https://github.com/gitleaks/gitleaks" target="_blank" rel="noopener"&gt;gitleaks&lt;/a&gt; run this search automatically, with patterns for well-known passwords and keys, and can run in CI on every commit.&lt;/p&gt;
&lt;h2 data-hextra-search-id="reading-environment-variables-in-php"&gt;Reading environment variables in PHP&lt;span class="hx:absolute hx:-mt-20" id="reading-environment-variables-in-php"&gt;&lt;/span&gt;
&lt;a href="#reading-environment-variables-in-php" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;PHP offers three ways to read an environment variable: the &lt;code&gt;getenv()&lt;/code&gt; function, the &lt;code&gt;$_ENV&lt;/code&gt; array and the &lt;code&gt;$_SERVER&lt;/code&gt; array. They look the same, but the lab showed they are not.&lt;/p&gt;
&lt;p&gt;The test ran a script that reads &lt;code&gt;DB_HOST&lt;/code&gt; in all three ways:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ DB_HOST=db.internal php src/02-getenv-vs-env.php
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;variables_order=GPCS
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;getenv(DB_HOST)=&amp;#39;db.internal&amp;#39;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$_ENV[DB_HOST]=NULL
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$_SERVER[DB_HOST]=&amp;#39;db.internal&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;code&gt;$_ENV&lt;/code&gt; came back empty. The reason is a &lt;code&gt;php.ini&lt;/code&gt; directive called &lt;code&gt;variables_order&lt;/code&gt;, which says which arrays PHP fills in. The default in common installs, like the Debian one used in the lab, is &lt;code&gt;GPCS&lt;/code&gt;, and the letter &lt;code&gt;E&lt;/code&gt; (for &lt;em&gt;environment&lt;/em&gt;) is not there. With the letter back, all three agree:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ DB_HOST=db.internal php -d variables_order=EGPCS src/02-getenv-vs-env.php
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;variables_order=EGPCS
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;getenv(DB_HOST)=&amp;#39;db.internal&amp;#39;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$_ENV[DB_HOST]=&amp;#39;db.internal&amp;#39;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$_SERVER[DB_HOST]=&amp;#39;db.internal&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In practice: to read straight from the environment, &lt;code&gt;getenv()&lt;/code&gt; is the most predictable path. Anyone using &lt;code&gt;$_ENV&lt;/code&gt; needs to check the server&amp;rsquo;s &lt;code&gt;variables_order&lt;/code&gt;.&lt;/p&gt;
&lt;h2 data-hextra-search-id="one-build-two-environments"&gt;One build, two environments&lt;span class="hx:absolute hx:-mt-20" id="one-build-two-environments"&gt;&lt;/span&gt;
&lt;a href="#one-build-two-environments" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;With reading sorted out, the lab built a &lt;code&gt;config.php&lt;/code&gt; that takes everything from the environment, with defaults for what is optional:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-php" data-lang="php"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="o"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="nx"&gt;php&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s1"&gt;&amp;#39;db_host&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;env_required&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;DB_HOST&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s1"&gt;&amp;#39;db_password&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;env_required&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;DB_PASSWORD&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s1"&gt;&amp;#39;log_level&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;env_optional&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;LOG_LEVEL&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;info&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s1"&gt;&amp;#39;cache_ttl&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;env_optional&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;CACHE_TTL&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;60&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;];&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;env_required()&lt;/code&gt; and &lt;code&gt;env_optional()&lt;/code&gt; functions are small and appear in the next section. The same code ran twice, first with development values and then with production values:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ DB_HOST=localhost DB_PASSWORD=dev-pass LOG_LEVEL=debug php src/03-app.php
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;connecting to localhost
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;password length=8
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;log_level=debug cache_ttl=60
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ DB_HOST=db.prod.internal DB_PASSWORD=Pr0d-s3cret CACHE_TTL=3600 php src/03-app.php
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;connecting to db.prod.internal
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;password length=11
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;log_level=info cache_ttl=3600&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;No file was edited between the two runs. Only the environment changed, and with it the database, the log level and the cache time. Notice also that the app prints only the password &lt;strong&gt;length&lt;/strong&gt;, never the password. Secret config does not show up in logs.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://luizschons.com/images/posts/twelve-factor-config/config-en.svg" alt="Diagram: the same code, one build with no passwords, runs in two deploys. With the dev env, DB_HOST=localhost, it becomes the dev deploy, with a local database and debug log. With the prod env, DB_HOST=db.prod.internal, it becomes the production deploy, with the production database and info log. The code goes to Git; the values live in the environment of each deploy and never in the repository" loading="lazy" /&gt;&lt;/p&gt;
&lt;h2 data-hextra-search-id="fail-fast-when-something-is-missing"&gt;Fail fast when something is missing&lt;span class="hx:absolute hx:-mt-20" id="fail-fast-when-something-is-missing"&gt;&lt;/span&gt;
&lt;a href="#fail-fast-when-something-is-missing" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;What if someone forgets to set the password in production? The worst outcome is the app starting, looking healthy and only breaking on the first request that needs the database, minutes or hours later.&lt;/p&gt;
&lt;p&gt;That is why the lab&amp;rsquo;s &lt;code&gt;config.php&lt;/code&gt; separates what is required from what is optional:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-php" data-lang="php"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="o"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="nx"&gt;php&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;env_required&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nv"&gt;$value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$value&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nv"&gt;$value&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;fwrite&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;STDERR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;Config error: required environment variable &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is not set&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;env_optional&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$default&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nv"&gt;$value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$value&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nv"&gt;$value&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nv"&gt;$default&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The test ran the app without the password and then with an empty password:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ DB_HOST=db.prod.internal php src/03-app.php
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Config error: required environment variable DB_PASSWORD is not set
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[exit 1]
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ DB_HOST=db.prod.internal DB_PASSWORD= php src/03-app.php
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Config error: required environment variable DB_PASSWORD is not set
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;[exit 1]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In both cases the app stopped right away, with a message that says exactly what is missing, and with exit code 1. That code matters: it is what tells the deploy, the CI or the orchestrator that something went wrong, so the new version does not go live. This behavior has a name: &lt;strong&gt;fail fast&lt;/strong&gt;.&lt;/p&gt;
&lt;h2 data-hextra-search-id="the-env-file-only-for-your-machine"&gt;The .env file, only for your machine&lt;span class="hx:absolute hx:-mt-20" id="the-env-file-only-for-your-machine"&gt;&lt;/span&gt;
&lt;a href="#the-env-file-only-for-your-machine" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;Setting five or ten variables by hand every time you open a terminal gets old. For local development, the common answer is a &lt;code&gt;.env&lt;/code&gt; file at the project root, read by a library. In PHP, the most used one is &lt;a href="https://github.com/vlucas/phpdotenv" target="_blank" rel="noopener"&gt;phpdotenv&lt;/a&gt;, installed with Composer:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;composer require vlucas/phpdotenv&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The lab used it like this:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-php" data-lang="php"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="o"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="nx"&gt;php&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;require&lt;/span&gt; &lt;span class="no"&gt;__DIR__&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;/../vendor/autoload.php&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;$dotenv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Dotenv\Dotenv&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="na"&gt;createImmutable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dirname&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;__DIR__&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;$dotenv&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;safeLoad&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;$dotenv&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;DB_HOST&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;DB_PASSWORD&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;notEmpty&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;DB_HOST=&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt; &lt;span class="nv"&gt;$_SERVER&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;DB_HOST&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Three details in this code:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;createImmutable&lt;/code&gt;: &lt;code&gt;.env&lt;/code&gt; &lt;strong&gt;does not overwrite&lt;/strong&gt; a variable that already exists in the environment. The real variable always wins.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;safeLoad&lt;/code&gt;: if &lt;code&gt;.env&lt;/code&gt; does not exist, it moves on without an error. In production, it should not exist.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;required(...)-&amp;gt;notEmpty()&lt;/code&gt;: the same fail fast from the previous section, now done by the library.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The test showed all three behaviors:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ php src/04-dotenv.php
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;DB_HOST=127.0.0.1
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ DB_HOST=db.from.real.env php src/04-dotenv.php
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;DB_HOST=db.from.real.env
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ php src/04-dotenv.php # no .env and no variables
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;PHP Fatal error: Uncaught Dotenv\Exception\ValidationException: One or more
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;environment variables failed assertions: DB_HOST is missing, DB_PASSWORD is missing.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;First, the value came from &lt;code&gt;.env&lt;/code&gt;. Then the real variable beat the file. Finally, with neither, the validation stopped the app.&lt;/p&gt;
&lt;p&gt;A trap showed up along the way. The first version of the script read &lt;code&gt;$_ENV['DB_HOST']&lt;/code&gt;, and when the variable came from the real environment, this was the result:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ DB_HOST=db.from.real.env php src/04a-dotenv-env-trap.php
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;PHP Warning: Undefined array key &amp;#34;DB_HOST&amp;#34; in .../src/04a-dotenv-env-trap.php on line 9
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;DB_HOST=&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;It is &lt;code&gt;variables_order&lt;/code&gt; again: PHP did not put the real variable in &lt;code&gt;$_ENV&lt;/code&gt;, and immutable mode stopped the library from putting it there. Reading from &lt;code&gt;$_SERVER&lt;/code&gt;, as in the final version, fixed it.&lt;/p&gt;
&lt;h3 data-hextra-search-id="what-goes-to-git-and-what-does-not"&gt;What goes to Git and what does not&lt;span class="hx:absolute hx:-mt-20" id="what-goes-to-git-and-what-does-not"&gt;&lt;/span&gt;
&lt;a href="#what-goes-to-git-and-what-does-not" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;&lt;code&gt;.env&lt;/code&gt; holds real values, so it &lt;strong&gt;never&lt;/strong&gt; goes to the repository. A &lt;code&gt;.env.example&lt;/code&gt; goes instead, with the variable names and sample values, and no secrets:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# .env.example: copy to .env for local development. Never commit .env.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;DB_HOST&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;127.0.0.1
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;DB_PASSWORD&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;LOG_LEVEL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;debug&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;And &lt;code&gt;.gitignore&lt;/code&gt; takes care of the rest:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;/vendor/
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;.env&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the lab, Git confirmed it:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ git check-ignore -v .env
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;.gitignore:2:.env .env
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ git ls-files | grep -E &amp;#39;^\.env&amp;#39;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;.env.example&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;code&gt;.env.example&lt;/code&gt; is also documentation: whoever joins the project sees, in one place, every setting the app needs. Frameworks like &lt;a href="https://hyperf.wiki/3.1/#/en/config" target="_blank" rel="noopener"&gt;Hyperf&lt;/a&gt; ship with this setup ready, with a &lt;code&gt;.env&lt;/code&gt; read at startup and an &lt;code&gt;env()&lt;/code&gt; function to use in config files.&lt;/p&gt;
&lt;h2 data-hextra-search-id="per-environment-or-per-variable"&gt;Per environment or per variable?&lt;span class="hx:absolute hx:-mt-20" id="per-environment-or-per-variable"&gt;&lt;/span&gt;
&lt;a href="#per-environment-or-per-variable" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;A common way to organize config is one file per environment:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-php" data-lang="php"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="o"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="nx"&gt;php&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// config/production.php
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s1"&gt;&amp;#39;db_host&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;db.prod.internal&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s1"&gt;&amp;#39;log_level&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;info&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s1"&gt;&amp;#39;cache_ttl&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;3600&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;];&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Plus a &lt;code&gt;config/staging.php&lt;/code&gt;, and a &lt;code&gt;config/development.php&lt;/code&gt;. The app picks the file through a single variable, like &lt;code&gt;APP_ENV=production&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The original text advises against this setup, for two reasons:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;It does not scale.&lt;/strong&gt; Each new environment, like a staging deploy for one specific client, becomes another file. Over time, nobody knows which differences between them are on purpose.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;It pulls secrets into the code.&lt;/strong&gt; The production file is the natural place for someone to put the production password, and then it goes to Git.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The factor III way is &lt;strong&gt;granular&lt;/strong&gt; variables: one variable per thing (&lt;code&gt;DB_HOST&lt;/code&gt;, &lt;code&gt;LOG_LEVEL&lt;/code&gt;, &lt;code&gt;CACHE_TTL&lt;/code&gt;), independent from each other. Each deploy sets its own, with no environment name tying them together. This example is only illustrative and was not run in the lab.&lt;/p&gt;
&lt;h2 data-hextra-search-id="the-php-fpm-trap"&gt;The PHP-FPM trap&lt;span class="hx:absolute hx:-mt-20" id="the-php-fpm-trap"&gt;&lt;/span&gt;
&lt;a href="#the-php-fpm-trap" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;So far, everything ran from the command line. On the web, PHP usually runs through &lt;a href="https://www.php.net/manual/en/install.fpm.php" target="_blank" rel="noopener"&gt;PHP-FPM&lt;/a&gt;, a manager that keeps PHP processes ready to handle the requests coming from the web server. And PHP-FPM has a setting that catches many people off guard: &lt;code&gt;clear_env&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;With &lt;code&gt;clear_env = yes&lt;/code&gt;, the default, PHP-FPM &lt;strong&gt;wipes the environment variables&lt;/strong&gt; before handing the request to PHP. Your app reads &lt;code&gt;getenv('DB_HOST')&lt;/code&gt; and gets &lt;code&gt;false&lt;/code&gt;, even with the variable set on the server.&lt;/p&gt;
&lt;p&gt;The lab tested it with a real php-fpm 8.4: it started PHP-FPM with &lt;code&gt;APP_SECRET&lt;/code&gt; and &lt;code&gt;DB_HOST&lt;/code&gt; set and made a request to a page that only prints both. With the default config:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;getenv(APP_SECRET)=false
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;getenv(DB_HOST)=false&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;There are two ways out. The first is to turn the wipe off in the pool file (on Debian, &lt;code&gt;/etc/php/8.4/fpm/pool.d/www.conf&lt;/code&gt;):&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-ini" data-lang="ini"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="na"&gt;clear_env&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;no&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The second, more controlled, is to keep the wipe and pass only the variables the app needs, one by one:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-ini" data-lang="ini"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="na"&gt;env[APP_SECRET]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;$APP_SECRET&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="na"&gt;env[DB_HOST]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;$DB_HOST&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In both cases, the same request started seeing the values:&lt;/p&gt;
&lt;div class="hextra-code-block hx:relative hx:mt-6 hx:first:mt-0 hx:group/code"&gt;
&lt;div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;getenv(APP_SECRET)=&amp;#39;from-shell&amp;#39;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;getenv(DB_HOST)=&amp;#39;db.prod.internal&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hextra-code-copy-btn-container hx:opacity-0 hx:transition hx:group-hover/code:opacity-100 hx:flex hx:gap-1 hx:absolute hx:m-[11px] hx:right-0 hx:top-0"&gt;
&lt;button
class="hextra-code-copy-btn hx:group/copybtn hx:cursor-pointer hx:transition-all hx:active:opacity-50 hx:bg-primary-700/5 hx:border hx:border-black/5 hx:text-gray-600 hx:hover:text-gray-900 hx:rounded-md hx:p-1.5 hx:dark:bg-primary-300/10 hx:dark:border-white/10 hx:dark:text-gray-400 hx:dark:hover:text-gray-50"
title="Copy code"
aria-label="Copy code"
data-copied-label="Copied!"
&gt;
&lt;div class="hextra-copy-icon hx:group-[.copied]/copybtn:hidden hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;div class="hextra-success-icon hx:hidden hx:group-[.copied]/copybtn:block hx:pointer-events-none hx:h-4 hx:w-4"&gt;&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The second option is usually the better one: the app gets only what it needs, not every variable on the server. One detail: the test used a minimal pool started by hand, and the requests went straight over the FastCGI protocol with &lt;code&gt;cgi-fcgi&lt;/code&gt;, with no web server in front.&lt;/p&gt;
&lt;h2 data-hextra-search-id="and-in-production-where-do-the-values-come-from"&gt;And in production, where do the values come from?&lt;span class="hx:absolute hx:-mt-20" id="and-in-production-where-do-the-values-come-from"&gt;&lt;/span&gt;
&lt;a href="#and-in-production-where-do-the-values-come-from" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;Outside your machine, the platform where the app runs sets the variables:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;In &lt;a href="https://docs.docker.com/reference/cli/docker/container/run/#env" target="_blank" rel="noopener"&gt;Docker&lt;/a&gt;, with &lt;code&gt;-e DB_HOST=...&lt;/code&gt; or &lt;code&gt;--env-file&lt;/code&gt; when starting the container.&lt;/li&gt;
&lt;li&gt;In &lt;a href="https://kubernetes.io/docs/concepts/configuration/secret/" target="_blank" rel="noopener"&gt;Kubernetes&lt;/a&gt;, with &lt;code&gt;Secret&lt;/code&gt; and &lt;code&gt;ConfigMap&lt;/code&gt; objects, which become environment variables inside the container.&lt;/li&gt;
&lt;li&gt;On platforms like Heroku, in a dashboard or from the command line, deploy by deploy.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Docker was not available on the lab machine, so none of these examples ran here. The idea is the same in all of them: there is one image or one codebase, and the values come in from outside at run time.&lt;/p&gt;
&lt;p&gt;One last caution: an environment variable is not a vault. A forgotten &lt;code&gt;phpinfo()&lt;/code&gt; call on a public page shows all of them. Secrets do not appear in logs, error pages or &lt;code&gt;phpinfo()&lt;/code&gt;.&lt;/p&gt;
&lt;h2 data-hextra-search-id="how-to-tell-if-your-project-follows-factor-iii"&gt;How to tell if your project follows factor III&lt;span class="hx:absolute hx:-mt-20" id="how-to-tell-if-your-project-follows-factor-iii"&gt;&lt;/span&gt;
&lt;a href="#how-to-tell-if-your-project-follows-factor-iii" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;A few quick questions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Could the code be made open source today, without leaking any password?&lt;/li&gt;
&lt;li&gt;Is &lt;code&gt;.env&lt;/code&gt; in &lt;code&gt;.gitignore&lt;/code&gt;, and is there a &lt;code&gt;.env.example&lt;/code&gt; in the repository?&lt;/li&gt;
&lt;li&gt;Does the same build run in every environment, with only different variables?&lt;/li&gt;
&lt;li&gt;Does the app stop at startup, with a clear message, when a required variable is missing?&lt;/li&gt;
&lt;li&gt;Is the config made of granular variables, not files per environment?&lt;/li&gt;
&lt;li&gt;If the app runs on PHP-FPM, do the variables reach PHP (&lt;code&gt;clear_env&lt;/code&gt; or &lt;code&gt;env[...]&lt;/code&gt;)?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If any answer is &amp;ldquo;no&amp;rdquo;, that is a good place to start. And there is an ownership angle here too. When config is explicit, with a &lt;code&gt;.env.example&lt;/code&gt; that lists everything and an app that complains about what is missing, anyone on the team can bring up a new environment, rotate a leaked password or understand why production behaves differently. Config stops being a secret held by whoever did the first deploy and becomes the team&amp;rsquo;s responsibility.&lt;/p&gt;
&lt;h2 data-hextra-search-id="next-article"&gt;Next article&lt;span class="hx:absolute hx:-mt-20" id="next-article"&gt;&lt;/span&gt;
&lt;a href="#next-article" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;The next article in the series covers factor IV, &lt;strong&gt;backing services&lt;/strong&gt;: why the database, the queue and the cache should be treated as attachable resources, swapped by changing only the config.&lt;/p&gt;
&lt;h2 data-hextra-search-id="glossary"&gt;Glossary&lt;span class="hx:absolute hx:-mt-20" id="glossary"&gt;&lt;/span&gt;
&lt;a href="#glossary" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Config:&lt;/strong&gt; everything that changes from one deploy to another, like addresses, passwords and keys.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Internal config:&lt;/strong&gt; what is the same in every deploy, like routes and app rules. It stays in the code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Environment variable:&lt;/strong&gt; a name and value pair that the operating system hands to each process.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Secret:&lt;/strong&gt; config that must not leak, like a password, token or private key.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fail fast:&lt;/strong&gt; stopping right at startup, with a clear message, instead of breaking later.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;.env&lt;/code&gt;:&lt;/strong&gt; a file with variables for local development. It never goes to Git.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PHP-FPM pool:&lt;/strong&gt; a group of PHP processes with the same config, defined in files like &lt;code&gt;www.conf&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Exit code:&lt;/strong&gt; the number a program returns when it ends. Zero means success; anything else, an error.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 data-hextra-search-id="sources"&gt;Sources&lt;span class="hx:absolute hx:-mt-20" id="sources"&gt;&lt;/span&gt;
&lt;a href="#sources" class="subheading-anchor" aria-label="Permalink for this section"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;a href="https://12factor.net/config" target="_blank" rel="noopener"&gt;The Twelve-Factor App: III. Config&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.php.net/manual/en/function.getenv.php" target="_blank" rel="noopener"&gt;PHP: getenv&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.php.net/manual/en/ini.core.php#ini.variables-order" target="_blank" rel="noopener"&gt;PHP: the variables_order directive&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.php.net/manual/en/reserved.variables.environment.php" target="_blank" rel="noopener"&gt;PHP: $_ENV&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.php.net/manual/en/install.fpm.configuration.php" target="_blank" rel="noopener"&gt;PHP: PHP-FPM configuration (clear_env and env)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/vlucas/phpdotenv" target="_blank" rel="noopener"&gt;phpdotenv on GitHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://hyperf.wiki/3.1/#/en/config" target="_blank" rel="noopener"&gt;Hyperf: config and the .env file&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.docker.com/reference/cli/docker/container/run/#env" target="_blank" rel="noopener"&gt;Docker: environment variables in docker run&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://kubernetes.io/docs/concepts/configuration/secret/" target="_blank" rel="noopener"&gt;Kubernetes: Secrets&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/gitleaks/gitleaks" target="_blank" rel="noopener"&gt;gitleaks on GitHub&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item></channel></rss>