#Can I host Laravel without a .env file?

12 messages · Page 1 of 1 (latest)

tepid hemlock
#

It seems I'm getting a HTTP 500 error due to: No application encryption key has been specified.

However I don't have any .env file on the production environment.

I have set APP_KEY and other related environmental variables into the env vars (thus printenv shows them), I thought this would be enough?

old salmon
#

Yes, as .env files are just replacements for environment variables proper. So if you define actual environment variables on your production server, you don’t need an .env file.

white sky
#

you can also just define your production values as the 2nd parameter of the env() functions in your config files

white sky
old salmon
#

It’s a terrible idea. You want production credentials and secrets nowhere near your source code.

white sky
# old salmon It’s a terrible idea. You want production credentials and secrets nowhere near y...

except when PHP fails to load the ENV variables on rapid concurrent requests due to the nature of the request handler - https://github.com/laravel/framework/issues/8191

GitHub

If you make requests fast enough some or all environment variables return null. To reproduce: Install fresh L5. Edit /app/Http/Controllers/WelcomeController.php Add dd(env('APP_ENV'...

old salmon
#

Run config:cache on deployment. Laravel app then never needs to call env

tepid hemlock
#

Hmm, config:cache doesn't seem to work either 🤔

Background info: I'm using a hosting service that hosts Docker via Kubernetes... so I have 3 services:

  • CLI (php-cli, main service, does composer install in the Dockerfile)
  • NGINX (nginx, exposed port, has copy of the files)
  • PHP (php-fpm, no bash available, just running PHP-FPM)

I can ssh into cli and nginx services and can confirm printenv has APP_KEY and all other related env vars that I added to the hosting environment.

So I'm not too sure why those env vars are not read for this Laravel app 🤔

#

It could be that the Dockerfile needs those env vars to be added in a different scope, they are added as runtime scope for all envs of the project (thus production, development, staging, any PR-envs I spin up etc.)

tepid hemlock
#

mmm I think I have to do these commands in post rollout task after deployments

php artisan config:cache    // flush all cached env variable
php artisan config:clear    // repopulate all the env variable
php artisan cache:clear     // flush all the cached content