Sander Visser Sander Visser - 2 years ago 324
PHP Question

dotenv requires .env file on production

I'm using dotenv for php to manage the environment settings (not lavarel but I tagged it because lavarel also uses dotenv)

I have excluded the .env from the code base and I have added the .env.example for all other collaborators

On the github page of dotenv:

phpdotenv is made for development environments, and generally should not be used in production. In production, the actual environment variables should be set so that there is no overhead of loading the .env file on each request. This can be achieved via an automated deployment process with tools like Vagrant, chef, or Puppet, or can be set manually with cloud hosts like Pagodabox and Heroku.

The thing that I don't understand is that I get the following exception:

PHP Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Dotenv: Environment file .env not found or not readable.

This contradicts with what documentation says "the actual environment variables should be set so that there is no overhead of loading the .env file on each request."

So the questions is if there's any reason why dotenv throws that exception and/or am I missing something?

I can easily work arround this, the not so nice solution:

if(getenv('APPLICATION_ENV') !== 'production') { /* or staging */
$dotenv = new Dotenv\Dotenv(__DIR__);

Nicest solution in my opinion, but I think dotenv should handle this.

$dotenv = new Dotenv\Dotenv(__DIR__);
//Check if file exists the same way as dotenv does it
//See classes DotEnv\DotEnv and DotEnv\Loader
//$filePath = $dotenv->getFilePath(__DIR__);
//This method is protected so extract code from method (see below)

//both calls are cached so (allmost) no performance loss
if(is_file($filePath) && is_readable($filePath)) {

Answer Source

Dotenv was built around an idea, that it will be used in development environments only. Thus, it always expects .env file to be present.

The solution you didn't like is a recommended way to use Dotenv. And it seems, that it won't change in near future. Related discussion in project's issue tracker:

Note, that Mark offers there a good approach for production/staging environments, which skips file loading, but not validation

$dotenv = new Dotenv\Dotenv();
if(getenv('APP_ENV') === 'development') {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download