Is .env file necessary on a webserver?

395 Views Asked by At

I noticed when testing around with test projects on a remote server that I was required to create an .env file.

With my non test project I'm planning to deploy I have the .env file missing but everything still works.

1

There are 1 best solutions below

0
Justin Origin Broadband On

The .env file isn't especially required by Laravel in order to function, as the config files use fallbacks or you can plumb your configuration values directly into the config files. You may notice settings have function based assginments, e.g: 'default' => env('CACHE_DRIVER', 'file'). The env() function reads from the .env file and if a value isn't present, has a default value (argument 2) which takes it's place when it's not set.

However: this is (considerably) bad practice. By default, the .env file is ignored by git and you may have noticed upon install that Laravel generates an application key for you. This is unique to the installation (but can be regenerated any time you wish using php artisan key:generate).

Laravel artisan has a handy feature that allows you to cache your configuration values into one massive file, bypassing having to read/re-read every single config file in the config directory.

php artisan config:cache

This greatly reduces config file parse time and therefore aids in speeding up application boot time. Being able to modify all of your settings through the .env file and then running the above command is considerably faster than editing each of the settings files.

TLDR; if you're working with other team members or have to deploy to different environments, use the .env file. If not, do it for the convenience. Either way, it's not essential, but saves headaches.

EDIT

Laravel requires an application key in order to use certain functionality

I thought it would be a good idea to back up my answer with research: https://laracasts.com/discuss/channels/general-discussion/app-key?page=1

It's better to store it in the .env file. If there's ever a suspected breach of security, the application key can be regenerated quickly.