Set and add $ _ SERVER environment variables for PHP in Apache/Nginx

Source: Internet
Author: User
Tags server array
: This article describes how to set and add $ _ SERVER environment variables for apacheng.pdf for PHP. For more information about PHP tutorials, see. Requirement

In PHP development, to distinguish between online production environments and local development environments, if we can identify how good it is by judging whether $ _ SERVER ['runtime _ ENVIROMENT '] is 'dev' or 'pro, unfortunately, the $ _ SERVER array does not contain the RUNTIME_ENVIROMENT element.

I. set it through fastcgi_param of nginx

In the nginx configuration file, you can either set it in the nginx configuration file nginx. conf or in a separate website configuration environment, such as www.tomener.com. conf.

Add the corresponding configuration information in the server segment location of the configuration environment:

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}

Only fastcgi_param RUNTIME_ENVIROMENT 'pro' is added here. more values can be added later.

Restart nginx

nginx -s reload

2. set through the php main configuration file php-fpm.conf

This setting must be placed in the php-fpm.conf of the main configuration file and not in the sub-configuration file set of the include command; otherwise, an error will be reported: "Array are not allowed in the global section 」

My php-fpm.conf location in/usr/local/php/etc/php-fpm.conf

Add the following 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 two ways, we can directly get the corresponding variable value through $ _ SERVER in the PHP file.

However, it is said that the configuration information is set through fastcgi_param of nginx. when nginx interacts with php, a large amount of data is transmitted.

Set environment variables in Apache

SetEnv variable name variable value

  
       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:

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

The preceding section describes how to set and add $ _ SERVER environment variables for PHP in Apache/Nginx, including related content. if you are interested in PHP Tutorial.

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.