1. 程式人生 > >多個laravel專案可能導致 .env檔案衝突的問題

多個laravel專案可能導致 .env檔案衝突的問題

(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() 函式