多个laravel项目可能导致 .env文件冲突的问题

版权声明: https://blog.csdn.net/qq_29785317/article/details/81068733

(When you have two or more sites on one server, the sites’s .env will affect each other)

这个问题多半发生在 Apache上,因为加载.env文件的时候用了 getenv() 和 setenv() 。
这两个函数不是线程安全的,它会将.env文件里面的值设置在进程级别的环境中(process-wide variable)
Apache 使用线程处理请求,当一个进程同时有几个线程的时候,就比较容易发生 ‘串值’ 的情况

例子:串库

不过这种情况在php-fpm+nginx上面好像并不怎么会受影响(我搜索相关问题时,问题提到环境都是 apache,没有找到nginx的案例),因为nginx使用进程来处理php请求,进程间相互独立,所以就算getenv()/setenv() 将变量设置在进程环境中,也没有太大影响,不会 ‘串值’

参考 :这个

This issue is related to a webserver hosting several requests within the same process. This causes problems with other things too, like setlocale(). Other configurations, like nginx + php-fpm, is not affected by this problem since it executes one process per request.

另外很多人建议由于getenv()/setenv()不是线程安全的原因,生产环境不要使用.env文件,对于laravel的应用来说,应该对配置文件进行缓存来代替,执行 php artisan config:cache 来进行生产,尽量避免使用 env() 函数

猜你喜欢

转载自blog.csdn.net/qq_29785317/article/details/81068733