Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

HungryBus's avatar

Laravel deploy with releases and shared .env

Hello,

I am trying to set up a deploy for Laravel app on a server. I've decided to go with a symlinks of latest release, so this is how my server dir looks like:

/var/www/html/server/
  current -> releases/initial
  releases
    initial
      // this is where the git clone has been done
  shared
    .env // this is my .env file

To make Laravel search for the correct .env file, I've made some changes to the bootstrap/app.php file:

$app = Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        ...
    )
    ->withMiddleware(function (Middleware $middleware): void {
        //
    })
    ->withExceptions(function (Exceptions $exceptions): void {
        //
    })->create();

if (isset($_SERVER['LARAVEL_PROD']) && $_SERVER['LARAVEL_PROD'] === '1') {
    $app->useEnvironmentPath('var/www/html/server/shared/.env');
}

return $app;

I thought it would see the $_SERVER param and try the specified location for .env file, but it throws an error that it can't find the .env in the releases/initial/.env somehow.

In nginx config, there's a fastcgi param: fastcgi_param LARAVEL_PROD "1"; and nginx is reloaded and everything.

What am I doing wrong here? Is it better to specify the APP_ENV_FILE /var/www/html/server/shared/.env;, but I suspect that Laravel won't take this var into account by default

0 likes
5 replies
Glukinho's avatar

Why you can't simply create symlink releases/initial/.env -> shared/.env?

Utilizing nginx for this purpose is bad because your app isn't always accessed with HTTP. You have queued jobs, artisan commands - they aren't about http/nginx at all and still need .env to function.

1 like
Snapey's avatar

I just copy it in as part of the deployment script

jlrdw's avatar

Why do that, just setup everything normal. And I agree with @martinbean about just using actual environment variables in production.

If not careful you could be opening up major security holes.

Please or to participate in this conversation.