設置環境變量常見的地方爲區分開發環境/生產環境,或者定義一些數據庫的賬號密碼php
設置當前環境變量爲DEV
nginx
SetEnv RUNTIME_ENVIROMENT DEV
數據庫賬號密碼數據庫
SetEnv MYSQL_USERNAME root
SetEnv MYSQL_PASSWORD root
<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>
指令服務器
設置當前環境變量爲DEV
spa
fastcgi_param RUNTIME_ENVIROMENT 'DEV'
數據庫賬號密碼code
fastcgi_param MYSQL_USERNAME 'root' fastcgi_param MYSQL_PASSWORD 'root'
在fastcgi_params文件中配置orm
fastcgi_param RUNTIME_ENVIROMENT 'DEV'; fastcgi_param MYSQL_USERNAME 'root'; fastcgi_param MYSQL_PASSWORD 'root';
在nginx.conf中配置server
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; } }
在PHP中有兩個調用方式:blog
$env = getenv('RUNTIME_ENVIROMENT');
還有超全局變量方式:ci
$env = $_SERVER['RUNTIME_ENVIROMENT'];