在PHP开发中为了区分线上生产环境仍是本地开发环境,
若是咱们能经过判断$_SERVER['RUNTIME_ENVIROMENT']为 'DEV'仍是'PRO'来区分该多好, 惋惜的是$_SERVER数组里面根本没有RUNTIME_ENVIROMENT这个元素。
在nginx配置文件中,能够在nginx整体的配置文件nginx.conf中,也能够在单独的网站配置环境中进行设置,如:www.tomener.com.confphp
在配置环境server段location中添加相应的配置信息:html
location ~ \.php($|/) { try_files $uri =404; fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param RUNTIME_ENVIROMENT 'PRO'; # PRO or DEV }
这里只添加了fastcgi_param RUNTIME_ENVIROMENT 'PRO'一个值,更多能够添加在后面nginx
而后重启重启nginxweb
nginx -s reload
这个设置必须放在主配置文件php-fpm.conf里,不能放到include指令设置的子配置文件里,不然会报错:「Array are not allowed in the global section」apache
个人php-fpm.conf位置在/usr/local/php/etc/php-fpm.conf数组
直接在配置文件中添加:bash
env[RUNTIME_ENVIROMENT] = 'PRO'
添加后重启php-fpmide
service restart php-fpm
经过上面2种方式添加$_SERVER变量值后,咱们就能够直接在php文件中经过$_SERVER来获取相应的变量值了。php-fpm
不过听说配置信息经过nginx的fastcgi_param来设置的话,当nginx和php交互时,会带来大量的数据传输。网站
SetEnv 变量名 变量值
<VirtualHost *:80> ServerAdmin webmaster@demo.com DocumentRoot "e:\wwwroot\demo" ServerName my.demo.com ErrorLog "logs/my.demo.com-error.log" CustomLog "logs/my.demo.com-access.log" common SetEnv RUNTIME_ENVIROMENT DEV <Directory "e:\wwwroot\demo"> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost>
参考文档:
http://man.chinaunix.net/newsoft/ApacheManual/mod/mod_env.html#setenv