Home >Backend Development >PHP Tutorial >How to Set Environment Variables for PHP Access in Apache?

How to Set Environment Variables for PHP Access in Apache?

Barbara Streisand
Barbara StreisandOriginal
2024-10-19 18:33:301023browse

How to Set Environment Variables for PHP Access in Apache?

Set Environment Variables for PHP Access in Apache

When developing PHP web applications that rely on environment variables, it's crucial to establish these variables correctly in Apache. Let's explore how to set them and configure unique values for each domain.

For each domain, modify the corresponding virtual host configuration file located in Apache's configuration directory. Once you have found the appropriate virtual host configuration, add the following line:

SetEnv VARIABLE_NAME variable_value

Replace VARIABLE_NAME with the name of the environment variable and variable_value with its desired value.

For example, if you want to set the BASE_URL environment variable with the value "https://example.com" for the example.com domain, you would add the following line to its virtual host configuration:

SetEnv BASE_URL https://example.com

Repeat this process for each environment variable you need to define. To ensure that these variables are accessible in your PHP application, use the getenv() function as follows:

<code class="php">$baseUrl = getenv('BASE_URL');</code>

The above is the detailed content of How to Set Environment Variables for PHP Access 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