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. Set the Apache environment variable command to set the current environment variable to DEVSetEnvRUNTIME_ENVIROMENTDEV database account and password. Format: Virtu
Set common environment variables to differentiate development and production environments, you can also define the account and password of some databases. Set the Apache environment variable command to set the current environment variable to DEV SetEnv RUNTIME_ENVIROMENT DEV database account password SetEnv MYSQL_USERNAME rootSetEnv MYSQL_PASSWORD root configuration file format to u.
The common setting of environment variables is to differentiate development and production environments, or to define the account and password of some databases.
Set Apache environment variable commands
Set the current environment variableDEV
SetEnv RUNTIME_ENVIROMENT DEV
Database account password
SetEnv MYSQL_USERNAME rootSetEnv 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 variable commands
Set the current environment variableDEV
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 the environment variable for the PHP script to temporary settings for the current user
Temporary settings only need to be executed
export KEY=VALUE
Set permanently for the current user
In~/.bashrc
(Different systems have different) Write
Set for all users (excluding root users)
Create a file/etc/profile.d/test.sh
, Write
KEY=VALUE
Set for all users (including root users)
In/etc/environment
Write
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'];
Reference
- How to Set environment variables for apache and nginx
Original article address: Set the server (Apache/Nginx) environment variable for PHP. Thank you for sharing it.