Setting server (APACHE/NGINX) environment variables for PHP
Setting environment variables common places to differentiate a development environment/production environment, or to define account passwords for some databases
Setting Apache Environment variablesInstructions
Set current environment variable to dev
SETENV runtime_enviroment DEV
Database account password
SetEnv mysql_username rootsetenv Mysql_password Root
Configuration file format
<virtualhost *:80> ServerAdmin [email protected] 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</virtualhost>
Setting Nginx Environment variablesInstructions
Set 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
Configuring in the Fastcgi_params file
Fastcgi_param runtime_enviroment ' DEV '; fastcgi_param mysql_username ' root '; Fastcgi_param mysql_password ' root ';
Configuring in Nginx.conf
server { listen ; 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; } }
Setting environment variables for PHP scriptsTemporarily set for the current user
Temporary settings only need to be performed
Export Key=value
Permanently set for the current user
Written in ~/.BASHRC (different systems vary)
Set for all users (not including root)
Create File/etc/profile.d/test.sh, write
Key=value
Set for all users (including root)
Write in/etc/environment
Key=value
Note that this file's effective time is when the user logs in, so for root, the machine needs to be restarted
Set in supervisor
Sometimes PHP scripts are controlled with supervisor, so remember to set the environment in the supervisor configuration
Invoking server environment variables in PHP
There are two ways to call in PHP:
$env = getenv (' runtime_enviroment ');
There are also hyper-global variable modes:
$env = $_server[' runtime_enviroment ');
Setting server (APACHE/NGINX) environment variables for PHP