About environmental variables – an introduction to environmental variables and how they work on the server.
Every time I need to set Linux Environment variables and I use google to find out how, I always end up with those VALUELESS posts that tell you to use a terminal with export blah blah blah. Then when I close the terminal and try to use a variable it doesn’t exist and I am like WTF. Well those posts are how to set a temporary environmental variable.
This post covers what I always google and get the failed results for. This post is how to permanently forever set environmental variables. This is often needed for development to store sensitive api key secrets, passwords etc. You should never put any of that information in a file for your project.
The idea is to create environmental variables to hold this info on your local machine, then when you put your code into production you add the necessary environmental variables to whatever controls them for your app. You can use kubernetes secrets or Hashicorp vault or if you are using something like Gitlab or Openshift continuous integration/delivery pipeline workflow will have a way to enter these values securely.
But here is how you set your local linux environment variables permanently. There are many ways to set these variables, here is a great article covering the topic.
But none of that above is useful to use, nope not for what we want to do. I just got a cool idea, rant/WOULDN’T IT BE COOL IF YOU COULD SET ENVIRONMENTAL VARIABLES IN MORE WAYS WITH LINUX. I mean where is the fun in only having 1 maybe 2 good ways of doing something, lets add more and more and more./rant
Ok so all of that sucks, how can we truly set environmental variables for our Development environment in a way that is easy and safe? Magic that is how. Actually what you do is create a file with all of the secrets your app needs for development testing as environmental variables. I had to search hard and long to figure this out.
You create a file with any name you want with extension .sh like example-app.sh The file goes in /etc/profile.d directory.
As described in the link above Linux will read all of the files in that directory and create the environmental variables. Hours of digging to figure that simple trick out FFS.
To set the environment variable globally add it to the .bashrc file on ubuntu it is found in
This is basically what I have in a simple text file
You simply add a new line for each variable you want to have created for your app.
I know I’ll never remember where I saw this. One day I will need to dump or check the environmental variables for my app.
The documentation here lists how to check the environmental variables towards the bottom of the page.