PHP .env 多环境部署
<p> 在 Laravel 中,可以通过
<code>.env 文件中的</code>
<code>APP_ENV 来区分不同的环境。虽然 Laravel 没有直接类似于 Spring Boot 的多环境配置文件(如</code>
<code>application-dev.yml 和</code>
<code>application-prod.yml),但可以使用</code>
<code>.env 文件以及一些环境管理技巧来实现相似的效果。</code> </p><h3>
实现步骤</h3>
<ol>
-
<p><strong>创建多个环境配置文件
</strong>: Laravel 默认加载项目根目录下的
<code>.env文件,你可以为不同环境创建多个</code>
<code>.env文件,如:</code>-
<ul><li><code>.env.local</code></li> -
.env.testing</code></li> -
.env.production</code></li>
</p><li><code><li><code></ul> -
-
<li><p><strong>切换环境
</strong>: 在部署时将所需环境的配置文件内容复制到
<code>.env中,或使用特定的</code>
<code>.env文件替换当前的</code>
<code>.env文件。可以通过设置</code>
<code>APP_ENV变量来指定环境类型。例如:</code>env</p><preclass="!overflow-visible"><div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950"><div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary">env</div><div class="sticky top-9 md:top-[5.75rem]"><div class="absolute bottom-0 right-2 flex h-9 items-center"><div class="flex items-center rounded bg-token-sidebar-surface-primary px-2 font-sans text-xs text-token-text-secondary dark:bg-token-main-surface-secondary"><span data-state="closed" class=""><button class="flex gap-1 items-center py-1"><svg class="icon-sm" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" height="24" width="24"><path fill="currentColor" d="M7 5C7 3.34315 8.34315 2 10 2H19C20.6569 2 22 3.34315 22 5V14C22 15.6569 20.6569 17 19 17H17V19C17 20.6569 15.6569 22 14 22H5C3.34315 22 2 20.6569 2 19V10C2 8.34315 3.34315 7 5 7H7V5ZM9 7H14C15.6569 7 17 8.34315 17 10V15H19C19.5523 15 20 14.5523 20 14V5C20 4.44772 19.5523 4 19 4H10C9.44772 4 9 4.44772 9 5V7ZM5 9C4.44772 9 4 9.44772 4 10V19C4 19.5523 4.44772 20 5 20H14C14.5523 20 15 19.5523 15 19V10C15 9.44772 14.5523 9 14 9H5Z" clip-rule="evenodd" fill-rule="evenodd"></path></svg>复制代码</button></span></div></div></div><divdir="ltr" class="overflow-y-auto p-4"><code class="!whitespace-pre hljs language-env">APP_ENV=local
APP_ENV=production</code></div></div></pre>
</li> -
<li><p><strong>自动加载特定的 .env 文件
</strong>: 若需要动态加载不同环境的
<code>.env文件,可以在</code>
<code>bootstrap/app.php中配置加载逻辑。例如:</code>php</p><preclass="!overflow-visible"><div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950"><div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary">php</div><div class="sticky top-9 md:top-[5.75rem]"><div class="absolute bottom-0 right-2 flex h-9 items-center"><div class="flex items-center rounded bg-token-sidebar-surface-primary px-2 font-sans text-xs text-token-text-secondary dark:bg-token-main-surface-secondary"><span data-state="closed" class=""><button class="flex gap-1 items-center py-1"><svg class="icon-sm" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" height="24" width="24"><path fill="currentColor" d="M7 5C7 3.34315 8.34315 2 10 2H19C20.6569 2 22 3.34315 22 5V14C22 15.6569 20.6569 17 19 17H17V19C17 20.6569 15.6569 22 14 22H5C3.34315 22 2 20.6569 2 19V10C2 8.34315 3.34315 7 5 7H7V5ZM9 7H14C15.6569 7 17 8.34315 17 10V15H19C19.5523 15 20 14.5523 20 14V5C20 4.44772 19.5523 4 19 4H10C9.44772 4 9 4.44772 9 5V7ZM5 9C4.44772 9 4 9.44772 4 10V19C4 19.5523 4.44772 20 5 20H14C14.5523 20 15 19.5523 15 19V10C15 9.44772 14.5523 9 14 9H5Z" clip-rule="evenodd" fill-rule="evenodd"></path></svg>复制代码</button></span></div></div></div><divdir="ltr" class="overflow-y-auto p-4"><code class="!whitespace-pre hljs language-php"><span class="hljs-variable">$app</span>=<spannewclass="hljs-keyword">new</span> <span class="hljs-title class_">Illuminate\Foundation\Application</span>(
Application(<span class="hljs-variable">$_ENV</span>[<span class="hljs-string">_ENV['APP_BASE_PATH'</span>] ??<spandirname(__DIR__)class="hljs-title function_ invoke__">dirname</span>(<span class="hljs-keyword">__DIR__</span>)
);<span class="hljs-variable">$appEnv</span>=<span class="hljs-variable">$_SERVER</span>[<span class="hljs-string">_SERVER['APP_ENV'</span>] ??<span class="hljs-string">'local'</span>;<spanclass="hljs-variable">$dotenv</span>=<span class="hljs-title class_">Dotenv\Dotenv</span>Dotenv:::<span class="hljs-title function_ invoke__">createMutable</span>(<span class="hljs-title function_ invoke__">base_path</span>(createMutable(base_path(),<span class="hljs-string">".env.<span class="hljs-subst">{$appEnv}</span>"</span>);<spanclass="hljs-variable"$dotenv->$dotenv</span>-><span class="hljs-title function_ invoke__">safeLoad</span>(safeLoad();</code></div></div></pre>
</li> -
<li><p><strong>使用环境变量
</strong>: 使用
<code>env('APP_ENV')来检测当前环境,例如,在代码中区分生产和测试环境:</code>php</p><preclass="!overflow-visible"><div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950"><div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary">php</div><div class="sticky top-9 md:top-[5.75rem]"><div class="absolute bottom-0 right-2 flex h-9 items-center"><div class="flex items-center rounded bg-token-sidebar-surface-primary px-2 font-sans text-xs text-token-text-secondary dark:bg-token-main-surface-secondary"><span data-state="closed" class=""><button class="flex gap-1 items-center py-1"><svg class="icon-sm" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" height="24" width="24"><path fill="currentColor" d="M7 5C7 3.34315 8.34315 2 10 2H19C20.6569 2 22 3.34315 22 5V14C22 15.6569 20.6569 17 19 17H17V19C17 20.6569 15.6569 22 14 22H5C3.34315 22 2 20.6569 2 19V10C2 8.34315 3.34315 7 5 7H7V5ZM9 7H14C15.6569 7 17 8.34315 17 10V15H19C19.5523 15 20 14.5523 20 14V5C20 4.44772 19.5523 4 19 4H10C9.44772 4 9 4.44772 9 5V7ZM5 9C4.44772 9 4 9.44772 4 10V19C4 19.5523 4.44772 20 5 20H14C14.5523 20 15 19.5523 15 19V10C15 9.44772 14.5523 9 14 9H5Z" clip-rule="evenodd" fill-rule="evenodd"></path></svg>复制代码</button></span></div></div></div><divdir="ltr" class="overflow-y-auto p-4"><code class="!whitespace-pre hljs language-php"><span class="hljs-keyword">if</span>(<span class="hljs-title function_ invoke__">env</span>(<span class="hljs-string">env('APP_ENV'</span>) ===<span class="hljs-string">'production'</span>) {
<span class="hljs-comment">// 生产环境的配置</span>
}<span class="hljs-keyword">else</span>{
<span class="hljs-comment">// 非生产环境的配置</span>
}</code></div></div></pre>
</li></ol><h3>
自动化的环境切换</h3>
<p>
可以在部署时通过脚本或 CI/CD 管理工具(如 Jenkins、GitLab CI)动态切换到对应的
<code>.env 文件,或者指定环境变量</code>
<code>APP_ENV 来自动匹配相应的配置文件。</code> </p>