A common place to set environment variables is to distinguish between development environment/production environment, or to define some database accounts and passwords
Set Apache environment variables
Command
Set the current environment variable to DEV
SetEnv RUNTIME_ENVIROMENT DEV
Database account and password
SetEnv MYSQL_USERNAME root SetEnv MYSQL_PASSWORD root
Configuration file format
<VirtualHost *:80> 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 </VirtualHost>
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';
in nginx.conf Configure
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
Temporarily set for the current user
Temporary settings only need to execute
export KEY=VALUE
Set permanently for the current user
Write in ~/.bashrc
(different systems vary)
Settings
for all users (excluding root) Create file /etc/profile.d/test.sh
and write
KEY=VALUE
Settings
for all users (including root) in /etc/environment
Write to
KEY=VALUE
Note that the effective time of this file is when the user logs in, so for root, you need to restart the machine
Set in Supervisor
Sometimes PHP scripts are controlled by Supervisor, so Remember to set the environment item in the supervisor configuration
Call the server environment variable in PHP
There are two calling methods in PHP:
$env = getenv('RUNTIME_ENVIROMENT');
There is also a super global variable method:
$env = $_SERVER['RUNTIME_ENVIROMENT'];
The above introduces how to set server Apache/Nginx environment variables for PHP, including Apache and nginx content. I hope it will be helpful to friends who are interested in PHP tutorials.