Resources Web Development

Server Environmental variables resources and info

About environmental variables – an introduction to environmental variables and how they work on the server.

Web Development Web Security

How to permanently set Linux environmental variablees

This is a giant subject. I’ll continue updating this article as I find and understand more about how Linux does all of this.

Most articles show how to set them in the terminal, then when you close the terminal and try to use a variable it doesn’t exist leaving you confused.

Well those posts are how to set a temporary environmental variable accessible in your terminal until it closes only.  To set a lasting Environmental variable you need to use a file.

This is often needed to store sensitive api key secrets, passwords etc. You should never put any of that information in a file for your project directly, meaning don’t put it in a file that is stored in your code repository.

Developer App Variables

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.

One problem you will run into with apps is how Nginx or Apache either pass or don’t pass the variables to your scripts. It can be confusing. Apache gladly passes values from the environment to our scripts, but Nginx not so much. With nginx you need to set your vars in 2 locations or run a hacky feeling 3rd party Perl script which takes your variables and passes them to Nginx. Nginx has better performance than Apache though. The story is an entire article for later though, back to the subject.

Overall Environment variables

There are many ways to set environment variables in linux and each depends on what needs to be done. Each user has their own files that set variables for them. There are also other files that set variables for programs that run on your computer like cronjob. There are also files to set variables system wide, but it depends on what is operating whether it can access it.

You can create a file with any name you want with extension .sh like and place it in /etc/profile.d directory.

As described in this link Linux will read all of the files in that directory and create the environmental variables.

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.

Other locations

Each user on your Linux system has a .bashrc file  the root, regular user etc. all have one. On Ubuntu root is disabled so settings in that .bashrc file are probably ignored. But the user you usually login as, the default name that shows in your terminal when you open it, that home directory has a .bashrc file. Located at /home/username/.bashrc If you set a variable in this file you must log out and log back into your system for them to take effect or restart your computer.

As mentioned in this article, the shell looks in several places when the terminal is invoked.  Where to place it depends on what you need to do.

There are login shells, interactive shells, non interactive shells, each looks in different locations. This article has a good discussion of interactive vs non interactive. Basically interactive is when you type into the terminal, non interactive is when you run a shell script or other program that does the interaction with bash ( or other shell ) for you.

This article describes the many locations and files used to set environment variables. Not all versions of Linux will have all of these or use them, each version is different and you should read the documentation for your specific distribution. The bottom of that article gives you a better idea of where to set the variables.

For scripts like shell scripts set the environmental variables in .bashrc files. For interactive use in the terminal where the user or their commands need the variables set them in the .profile file or profile.d directory as mentioned above.

Here is a much more in depth article about Linux Environment variables.

As you can see in this link, this whole process depends on many things.

Here is another good, long explanation of Environment and Variables by digital ocean.

Resources Web Development

Symfony how to list environmental variables

You can use this command to see what environmental variables Symfony can see.

php bin/console debug:dotenv

If it is not in that list, symfony cannot see it.

I just updated to version 5.4 and they have changed something because now all of my environmental variables are not seen.

So now I must do research and figure out what has been done and how to get my environmental variables back. I had them set as Linux system environment variables and it worked great. Then update and now BOOM DEAD.

The documentation here lists how to check the environmental variables towards the bottom of the page.

Resources Software Development

BASH shell scripting links and resources

Bash scripting manual 5.1

Advanced Bash scripting guide

Bash script cheat sheet

Bash Scripting: Everything you need to know about Bash-shell programming

Respond to prompt in Bash Script – a tutorial on how to respond to prompts in Bash scripts.

How To Write a Bash Script That Answers Interactive Prompts 

MySQL: Run Query from Bash Script or Linux Command Line

How to Use Variables in Shell Scripting

Bash scripts and Mysql – a good article about working with Mysql in bash scripts, how to load SQL etc.

If statement syntax example coverage

More coverage and examples of Bash if/else statement syntax and use

Bash read builtin command used to prompt the user for a response and store it for use in the script

How to prompt for input from user in a linux shell script

How to prompt and read user input in a Bash shell script – more examples and information. Different from the above link.