<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Damien Alexandre</title>
    <description>The latest articles on DEV Community by Damien Alexandre (@damienalexandre).</description>
    <link>https://dev.to/damienalexandre</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F39052%2Fd0f7b435-f897-4a94-a0bd-557cd8c34c3b.png</url>
      <title>DEV Community: Damien Alexandre</title>
      <link>https://dev.to/damienalexandre</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/damienalexandre"/>
    <language>en</language>
    <item>
      <title>What you need to know about environment variables with PHP</title>
      <dc:creator>Damien Alexandre</dc:creator>
      <pubDate>Thu, 12 Oct 2017 08:00:00 +0000</pubDate>
      <link>https://dev.to/damienalexandre/what-you-need-to-know-about-environment-variables-with-php-d3c</link>
      <guid>https://dev.to/damienalexandre/what-you-need-to-know-about-environment-variables-with-php-d3c</guid>
      <description>&lt;p&gt;Environment variables for configuration are today's best practice for application setup - database credentials, API Keys, secrets and everything &lt;em&gt;varying between deploys&lt;/em&gt; are now exposed to the code via the environment, instead of configuration files or worse, directly hard-coded.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--i8_cP-fy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/zxvspazedf8zscmax4k6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--i8_cP-fy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/zxvspazedf8zscmax4k6.png" alt="You can't leak what you don't store"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's dive into:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;how does it work?&lt;/li&gt;
&lt;li&gt;is it really a good idea?&lt;/li&gt;
&lt;li&gt;how to deal with them in PHP?&lt;/li&gt;
&lt;li&gt;and finally some recommendations and common errors to avoid - with some real world traps we fell into!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We are not going to cover how to setup environment variables in your webserver / Docker / crontabs... as it depends on the system, the software and we want to focus on env vars.&lt;/p&gt;

&lt;p&gt;If your hosting is using &lt;a href="https://blog.docker.com/2017/02/docker-secrets-management/"&gt;Docker Swarm&lt;/a&gt; or &lt;a href="https://aws.amazon.com/fr/blogs/mt/the-right-way-to-store-secrets-using-parameter-store/"&gt;AWS&lt;/a&gt;, things will be a little bit different for example, as they decided to push files on your container filesystem to expose your secrets, not env vars: that's very specific to those platforms and not a standard at all.&lt;/p&gt;

&lt;h2&gt;
  
  
  Env vars 101
&lt;/h2&gt;

&lt;p&gt;When you run a program, it inherits all environment variables from its parent. So if you set a variable named &lt;code&gt;YOLO&lt;/code&gt; with the value &lt;code&gt;covfefe&lt;/code&gt; in your bash and then run a command, you will be able to read &lt;code&gt;YOLO&lt;/code&gt; in any child process.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ YOLO=covfefe php -r 'echo getenv("YOLO");'
covfefe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As this variable is only locally defined, we can't read it from another terminal (another parent). So the idea is to make sure your application always inherits the needed variables.&lt;/p&gt;

&lt;p&gt;You can see all environment variables in your shell by running the following command, but as you will not see the &lt;code&gt;YOLO&lt;/code&gt; variable yet because it was only passed to the &lt;code&gt;php&lt;/code&gt; command on the fly, not set in the current process:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can set an environment variable with the syntax &lt;code&gt;export &amp;lt;NAME&amp;gt;=&amp;lt;VALUE&amp;gt;&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ export YOLO=covfefe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Variable names are case sensitive and the convention is to only use English, uppercase names with _ as separator (upper snake case). You already know some like &lt;code&gt;PATH&lt;/code&gt;, &lt;code&gt;DISPLAY&lt;/code&gt;, &lt;code&gt;HTTP_PROXY&lt;/code&gt;, ...&lt;/p&gt;

&lt;h3&gt;
  
  
  Today's best practice
&lt;/h3&gt;

&lt;p&gt;You may already know the &lt;a href="https://www.12factor.net/"&gt;twelve-factor methodology&lt;/a&gt; to build robust and scalable applications (if not, I suggest you take a break and check it out). The Configuration chapter explains why storing configuration in the environment is the way to go:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Config varies substantially across deploys (production, staging, testing...), code does not;&lt;/li&gt;
&lt;li&gt;Env vars are easy to change between deploys without changing any code;&lt;/li&gt;
&lt;li&gt;They are a language - and OS - agnostic standard. The same configuration can be shared between your PHP and Python processes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The manifesto also describes quite well what should be in the code and what should be in the environment - do not put your whole application configuration in it, &lt;strong&gt;only what differ from one deploy to another&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  I read on the Internet that env vars are dangerous
&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://movingfast.io/articles/environment-variables-considered-harmful/"&gt;Some articles&lt;/a&gt; will tell you that env vars are harmful for your secrets; the main reason is that any process inherits from his parent variables, &lt;strong&gt;all of them&lt;/strong&gt;. So if you have a very secret setting in the environment, child processes will have access to it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ export YOLO=covfefe
$ php -r "echo exec('echo $YOLO');"
covfefe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Child processes can consider environment variable to be something public, writable into logs, to include in bug reports, to dump to the user in case of error... &lt;strong&gt;They can leak your secrets&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The alternative is plain old text files, with strong Unix permissions. But what should really be done is &lt;strong&gt;clearing the environment when running a child process you do not trust&lt;/strong&gt; , like nginx does. By default, nginx removes all environment variables inherited from its parent process except the TZ variable. Problem solved!&lt;/p&gt;

&lt;p&gt;This can be done with &lt;code&gt;env -i&lt;/code&gt; which tells to start the following commands with an empty environment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ php -r "echo exec('env -i php -r \'echo getenv(\"YOLO\");\'');"

$ php -r "echo exec('php -r \'echo getenv(\"YOLO\");\'');"
covfefe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Always run processes you do not trust in a restricted environment&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Even if you trust your code, you should still be very careful and expose your variables to the least possible processes - you &lt;a href="https://twitter.com/o_cee/status/892306836199800836"&gt;never know&lt;/a&gt; (NPM Drama inside).&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting your PHP application ready
&lt;/h2&gt;

&lt;p&gt;When dealing with env vars in a PHP project, you want to make sure your code is going to always get the variable from a reliable source, be it &lt;code&gt;$_ENV&lt;/code&gt;, &lt;code&gt;$_SERVER&lt;/code&gt;, &lt;code&gt;getenv&lt;/code&gt;... But those three methods are not returning the same results!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ php -r "echo getenv('HOME');"
/home/foobar

$ php -r 'echo $_ENV["HOME"];'
PHP Notice: Undefined index: HOME

$ php -r 'echo $_SERVER["HOME"];'
/home/foobar
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is because of &lt;a href="http://us.php.net/manual/en/ini.core.php#ini.variables-order"&gt;the &lt;code&gt;variables_order&lt;/code&gt; PHP setting&lt;/a&gt; on my machine which is &lt;code&gt;GPCS&lt;/code&gt;, as there is no E I can't rely on the &lt;code&gt;$_ENV&lt;/code&gt; superglobal. This can lead to code working on one PHP installation and not the other.&lt;/p&gt;

&lt;p&gt;Another point is that developers don't want to manage env vars locally. We do not want to edit VirtualHost all the time, reloading php-fpm, rebooting some services, clearing caches... Developers wants a simple and painless way of setting environment variables... like a &lt;code&gt;.env&lt;/code&gt; file!&lt;/p&gt;

&lt;p&gt;An &lt;code&gt;.env&lt;/code&gt; file is just a compilation of env vars with their values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DATABASE_USER=donald
DATABASE_PASSWORD=covfefe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Dot Env libraries to the rescue
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://packagist.org/packages/vlucas/phpdotenv"&gt;vlucas/phpdotenv&lt;/a&gt;, the most popular library at the moment
&lt;/h4&gt;

&lt;p&gt;This library will read a &lt;code&gt;.env&lt;/code&gt; file and populate all the superglobals:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$dotenv = new Dotenv\Dotenv( __DIR__ );
$dotenv-&amp;gt;load();

$s3Bucket = getenv('S3_BUCKET');
$s3Bucket = $_ENV['S3_BUCKET'];
$s3Bucket = $_SERVER['S3_BUCKET'];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are some nice additions like the ability to mark some variables as required (and this is the one &lt;a href="https://github.com/laravel/framework/blob/5069e7a36e7f901234ff68f9748e55b6db9922b3/composer.json#L40"&gt;used by Laravel&lt;/a&gt;).&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://packagist.org/packages/josegonzalez/dotenv"&gt;josegonzalez/dotenv&lt;/a&gt;, security oriented
&lt;/h4&gt;

&lt;p&gt;This library doesn't populate the superglobals by default:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$Loader = new josegonzalez\Dotenv\Loader('path/to/.env');
// Parse the .env file
$Loader-&amp;gt;parse();
// Send the parsed .env file to the $_ENV variable
$Loader-&amp;gt;toEnv();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It supports required keys, filtering, and can throw exceptions when a variable is overwritten.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://packagist.org/packages/symfony/dotenv"&gt;symfony/dotenv&lt;/a&gt;, the new kid on the block
&lt;/h4&gt;

&lt;p&gt;Available since Symfony 3.3, this component takes care of the &lt;code&gt;.env&lt;/code&gt; file like the others, and populates the superglobals too:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$dotenv = new Symfony\Component\Dotenv\Dotenv();
$dotenv-&amp;gt;load( __DIR__.'/.env');

$dbUser = getenv('DB_USER');
$dbUser = $_ENV['DB_USER'];
$dbUser = $_SERVER['DB_USER'];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;There is more on &lt;a href="https://packagist.org/?q=dotenv&amp;amp;idx=packagist&amp;amp;p=0"&gt;packagist&lt;/a&gt; and at that point I'm too afraid to ask why everyone is writing the same parser all over again.&lt;/p&gt;

&lt;p&gt;But they are all using the same logic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;find a &lt;code&gt;.env&lt;/code&gt; file;&lt;/li&gt;
&lt;li&gt;parse it, check for nested values, extract all the variables;&lt;/li&gt;
&lt;li&gt;populate all the superglobals only for variables not already set.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I recommend to &lt;strong&gt;commit a &lt;code&gt;.env&lt;/code&gt; file with values made for the developers&lt;/strong&gt; : everyone should be able to checkout your project and run it the way they like (command line server, Apache, nginx...) without dealing with configuration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(new Dotenv())-&amp;gt;load( __DIR__.'/.env');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This recommendation work well when everyone has the same infrastructure locally: same database password, same server port… As we use Docker Compose on all our projects we never have any difference from one developer to another, if you don't have this luxury, just allow developers to overwrite the defaults by importing two files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(new Dotenv())-&amp;gt;load( __DIR__.'/.env', __DIR__.'/.env.dev');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That way you just have to create and populate a &lt;code&gt;.env.dev&lt;/code&gt; file with what's different for you (and add it to &lt;code&gt;.gitignore&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Then on production, you &lt;strong&gt;should not load those default values&lt;/strong&gt; , so the idea is to &lt;strong&gt;protect the loader&lt;/strong&gt; with an env var only set in production:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (!isset($_SERVER['APP_ENV'])) {
    (new Dotenv())-&amp;gt;load( __DIR__.'/.env', __DIR__.'/.env.dev');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you don't do that and your hosting provider forgot a variable, you are going to run development settings in production and have a bad time.&lt;/p&gt;

&lt;h2&gt;
  
  
  The pitfalls you have to look for âš
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Name conflicts
&lt;/h3&gt;

&lt;p&gt;Naming is hard, and env vars don't escape this rule.&lt;/p&gt;

&lt;p&gt;So when naming your env vars, you have to be specific and avoid as much as possible name collision. As there is no official list of reserved names, it's up to you. &lt;strong&gt;Prefixing custom variables can't harm&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The Unix world do it already, with &lt;code&gt;LC_&lt;/code&gt;, &lt;code&gt;GTK_&lt;/code&gt;, &lt;code&gt;NODE_&lt;/code&gt;...&lt;/p&gt;

&lt;h3&gt;
  
  
  Missing variables at runtime
&lt;/h3&gt;

&lt;p&gt;You have two choices when a variable is missing: either throw an Exception, or use a default value. That's up to you but the second one is silent... Which can cause harm in a lot of contexts.&lt;/p&gt;

&lt;p&gt;As soon as you want to use env vars, you have to set them everywhere:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;in the webserver;&lt;/li&gt;
&lt;li&gt;in the long running scripts and services;&lt;/li&gt;
&lt;li&gt;in the crontabs...&lt;/li&gt;
&lt;li&gt;and in the deployment scripts!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The last one is easy to miss, but as some deployment can warm application cache (like Symfony's)... Yep, a &lt;strong&gt;missing variable can lead to a corrupted application delivery&lt;/strong&gt;. Be strict about them and add a requirement check on your application startup.&lt;/p&gt;

&lt;h3&gt;
  
  
  The &lt;code&gt;HTTP_&lt;/code&gt; prefix
&lt;/h3&gt;

&lt;p&gt;There is just one prefix you should never use: &lt;code&gt;HTTP_&lt;/code&gt;. Because this is the one used by PHP itself (and other CGI-like contexts) to store HTTP request headers.&lt;/p&gt;

&lt;p&gt;Do you remember the &lt;a href="https://httpoxy.org/"&gt;httpoxy&lt;/a&gt; security vulnerability? It was caused by HTTP Client looking for this variable in the environment, in a way that could be set via a simple HTTP header.&lt;/p&gt;

&lt;p&gt;Some DotEnv libraries also prevent override of those variables, like &lt;a href="https://github.com/symfony/symfony/blob/v3.3.9/src/Symfony/Component/Dotenv/Dotenv.php#L73-L85"&gt;the Symfony one&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Thread safety of getenv()
&lt;/h3&gt;

&lt;p&gt;I have a bad news: in some configurations, using the &lt;code&gt;getenv&lt;/code&gt; function will result in unexpected results. &lt;strong&gt;This function is &lt;a href="https://github.com/laravel/framework/issues/7354"&gt;not thread safe&lt;/a&gt;&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;You should not use it to retrieve your values, so I suggest you call &lt;code&gt;$_SERVER&lt;/code&gt; instead - there is also a small performance difference between an array access and a function call for what it's worth.&lt;/p&gt;

&lt;h3&gt;
  
  
  Env vars are always strings
&lt;/h3&gt;

&lt;p&gt;One of the main issue now that we have type casting in PHP is that our settings coming from env vars are not always properly typed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function connect(string hostname, int port)
{
}

// This will not work properly:
$db-&amp;gt;connect($_SERVER['DATABASE_HOSTNAME'], $_SERVER['DATABASE_PORT']);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Symfony now allow to &lt;a href="http://symfony.com/blog/new-in-symfony-3-4-advanced-environment-variables"&gt;cast variables&lt;/a&gt;, and more like reading a file, decoding JSON...&lt;/p&gt;

&lt;h2&gt;
  
  
  Env vars everywhere, or not
&lt;/h2&gt;

&lt;p&gt;There is a lot of debates at the moment between env vars, files, or a mix of it: env vars referencing a configuration file. The fact is that despite being considered a best practice, env vars are not introducing a lot of advantages...&lt;/p&gt;

&lt;p&gt;But if properly used, in a Symfony application for example, env vars can be changed on the fly, without clearing any cache, without doing any filesystem access, without deploying code: just by restarting a process, for example.&lt;/p&gt;

&lt;p&gt;The trend to have just one variable, like &lt;code&gt;APP_CONFIG_PATH&lt;/code&gt;, and reading it via &lt;code&gt;'%env(json:file:APP_CONFIG_PATH)%'&lt;/code&gt; looks like re-inventing the good old &lt;code&gt;parameters.yml&lt;/code&gt; to me, unless the file is managed automatically by a trusted tool (like AWS Secret Store). There is also &lt;a href="https://www.envkey.com/"&gt;envkey.com&lt;/a&gt; which allow to control your env vars from one location, without dealing with files yourself, I like this approach as it's closer to the simplicity of Heroku-like hosting!&lt;/p&gt;

&lt;p&gt;What are you using to expose your credentials to your application? Do you have any pro-tips Â©ï¸ to share about env vars? Please comment!&lt;/p&gt;

</description>
      <category>php</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
