如何在php-fpm中读取系统环境变量?

时间:2019-03-19 14:37:00

标签: php nginx environment-variables

Nginx + php-fpm + yii

我的系统环境变量带有数据库连接字符串

$ cat '~/.bashrc'
export DB_CONNECTION="connection_string"

在php-fpm配置中设置

$ cat /etc/php/7.2/fpm/pool.d/www.conf
env[DB_CONNECTION] = $DB_CONNECTION

尝试在我的PHP代码中使用

<?php
var_dump(getenv('DB_CONNECTION'));

知道了

string(0) ""

如果我在php-fpm配置中对其进行硬编码

$ cat /etc/php/7.2/fpm/pool.d/www.conf
env[DB_CONNECTION] = "connection_string"

有效:

<?php
var_dump(getenv('DB_CONNECTION'));
string(17) "connection_string"

如何使php-fpm读取实际的系统环境变量?

1 个答案:

答案 0 :(得分:0)

Why don't you use a PHP Package to handle your environment variables? Laravel uses https://github.com/vlucas/phpdotenv

I suggest you use that, and since you are using Yii that means you have composer installed.

composer require vlucas/phpdotenv