Set server (Apache/Nginx) environment variables for php

Source: Internet
Author: User
The common setting of environment variables is to differentiate the development environment and production environment, or to define the account and password of some databases. the common setting of environment variables is to differentiate the development environment and production environment, or define the account and password of some databases.

Set Apache environment variables

Command

Set the current environment variable to DEV.

SetEnv RUNTIME_ENVIROMENT DEV

Database account password

SetEnv MYSQL_USERNAME root

SetEnv MYSQL_PASSWORD root

Configuration file format

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

Set Nginx environment variables

Command

Set the current environment variable to DEV.

Fastcgi_param RUNTIME_ENVIROMENT 'dev'

Database account password

Fastcgi_param MYSQL_USERNAME 'root'

Fastcgi_param MYSQL_PASSWORD 'root'

Configuration file format

Configure in the fastcgi_params file

Fastcgi_param RUNTIME_ENVIROMENT 'dev ';

Fastcgi_param MYSQL_USERNAME 'root ';

Fastcgi_param MYSQL_PASSWORD 'root ';

Configure in nginx. conf

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;    }  }

Set environment variables for PHP scripts

Temporary settings for the current user

Temporary settings only need to be executed

Export KEY = VALUE

Set permanently for the current user

In ~ /. Write in bashrc (different systems)

Set for all users (excluding root users)

Create the/etc/profile. d/test. sh file and write

KEY = VALUE

Set for all users (including root users)

Write in/etc/environment

KEY = VALUE

Note that this file takes effect at user logon, so for root, you need to restart the machine.

Set in Supervisor

Sometimes the PHP script is controlled by a Supervisor, so remember to set the environment item in the supervisor configuration.

Call server environment variables in PHP

There are two call methods in PHP:

$ Env = getenv ('runtime _ ENVIROMENT ');

There are also super global variables:

$ Env = $ _ SERVER ['runtime _ ENVIROMENT '];

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.