Setting environment variables for accessing in PHP when using Apache

Setting environment variables for accessing in PHP when using Apache

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Setting Environment Variables for PHP on Apache with Domain Isolation

When a PHP application relies on getenv() to determine its behavior, the mechanism for populating those variables depends entirely on how Apache serves the request. Unlike a CLI script where you might simply export variables in your shell profile, an Apache server runs processes that need explicit configuration to inherit these values. The standard approach involves setting environment variables at the process level or within the specific virtual host context.

If you are running PHP via mod_php (the traditional shared hosting method), the most direct way to set global environment variables is through the Environment directive in your Apache configuration file, typically located in /etc/apache2/envvars on Debian/Ubuntu systems or directly in the httpd.conf on RHEL/CentOS. You would add a line like:

Environment MY_APP_SECRET=supersecret123

However, this method has a significant limitation regarding your requirement for domain-specific variables. The Environment directive defined globally applies to all sites served by that Apache instance. To isolate configuration per domain—such as setting a different database URL for app.example.com versus staging.app.example.com—you must move the definition into the VirtualHost block.

Here is how you configure it within a specific <VirtualHost>:

<VirtualHost *:80>
    ServerName app.example.com
    
    # Set environment variables for this specific domain
    Environment APP_ENV=production
    Environment DB_HOST=db-prod.internal
    Environment CACHE_ENABLED=true
    
    DocumentRoot /var/www/app.example.com/public
</VirtualHost>

This approach ensures that when Apache spawns the PHP-FPM or mod_php process to handle requests for app.example.com, those specific variables are injected into the environment before the script executes. When PHP receives the request, getenv('APP_ENV') will return production only for this domain.

There is a critical nuance regarding how Apache passes these variables to the CGI/FCGI process depending on your setup. If you are using mod_proxy_fcgi with PHP-FPM (a more modern and recommended setup), the Environment directive might not be passed through automatically in all versions of Apache or when using specific MPM configurations. In those cases, relying solely on the Environment directive inside the VirtualHost might result in empty values inside PHP.

To guarantee compatibility, especially if you are dealing with complex setups or newer Apache versions, it is often safer to pass these variables via the .env file logic or by utilizing the SetEnvIf directive combined with headers, though the latter is less common for backend secrets. A more robust alternative that works consistently across different PHP wrappers (mod_php and PHP-FPM) without relying on potentially flaky Environment directive propagation is to define these variables directly in the Apache configuration using SetEnv.

SetEnv APP_ENV production
SetEnv DB_HOST db-prod.internal

While SetEnv and Environment are often synonymous, SetEnv is generally preferred when interacting with CGI/FCGI wrappers because it explicitly sets the variable regardless of the underlying MPM. However, keep in mind that SetEnv does not allow you to set complex values like arrays or very long strings as effectively as some other methods might suggest; simple string assignments are best.

If your application logic requires different configurations based on the request URI rather than just the domain, you can leverage SetEnvIf. This allows conditional variable setting:

SetEnvIf Request_URI "^/api/v2" ENVIRONMENT=API_V2_MODE

This is useful if a single domain hosts multiple distinct applications or microservices behind a reverse proxy.

Handling Sensitive Data and Security

When storing sensitive values like API keys or database passwords in Apache configuration files, you face a trade-off between security and convenience. If your config file resides on disk, any user with read access to that file (and potentially root access depending on permissions) can see these variables. This is often acceptable for staging environments but problematic for production.

For high-security requirements, avoid hardcoding secrets directly in the Apache config. Instead, use a separate configuration directory with restricted permissions (e.g., chown www-data:www-data and chmod 640) and include that file conditionally. Alternatively, leverage system-wide environment variables if you prefer keeping secrets out of the web root entirely, ensuring the parent shell exports them before Apache starts.

Conclusion on Implementation

To achieve domain-specific isolation reliably, place your SetEnv or Environment directives inside the corresponding <VirtualHost> blocks. If global settings are needed for all domains, define them at the server level but override them in specific VirtualHosts where necessary. Always verify that your PHP version and Apache MPM support the chosen method, as propagation mechanisms can vary between mod_php and PHP-FPM setups.

Our network