为PHP设置服务器(Apache/Nginx)环境变量

服务器zkbhj 发表了文章 • 0 个评论 • 948 次浏览 • 2016-08-09 10:53 • 来自相关话题

设置环境变量常见的地方为区分开发环境/生产环境,或者定义一些数据库的帐号密码。
 设置Apache环境变量
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>设置Nginx环境变量
 fastcgi_param RUNTIME_ENVIROMENT 'DEV'
fastcgi_param RUNTIME_ENVIROMENT 'DEV'; fastcgi_param MYSQL_USERNAME 'root'; fastcgi_param MYSQL_PASSWORD 'root';

fastcgi_param MYSQL_USERNAME 'root'
fastcgi_param MYSQL_PASSWORD 'root'

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中调用服务器环境变量
$env = getenv('RUNTIME_ENVIROMENT');
$env = $_SERVER['RUNTIME_ENVIROMENT']; 查看全部
设置环境变量常见的地方为区分开发环境/生产环境,或者定义一些数据库的帐号密码。
 设置Apache环境变量
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>
设置Nginx环境变量
 
fastcgi_param RUNTIME_ENVIROMENT 'DEV'

fastcgi_param RUNTIME_ENVIROMENT 'DEV'; fastcgi_param MYSQL_USERNAME 'root'; fastcgi_param MYSQL_PASSWORD 'root';

fastcgi_param MYSQL_USERNAME 'root'
fastcgi_param MYSQL_PASSWORD 'root'

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中调用服务器环境变量
$env = getenv('RUNTIME_ENVIROMENT');
$env = $_SERVER['RUNTIME_ENVIROMENT'];