Home >Backend Development >PHP Tutorial >Set server (Apache/Nginx) environment variables for php

Set server (Apache/Nginx) environment variables for php

伊谢尔伦
伊谢尔伦Original
2016-11-29 11:50:071500browse

A common place to set environment variables is to distinguish between development environment/production environment, or to define some database accounts and passwords

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

fe9869201ab8e8d7bca10d8d2b16f174

Set Nginx environment variable

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

Configured in the fastcgi_params file

fastcgi_param RUNTIME_ENVIROMENT 'DEV';

fastcgi_param MYSQL_USERNAME 'root';

fastcgi _param MYSQL_PASSWORD 'root';

Configure

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

in nginx.conf

Set environment variables for PHP scripts

Temporary settings for the current user

Temporary settings only need to be executed

export KEY=VALUE

is current Permanent user settings

Write in ~/.bashrc (different systems vary)

Set

for all users (excluding root) Create the file /etc/profile.d/test.sh and write

AKey = value

Set

Key = Value

When logging in, so for root, you need to restart the machine

Set in Supervisor

Sometimes PHP scripts are controlled by Supervisor, so remember to set the environment item in the supervisor configuration

Call the server environment variable in PHP

There are two calling methods in PHP:

$env = getenv('RUNTIME_ENVIROMENT');

There is also a super global variable method:

🎜🎜🎜$env = $_SERVER['RUNTIME_ENVIROMENT'] ;🎜🎜 🎜🎜🎜🎜🎜🎜
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Ajax cachingNext article:Ajax caching