Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

QuangPB's avatar

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

  • 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

0 likes
2 replies
Cronix's avatar
Cronix
Best Answer
Level 67

Have you tried caching the config (artisan config:cache)? Once cached, laravel will no longer read from .env and will read from the cache instead. If you make any changes to .env you'd have to clear the cache (artisan config:clear)

QuangPB's avatar

Thank you @Cronix. It's worked :D

It merge all .env into config app. I think Laravel document should highlight this note because Dotenv using putenv function to put all .env variable to server environment variable check this and this

putenv function make sites in the same Apache Server can getenv of each others.

Please or to participate in this conversation.