Apache/nginx setting, adding $_server Server environment variables for PHP

Source: Internet
Author: User
Demand

在PHP开发中为了区分线上生产环境还是本地开发环境,如果我们能通过判断$_SERVER['RUNTIME_ENVIROMENT']为 'DEV'还是'PRO'来区分该多好,可惜的是$_SERVER数组里面根本没有RUNTIME_ENVIROMENT这个元素。

First, through the Nginx Fastcgi_param to set

In the Nginx configuration file, it can be set in the Nginx profile nginx.conf, or in a separate site configuration environment, such as: www.tomener.com.conf

Add the appropriate configuration information in the Configure Environment Server segment location:

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}

Here only added Fastcgi_param runtime_enviroment ' PRO ' a value, more can be added in the back

Then reboot and restart Nginx.

nginx -s reload

Second, through the PHP main configuration file php-fpm.conf to set

This setting must be placed in the main configuration file php-fpm.conf, can not be placed in the include directive settings sub-configuration file, or will be error: "array is not allowed in the global section"

My php-fpm.conf position is in/usr/local/php/etc/php-fpm.conf.

Add directly to the configuration file:

env[RUNTIME_ENVIROMENT] = 'PRO'

Restart PHP-FPM after adding

service restart php-fpm

After adding the $_server variable value in the above 2 ways, we can get the corresponding variable value directly in the PHP file by $_server.

However, it is said that the configuration information through the Nginx Fastcgi_param to set, when Nginx and PHP interaction, will bring a lot of data transfer.

Apache Set Environment variables

SetEnv 变量名 变量值

  
   
    
       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    
   
    
     
            Options Indexes FollowSymLinks        AllowOverride All        Require all granted    
   
    
  
   

Reference Documentation:

http://man.chinaunix.net/newsoft/ApacheManual/mod/mod_env.html#setenv

The above describes the Apache/nginx for PHP settings, add $_server server environment variables, including the aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.