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. Set the Apache environment variable command to set the current environment variable to DEVSetEnvRUNTIME_ENVIROMENTDEV database account and password. Format: Virtu

Set common environment variables to differentiate development and production environments, you can also define the account and password of some databases. Set the Apache environment variable command to set the current environment variable to DEV SetEnv RUNTIME_ENVIROMENT DEV database account password SetEnv MYSQL_USERNAME rootSetEnv MYSQL_PASSWORD root configuration file format to u.

The common setting of environment variables is to differentiate development and production environments, or to define the account and password of some databases.

Set Apache environment variable commands

Set the current environment variableDEV

SetEnv RUNTIME_ENVIROMENT DEV

Database account password

SetEnv MYSQL_USERNAME rootSetEnv 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 variable commands

Set the current environment variableDEV

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 the environment variable for the PHP script to temporary settings for the current user

Temporary settings only need to be executed

export KEY=VALUE
Set permanently for the current user

In~/.bashrc(Different systems have different) Write

Set for all users (excluding root users)

Create a file/etc/profile.d/test.sh, Write

KEY=VALUE
Set for all users (including root users)

In/etc/environmentWrite

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'];
Reference
  1. How to Set environment variables for apache and nginx

Original article address: Set the server (Apache/Nginx) environment variable for PHP. Thank you for sharing it.

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.