Set server (Apache/Nginx) environment variables for php

伊谢尔伦
Release: 2016-11-29 11:50:07
Original
1404 people have browsed it

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

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

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;
    }  
}
Copy after login

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'] ;🎜🎜 🎜🎜🎜🎜🎜🎜
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!