Home > Backend Development > PHP Tutorial > Set server Apache/Nginx environment variables for PHP under win7 64 apache apache struts2 apache

Set server Apache/Nginx environment variables for PHP under win7 64 apache apache struts2 apache

WBOY
Release: 2016-07-29 08:50:04
Original
1108 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
Copy after login

Database account and password

SetEnv MYSQL_USERNAME root
SetEnv MYSQL_PASSWORD root
Copy after login

Configuration file format

<VirtualHost *:80>
    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
</VirtualHost>
Copy after login

Set Nginx environment variables

Command

Set the current environment variable to DEV

fastcgi_param RUNTIME_ENVIROMENT 'DEV'
Copy after login

Database account password

fastcgi_param MYSQL_USERNAME 'root'
fastcgi_param MYSQL_PASSWORD 'root'
Copy after login

Configuration file format

Configure in the fastcgi_params file

fastcgi_param RUNTIME_ENVIROMENT 'DEV';
fastcgi_param MYSQL_USERNAME 'root';
fastcgi_param MYSQL_PASSWORD 'root';
Copy after login

in nginx.conf 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

Set environment variables for PHP scripts

Temporarily set for the current user

Temporary settings only need to execute

export KEY=VALUE
Copy after login

Set permanently for the current user

Write in ~/.bashrc (different systems vary)

Settings

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

KEY=VALUE
Copy after login
Copy after login

Settings

for all users (including root) in /etc/environment Write to

KEY=VALUE
Copy after login
Copy after login

Note that the effective time of this file is when the user logs 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');
Copy after login

There is also a super global variable method:

$env = $_SERVER['RUNTIME_ENVIROMENT'];
Copy after login

The above introduces how to set server Apache/Nginx environment variables for PHP, including Apache and nginx content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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