Why /etc/environment Often Fails to Set PHP Variables and What to Do Instead
Developers frequently encounter a specific friction point when managing deployment environments: determining how to broadcast a global setting like ENVIRONMENT=local across all services on a machine without duplicating configuration in every application. The instinctive move is often to edit /etc/environment, but if you find that PHP cannot see this variable via getenv(), $_ENV, or $_SERVER, the issue isn't necessarily the syntax of the file itself, but rather how the operating system initializes process environments.
The confusion usually stems from a misunderstanding of when environment variables are loaded. When you edit /etc/environment (or /etc/profile.d/*.sh on some distributions), these files are sourced during the login shell initialization sequence for interactive user sessions. However, web servers like Apache, PHP CLI scripts running via Cron, and containerized NodeJS instances spawn as background processes or daemon services. These processes bypass the interactive login scripts entirely. They read their environment directly from /etc/environment only if they are started by systemd in a way that explicitly passes those variables, but standard system daemons often ignore this file for security and isolation reasons.
Consequently, simply adding export ENVIRONMENT=local to /etc/environment will not make the variable available to your web application. The shell does not export these variables to child processes unless specific flags are used or if the process is explicitly started from a context that respects them (like a user's interactive session). If you restart your machine or log out and back in, an Apache service running as a system user will still see an empty environment regarding those custom variables because it never executed the login script where /etc/environment was read.
To achieve true global visibility for PHP, NodeJS, and Java applications simultaneously, you must target the init system or the specific shell profile used by the daemon users. The most reliable method on modern Linux distributions is to use systemd-environment.d. These files are specifically designed to set environment variables for services managed by systemd.
You can create a file at /etc/systemd/system/env-global.conf with the following content:
[Service]
Environment=ENVIRONMENT=local
This approach is superior because systemd automatically reloads and restarts dependent services to pick up these changes without requiring a full reboot. You can verify this works by restarting your web server:
sudo systemctl daemon-reexec
sudo systemctl restart apache2
Now, when you run a PHP CLI script or access the site via Apache, getenv('ENVIRONMENT') should return local. This same file applies to all services managed by systemd, satisfying the requirement for NodeJS and Java applications as well.
An alternative is modifying /etc/profile.d/local-env.sh, but this only works if you explicitly configure your daemon users to source it, which often requires editing /etc/bash.bashrc or similar files in ways that can break interactive shells. Stick with systemd-environment.d for system-wide service configurations. If you are using Docker, remember that the host's environment variables do not automatically leak into containers; you would need to pass them explicitly via --env-file.
For Laravel projects specifically, while .env files handle local overrides, relying on global OS-level variables for environment detection is a double-edged sword. It allows your application to detect the mode without reading file I/O on every request, but it couples your deployment strategy tightly to the host OS configuration. If you need to distinguish between a specific virtual host and a global development setting, using SetEnv in Apache's Virtual Host configuration remains valid, provided that logic is encapsulated in a central config snippet rather than copied manually per server. However, for a true "machine-wide" state applicable to mixed-stack deployments (PHP, Node, Java), configuring the init system via systemd-environment.d is the robust, standard-compliant solution.