Laravel Environment File .env

Generate application key

Most of the time this is done via Laravel artisan command php artisan key:generate. But if you want to set it manually, you just need to fill APP_KEY inside the .env file with:
– random 32 characters or
– something with the length of 32 bytes encoded in base64.

If you choose to go with typing manually 32 characters please note that you need to (in fact these rules apply to all values in .env):
– enclose with quote if you planning to use space as part of the value or
– enclose with double-quote if you are planning to use (escaped) double-quote as part of the value.

This will write to .env:

# this will write to .env, fallback to error
php artisan key:generate

This will write to .env.example:

# this will write to .env.example, fallback to .env, fallback to error
php artisan key:generate --env example

Serving the app

Laravel artisan command

# this will load .env
php artisan serve
# this will load .env.example
php artisan serve --env example

You need to set APP_ENV=example if you are using --env example flag. Or else, everything except the APP_ENV itself will be loaded from .env. You can trick this situation by making symbolic link ln -s .env.example .env.

Apache2

Variable set in the virtual host config file takes precedence over .env file.

SetEnv APP_NAME my-app-name

Nginx

Variable set in the virtual host config file takes precedence over .env file.

fastcgi_param APP_NAME my-app-name;

Docker

To set the environment variable using key-value pair:

docker run -e APP_NAME=my-app-name -e APP_ENV=local ...

To set the environment variable using file:

docker run --env-file=.env1 --env-file=.env2 ...

Shell

To set the environment variable via shell:

export APP_NAME=my-app-name

To delete the environment variable via shell:

unset APP_NAME
set -n APP_NAME

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *