Requirements
<code>在PHP开发中为了区分线上生产环境还是本地开发环境, 如果我们能通过判断$_SERVER['RUNTIME_ENVIROMENT']为 'DEV'还是'PRO'来区分该多好, 可惜的是$_SERVER数组里面根本没有RUNTIME_ENVIROMENT这个元素。 </code>
1. Set it through nginx’s fastcgi_param
In the nginx configuration file, it can be set in the nginx overall configuration file nginx.conf, or in a separate website configuration environment, such as: www.tomener .com.conf
Add the corresponding configuration information in the configuration environment server section location:
<code>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 }</code>
Only the fastcgi_param RUNTIME_ENVIROMENT 'PRO' value is added here, more can be added later
Then restart nginx
<code>nginx -s reload</code>
2 , set through the main php configuration file php-fpm.conf
This setting must be placed in the main configuration file php-fpm.conf and cannot be placed in the sub-configuration file set by the include directive, otherwise an error will be reported: "Array are not allowed in the global section"
My php-fpm.conf location is in /usr/local/php/etc/php-fpm.conf
Add it directly in the configuration file:
<code>env[RUNTIME_ENVIROMENT] = 'PRO' </code>
Restart php-fpm after adding it
<code>service restart php-fpm</code>
After adding the $_SERVER variable value through the above two methods, we can directly obtain the corresponding variable value through $_SERVER in the php file.
However, it is said that if the configuration information is set through nginx's fastcgi_param, when nginx interacts with php, a large amount of data will be transmitted.
Apache sets environment variables
<code>SetEnv 变量名 变量值</code>
<code><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></code>
Reference documentation:
<code>http://man.chinaunix.net/newsoft/ApacheManual/mod/mod_env.html#setenv</code>
The above introduces how to set and add the $_SERVER server environment variable for PHP by Apache/Nginx, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.