Home  >  Article  >  Operation and Maintenance  >  How to set environment variables in Apache

How to set environment variables in Apache

(*-*)浩
(*-*)浩Original
2019-11-29 14:45:284764browse

There are two environment variables that affect Apache HTTP Server.

How to set environment variables in Apache

First, there are environment variables that are controlled by the underlying operating system. These are set before the server starts. They can be used in extensions to configuration files or passed to CGI scripts and SSI using the PassEnv directive. (Recommended learning: Apache Server)

Secondly, Apache HTTP Server provides a mechanism for storing information in named variables, which are also called environment variables. This information can be used to control various operations, such as logging or access control. Variables are also used as a mechanism to communicate with external programs such as CGI scripts. This article discusses different ways of manipulating and using these variables.

Although these variables are called environment variables, they are not the same as the environment variables controlled by the underlying operating system. Instead, these variables are stored and manipulated in internal Apache structures.

They only become actual operating system environment variables when provided to CGI scripts and server-side include scripts. If you wish to manipulate the operating system environment in which the server itself is running, you must use the standard environment manipulation mechanisms provided by the operating system shell.

Set environment variables

Basic environment operations

The most basic way to set environment variables in Apache is Use the unconditional SetEnv directive. You can also use the PassEnv directive to pass variables from the shell environment that started the server.

Conditional per-request setting

To increase flexibility, mod_setenvif provides directives that allow environment variables to be set on a per-request basis, depending on Characteristics of a specific request.

For example, a variable can only be set when a request is made by a specific browser (user agent), or only when a specific Referer [sic] header is found. Greater flexibility is gained through mod_rewrite's RewriteRule, which uses the [E = ...] option to set environment variables.

Unique Identifier Finally, mod_unique_id sets the per-request environment variable UNIQUE_ID to a value that is guaranteed to be unique across "all" requests under very specific conditions.

Standard CGI Variables

In addition to all environment variables set in the Apache configuration and passed from the shell, CGI scripts and SSI pages provide a set of environment variables , which contains meta-information about the request as required by the CGI specification.

Some warnings

Standard CGI variables cannot be overridden or changed using environment manipulation directives.

When suexec is used to start a CGI script, the environment will be cleared to a set of safe variables before starting the CGI script. The list of safe variables is defined at compile time in suexec.c.

For portability reasons, environment variable names may contain only letters, numbers, and underscore characters. Also, the first character may not be a number. Characters that do not match this limit will be replaced with underscores when passed to CGI scripts and SSI pages.

One special case is HTTP headers, which are passed to CGI scripts etc. via environment variables (see below). They are converted to uppercase and only dashes are replaced with underscores; if the header contains any other (invalid) characters, the entire header is silently removed.

The SetEnv directive runs late during request processing, which means directives such as SetEnvIf and RewriteCond will not see variables set with it.

When the server looks up a path via an internal subrequest (such as looking up a DirectoryIndex or using mod_autoindex to generate a directory listing), per-request environment variables are not inherited in the subrequest. Additionally, due to the API phase in which mod_setenvif takes action, the SetEnvIf directive is not evaluated separately in subrequests.

The above is the detailed content of How to set environment variables in Apache. For more information, please follow other related articles on the PHP Chinese website!

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