Setting server (APACHE/NGINX) environment variables for PHP

Source: Internet
Author: User

Setting server (APACHE/NGINX) environment variables for PHP

Setting environment variables common places to differentiate a development environment/production environment, or to define account passwords for some databases

Setting Apache Environment variablesInstructions

Set current environment variable to dev

SETENV runtime_enviroment DEV

Database account password

SetEnv mysql_username rootsetenv Mysql_password Root
Configuration file format
<virtualhost *:80>    ServerAdmin [email protected]    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</virtualhost>
Setting Nginx Environment variablesInstructions

Set 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

Configuring in the Fastcgi_params file

Fastcgi_param runtime_enviroment ' DEV '; fastcgi_param mysql_username ' root '; Fastcgi_param mysql_password ' root ';

Configuring in Nginx.conf

server {    listen   ;     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;    }   }
Setting environment variables for PHP scriptsTemporarily set for the current user

Temporary settings only need to be performed

Export Key=value
Permanently set for the current user

Written in ~/.BASHRC (different systems vary)

Set for all users (not including root)

Create File/etc/profile.d/test.sh, write

Key=value
Set for all users (including root)

Write in/etc/environment

Key=value

Note that this file's effective time is when the user logs in, so for root, the machine needs to be restarted

Set in supervisor

Sometimes PHP scripts are controlled with supervisor, so remember to set the environment in the supervisor configuration

Invoking server environment variables in PHP

There are two ways to call in PHP:

$env = getenv (' runtime_enviroment ');

There are also hyper-global variable modes:

$env = $_server[' runtime_enviroment ');

Setting server (APACHE/NGINX) environment variables for PHP

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.