Framework: Problem with .env when using multiple Laravel sites in a same apache Server

Created on 29 Jun 2017  路  2Comments  路  Source: laravel/framework

  • Laravel Version: 5.2
  • PHP Version: 5.6.30
  • Database Driver & Version: mysql

Description:

From this I see init constructor of Dotenv and use load() function, So, all keys of .env file will set to immutable.
Please check this from Loader of Dotenv. When environment variable is immutable and it was existed we will ignore it.

My Problem:

When setup multiple Laravel sites in a same Apache server. In same request, they can read env value of each others (same request mean we access to A site from browser, A site make a http request to B site (using curl) and A site display response of B site to web browser).

I think the main reason related to putenv of Dotenv Please check this
Php putenv ref

I can bypass this issue by changing here, from load() function to overload() function to make variable become mutable. But I don't think this is safe solution.

Anybody meet the same situation? Please give me an advise!
Thanks

Most helpful comment

You should be running with a cached configuration to prevent these kinds of issues. Environment variables defined by php and not Apache (or FPM) can have issues like this.

There are two things to make sure of here:

  1. Only useenv() in your config/*.php files. If you need to use it elsewhere add a property to the appropriate config file (or create a new one to put it in if necessary) and reference it with the config() function.
  2. When deploying your code you should run php artisan config:cache to ensure that the env vars are cached. When you do this Laravel will not read from the .env or config/*.php files, and instead used a cached config file. Because of this, referencing those environment variables with env() will return null. You should run the php artisan config:cache command any time you deploy code or make changes to your environment variables.

All 2 comments

You should be running with a cached configuration to prevent these kinds of issues. Environment variables defined by php and not Apache (or FPM) can have issues like this.

There are two things to make sure of here:

  1. Only useenv() in your config/*.php files. If you need to use it elsewhere add a property to the appropriate config file (or create a new one to put it in if necessary) and reference it with the config() function.
  2. When deploying your code you should run php artisan config:cache to ensure that the env vars are cached. When you do this Laravel will not read from the .env or config/*.php files, and instead used a cached config file. Because of this, referencing those environment variables with env() will return null. You should run the php artisan config:cache command any time you deploy code or make changes to your environment variables.

Please continue the discussion on the forums. The repo is for bugs in the core only.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Fuzzyma picture Fuzzyma  路  3Comments

RomainSauvaire picture RomainSauvaire  路  3Comments

Anahkiasen picture Anahkiasen  路  3Comments

felixsanz picture felixsanz  路  3Comments

ghost picture ghost  路  3Comments