How do I access environment variables set in .conf file in PHP code?
I'm running Ubuntu/Apache and my app is written in PHP. I have set an environment variable in my /etc/apache2/sites-available/example-app.conf like the following (the SetEnv line at the top):
<VirtualHost *:80>
SetEnv APP_ENV staging
ServerAdmin info@example.com
ServerName stag.example.com
ServerAlias stag.example.com
DocumentRoot /var/www/example-app/public
ErrorLog /var/www/example-app/logs/errors.stag.log
CustomLog /var/www/example-app/logs/access.stag.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =stag.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
In my PHP file, I am trying to access this APP_ENV variable by doing the following, but it is returning an empty result:
getenv("APP_ENV")
I have also tried accessing it in $_ENV, $_SERVER, and apache_getenv("APP_ENV") with no luck.
Any idea how I can access this environment variable in my PHP code?
3 Replies
✓ Best Answer
My understanding was that
apache_getenv()
would be able to access apache environment variables, but so far, that doesn't return any information.
apache_getenv()
will only work if you're using mod_php as your PHP interpreter. mod_php embeds the PHP interpreter into the apache2 process. While this sounds like what you want, it probably isn't… mod_php is not thread-safe and it's use prevents you from using any of the apache2 multi-threaded MPMs (mpm-event & mpm-worker)…restricting you to the uber-slow & process-hogging mpm-prefork. It's also old and creaky and it's useful life is probably going to end sooner rather than later (it's part of PHP…not apache2).
If you use php-fpm (which you should be) or any other CGI configuration to invoke the PHP interpreter, it will live in a separate process from the web server. Therefore, any code being run by the PHP interpreter cannot reach inside the apache2 process to get SetEnv values. Those are private to the apache2 process.
Believe me, you will save yourself much pain if you adopt the approach I suggested and use a PHP-only mechanism to transmit this information to the PHP-interpreter. Been there, done that…
-- sw
P.S. See also: https://github.com/vlucas/phpdotenv
Any idea how I can access this environment variable in my PHP code?
SetEnv only applies to the apache web server. See:
https://httpd.apache.org/docs/current/env.html
You can't access variables set with SetEnv in your PHP code because they are not in the global environment.
The way to do this is to set up a private ini file and follow the suggestions here:
https://stackoverflow.com/questions/15976038/how-to-use-php-with-ini-files
If you use php-fpm (which you should be), the PHP interpreter lives in a separate process from the web server.
-- sw
P.S. I often use JSON- or YAML-formatted files for this kind of stuff because the feature set is richer (i.e., they support arrays and hashes) and parsing them is a snap. See:
Thank you for that clarification, @stevewi. My goal is to be able to manage separate staging and production environments on the same linode instance, so my real challenge is passing a variable from the <VirtualHost>
to the PHP code so that I know which environment should be loaded. Is there no way to do that? My understanding was that apache_getenv()
would be able to access apache environment variables, but so far, that doesn't return any information.