The common setting of environment variables is to differentiate the development environment and production environment, or to define the account and password of some databases. the common setting of environment variables is to differentiate the development environment and production environment, or define the account and password of some databases.
Set Apache environment variables
Command
Set the current environment variable to DEV.
SetEnv RUNTIME_ENVIROMENT DEV
Database account password
SetEnv MYSQL_USERNAME root
SetEnv MYSQL_PASSWORD root
Configuration file format
ServerAdmin admin@admin.com
DocumentRoot "/var/www /"
ServerName localhost
SetEnv RUNTIME_ENVIROMENT DEV
SetEnv MYSQL_USERNAME root
SetEnv MYSQL_PASSWORD root
ErrorLog "logs/error. log"
CustomLog "logs/access. log" common
Set Nginx environment variables
Command
Set the current environment variable to DEV.
Fastcgi_param RUNTIME_ENVIROMENT 'dev'
Database account password
Fastcgi_param MYSQL_USERNAME 'root'
Fastcgi_param MYSQL_PASSWORD 'root'
Configuration file format
Configure in the fastcgi_params file
Fastcgi_param RUNTIME_ENVIROMENT 'dev ';
Fastcgi_param MYSQL_USERNAME 'root ';
Fastcgi_param MYSQL_PASSWORD 'root ';
Configure in nginx. conf
server { listen 80; root /var/www; index index.php; server_name localhost; location / { index index.php; } location ~ .*\.(php|php5)?$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; } }
Set environment variables for PHP scripts
Temporary settings for the current user
Temporary settings only need to be executed
Export KEY = VALUE
Set permanently for the current user
In ~ /. Write in bashrc (different systems)
Set for all users (excluding root users)
Create the/etc/profile. d/test. sh file and write
KEY = VALUE
Set for all users (including root users)
Write in/etc/environment
KEY = VALUE
Note that this file takes effect at user logon, so for root, you need to restart the machine.
Set in Supervisor
Sometimes the PHP script is controlled by a Supervisor, so remember to set the environment item in the supervisor configuration.
Call server environment variables in PHP
There are two call methods in PHP:
$ Env = getenv ('runtime _ ENVIROMENT ');
There are also super global variables:
$ Env = $ _ SERVER ['runtime _ ENVIROMENT '];