Why /etc/environment Disappears for Apache and How to Fix It
A common point of confusion in Linux system administration involves the hierarchy of environment variables. On Ubuntu, placing definitions in /etc/environment is a standard practice for defining the baseline environment for all processes on the machine. If you run a PHP script via the command line, those variables are indeed available. However, when that same script is executed through an Apache2 handler (via http://domain/test.php), those specific variables often vanish from $_SERVER, $_ENV, and getenv().
The root cause lies in how different init systems and service managers launch their processes. When you invoke a command directly in your terminal, the shell reads /etc/environment early in its initialization sequence, merging those variables into the environment before spawning child processes like PHP. Consequently, your CLI script inherits them naturally.
Apache, however, does not run in a standard interactive shell session. The Apache httpd daemon starts as a background service managed by systemd. Crucially, systemd units (like the default apache2.service) are often configured to ignore /etc/environment or treat it differently than an interactive login shell. Furthermore, Apache typically spawns its child worker processes (MPM) which may not inherit the parent process's environment in the way you expect if the parent wasn't explicitly initialized with those variables via a login profile like .bashrc.
This architectural difference explains why adding variables to /etc/apache2/envvars resolves the issue. The Apache configuration file located at that path is specifically designed for this purpose. It is sourced by the httpd master process before it starts listening for requests, ensuring that any variables defined there are injected into the environment of every child worker process spawned subsequently.
While maintaining two separate filesâone for system-wide CLI access and one specifically for Apacheâis functionally effective, it introduces operational overhead. You must manually keep them in sync whenever you update a variable definition. Missing an update in /etc/environment but forgetting to apply it to /etc/apache2/envvars can lead to intermittent failures where your web application behaves differently than your local development scripts.
To achieve true consistency where both CLI and Apache load variables from a single source, you generally need to influence how systemd handles the environment for the Apache service. The most robust method involves modifying the unit file itself or ensuring that the specific systemd drop-in configuration explicitly sources the system environment.
You can inspect the current behavior of your Apache service using the following command:
systemctl show apache2 | grep EnvironmentFile
If EnvironmentFile is not listed or points to a different location, you may need to create a drop-in override file. Create a directory if it doesn't exist and add a new configuration file:
sudo mkdir -p /etc/systemd/system/apache2.service.d
sudo nano /etc/systemd/system/apache2.service.d/override.conf
Inside this file, you can attempt to explicitly tell systemd to use the system environment. However, systemd does not automatically read /etc/environment for services in the same way a login shell does. The most direct approach to make Apache respect your system-wide settings without duplicating data is to ensure that /etc/apache2/envvars is updated with your variables and then restart the service.
If you strictly require a single source of truth, the recommended pattern is often to keep /etc/environment for user-defined shell sessions and rely on systemd overrides or specific service configuration files for managed daemons like Apache. Alternatively, you can place the variable definitions in a script that both your CLI entry points and your Apache startup hooks execute.
In production environments, relying on /etc/apache2/envvars is often preferred precisely because it isolates web server configuration from general system user profiles. This separation prevents accidental pollution of the environment for interactive shells while ensuring the web server always runs with a known, consistent set of variables regardless of how systemd initializes the process.
If you are building complex applications, such as those using the Laravel framework, managing these environments becomes critical. Ensuring that secrets and configuration values defined in /etc/environment are accessible via $_SERVER in your web handler requires careful attention to this initialization order. For large-scale deployments involving multiple services, consider utilizing a centralized configuration management tool or a dedicated secret manager rather than relying on disparate environment files across the system.