My nginx configuration is as follows

location ~* \.(php)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } 

fastcgi_params has a DB_PASSWORD variable. And I can get the value of this variable by running a php script in the browser through $_SERVER['DB_PASSWORD'] , but if I run a console script, for example

 ./yii migrate/up 

Then these variables $_SERVER no longer contains. How to send them there? I use php-fpm

    1 answer 1

    It is impossible to forward fastcgi_params to the console script.

    FastCGI - interface between a web server and PHP. The path is as follows: variable from the config => protocol FastCGI => PHP interpreter parses the request by the protocol (see also variables_order ) => global variable in your PHP script.

    The console script runs the CLI SAPI interpreter ( php cli ). FastCGI is not used here, since it is started from the command line, and the web server does not take any part. Also, php cli has its own php.ini file, so its configuration is separate. You can learn it like this: php --ini

    If you need variables in php-cli - use the environment.

    1 way - via the command line (my choice)

     DB_PASSWORD=password php {ваш скрипт}.php 

    2 way - export to the environment

     export DB_PASSWORD=password php {ваш скрипт}.php 

    3 way - set at the level of the entire OS (not recommended)

    You need to edit /etc/environment , the changes will be visible after restarting the service (for the user - to re-login). To transfer a password, this method is bad for security: visible to all.

    4 way - for Docker

    If you use Docker, in Dockerfile it is possible to write ENV DB_PASSWORD=password , or transfer from the command line at the start:

     docker run ... -e DB_PASSWORD=password ... 

    PHP access

    Inside the script will be available:

     var_dump($_SERVER['DB_PASSWORD']); var_dump(getenv('DB_PASSWORD')); 

    Ps. You can find out which SAPI is used through php_sapi_name () .