Skip to content

Environment Variables

Solved Linux
8 3 1.6k 1
  • Hello and thank you in advance for your help.
    I am running a Digital Ocean Droplet with Ubuntu Server 20.04. I have Virtualmin, Webmin and the package for that setup. I have the same setup for a dev platform that I test before making update live on the production server. I am also using pm2 to run my node applications with a proxy website under the domain in virtualmin pointing to http://127.0.0.1:PORT.

    I am having trouble on where to put environment variables.

    I have tried this location and the variables just don’t work. In my node app I am using process.env.VARIABLE.
    97e71179-231d-4315-8efe-06a8a749d069-image.png

    I have also put variables in /etc/environment and have created a sh file with the variables in /etc/profile.d folder.

    I am not sure what I am doing wrong or if I need to be adding these variables in a totally different spot. I have done hours of research on the internet. The current solution that I have somewhat working is putting the variables in both /etc/environment and /etc/profile.d/myvariables.sh.

    In /etc/environment I am using the following format:

    VARIABLE_NAME=whatiwantthevaluetobe
    

    In the myvariables.sh I am using the format below:

    export VARIABLE_NAME=whatiwantthevauletobe
    

    I am lost and need some help getting back on the path and the proper way to make this work. Let me know if you need any other info and thanks again for the help.

  • @madchatthew I’ve had some instances in the past where any environmental variables I set wouldn’t take without a restart.

    Long shot, but worth a try.

    @phenomlab So I found that I had to delete the pm2 instance of my node app and then add it again. I wasn’t able to tell if I had to logout and log back in first. I rebooted the server and then it continued to work. I shut down the server over night and restarted it just now and everything seems to be back up and running.

    Thank you everyone, I think I finally understand how to make environment variables work. It is nice because other logins shouldn’t have access to variables from other apps. That will help to keep things secured a little better than just having global environment variables. If I am understanding that wrong, please let me know.

    Thank again!

  • Hello and thank you in advance for your help.
    I am running a Digital Ocean Droplet with Ubuntu Server 20.04. I have Virtualmin, Webmin and the package for that setup. I have the same setup for a dev platform that I test before making update live on the production server. I am also using pm2 to run my node applications with a proxy website under the domain in virtualmin pointing to http://127.0.0.1:PORT.

    I am having trouble on where to put environment variables.

    I have tried this location and the variables just don’t work. In my node app I am using process.env.VARIABLE.
    97e71179-231d-4315-8efe-06a8a749d069-image.png

    I have also put variables in /etc/environment and have created a sh file with the variables in /etc/profile.d folder.

    I am not sure what I am doing wrong or if I need to be adding these variables in a totally different spot. I have done hours of research on the internet. The current solution that I have somewhat working is putting the variables in both /etc/environment and /etc/profile.d/myvariables.sh.

    In /etc/environment I am using the following format:

    VARIABLE_NAME=whatiwantthevaluetobe
    

    In the myvariables.sh I am using the format below:

    export VARIABLE_NAME=whatiwantthevauletobe
    

    I am lost and need some help getting back on the path and the proper way to make this work. Let me know if you need any other info and thanks again for the help.

    @madchatthew Generally speaking one sets environment variables for various users/environments in their login/profiles. For e.g., a nodebb instance running as user nodebb, and presuming a bash shell, then add those env vars to either .profile or .bash_profile in the user’s home dir, e.g.;

    FOO='totally awesome bar!''
    export FOO
    

    Depending on your shell you may be able to combine those into a single line.

    But I do not really know. If using systemd to start the app then maybe the service file is the place to be doing this?

    Just a couple ideas since nobody has yet responded. You’ll get better. Be patient.

  • @madchatthew Generally speaking one sets environment variables for various users/environments in their login/profiles. For e.g., a nodebb instance running as user nodebb, and presuming a bash shell, then add those env vars to either .profile or .bash_profile in the user’s home dir, e.g.;

    FOO='totally awesome bar!''
    export FOO
    

    Depending on your shell you may be able to combine those into a single line.

    But I do not really know. If using systemd to start the app then maybe the service file is the place to be doing this?

    Just a couple ideas since nobody has yet responded. You’ll get better. Be patient.

    @gotwf Thank you for your response. I will give that a try and see if it makes a difference. I will let you know what I find out. This is something I need to know because it will affect how things are setup for other apps on the server.

    Thank you again, I will let you know what I find out.

  • So the solution was to create a .bash_profile and place all of my variables in that file. Then I just had to make sure to export each variable just like in the example from @gotwf post. Thank you @gotwf for your input. I am confident I can replicate this over to my production server now and make everything run the way it should.

  • Well I take that back, the one app is working but my other app isn’t working. I will have to dig deeper into this and try to figure out why that is. So weird.

  • Well I take that back, the one app is working but my other app isn’t working. I will have to dig deeper into this and try to figure out why that is. So weird.

    @madchatthew I’ve had some instances in the past where any environmental variables I set wouldn’t take without a restart.

    Long shot, but worth a try.

  • @madchatthew I’ve had some instances in the past where any environmental variables I set wouldn’t take without a restart.

    Long shot, but worth a try.

    @phenomlab So I found that I had to delete the pm2 instance of my node app and then add it again. I wasn’t able to tell if I had to logout and log back in first. I rebooted the server and then it continued to work. I shut down the server over night and restarted it just now and everything seems to be back up and running.

    Thank you everyone, I think I finally understand how to make environment variables work. It is nice because other logins shouldn’t have access to variables from other apps. That will help to keep things secured a little better than just having global environment variables. If I am understanding that wrong, please let me know.

    Thank again!

  • @phenomlab So I found that I had to delete the pm2 instance of my node app and then add it again. I wasn’t able to tell if I had to logout and log back in first. I rebooted the server and then it continued to work. I shut down the server over night and restarted it just now and everything seems to be back up and running.

    Thank you everyone, I think I finally understand how to make environment variables work. It is nice because other logins shouldn’t have access to variables from other apps. That will help to keep things secured a little better than just having global environment variables. If I am understanding that wrong, please let me know.

    Thank again!

    @madchatthew great you got this to work ! Thanks for the update.

  • phenomlabundefined phenomlab has marked this topic as solved on

Did this solution help you?
Did you find the suggested solution useful? Why not buy me a coffee? It's a nice gesture, and a great way to show your appreciation 💗

Related Topics
  • To the Window to the Linux . . .

    Pinned Linux arch linux windows endoflife
    18
    13 Votes
    18 Posts
    2k Views
    Just wanted to say that it has been fun making my mom learn Arch Linux by switching the church school computers over to it where she assists.
  • Arch Linux | Reflector

    Linux arch linux pacman reflector updates
    4
    4 Votes
    4 Posts
    1k Views
    I have another update for reflector that I just figured out. You will want to edit the following file and add details to make sure that every time it updates the mirror list, it is grabbing them from your country and I grab 10 and sort them by rate. This will ensure that your mirrorlist isn’t getting populated with links from other countries which can slow down you downloads. sudo nano /etc/xdg/reflector/reflector.conf This is what the file looks like. # Reflector configuration file for the systemd service. # # Empty lines and lines beginning with "#" are ignored. All other lines should # contain valid reflector command-line arguments. The lines are parsed with # Python's shlex modules so standard shell syntax should work. All arguments are # collected into a single argument list. # # See "reflector --help" for details. # Recommended Options # Set the output path where the mirrorlist will be saved (--save). --save /etc/pacman.d/mirrorlist # Select the transfer protocol (--protocol). --protocol https # Select the country (--country). # Consult the list of available countries with "reflector --list-countries" and # select the countries nearest to you or the ones that you trust. For example: --country US # Use only the most recently synchronized mirrors (--latest). --latest 10 # Sort the mirrors by synchronization time (--sort). --sort rate Here are the different flag options that you can change or add to this file. I found this on google by searching arch linux reflector flags. Filtering Options (What to find) --country <CODE/Name>: Selects mirrors from specific countries (e.g., US, France,Germany). Use {Link: reflector --list-countries https://archlinux.org/mirrors/status/}, for a full list. --protocol <http|https>: Filters for HTTP or HTTPS mirrors. --age <hours>: Only includes mirrors synced within the last X hours (e.g., --age 12). --delay <hours>: Limits to mirrors with a reported sync delay of X hours or less (e.g., --delay 0.25 for 15 mins). --latest <N>: Limits results to the top N newest mirrors. Sorting Options (How to order) --sort rate: Sorts by download speed (requires testing, can be slow). --sort age: Sorts by most recently synced. --sort score: Sorts by overall score. --sort country: Sorts by country, useful with multiple countries. Output Options (Where to put it) --save <path>: Writes the filtered/sorted list to a file (e.g., /etc/pacman.d/mirrorlist). --verbose or -v: Shows detailed progress and mirror info.
  • Ubuntu 22.04 to Ubuntu 24.04 Unable to ping

    Locked Unsolved Linux ubuntu
    37
    3 Votes
    37 Posts
    4k Views
    Okay, you have time, let me know
  • Linux on a Stick

    Linux linux usb arch
    29
    16 Votes
    29 Posts
    3k Views
    @DownPW I haven’t tried either of those, I am sure they run fast as well. Linux is so versatile it is awesome!
  • Arch Linux + Me = Crazy

    Linux arch linux web server vps
    10
    0 Votes
    10 Posts
    1k Views
    @Madchatthew hmm, yes, that might be problematic.
  • 3 Votes
    30 Posts
    4k Views
    @DownPW any update?
  • Error certification on virtualmin/Nginx

    Solved Linux cert virtualmin nginx
    17
    2
    0 Votes
    17 Posts
    2k Views
    @DownPW anytime
  • Ubuntu Upgrade All Packages

    Solved Linux mangodb sudonix
    3
    3 Votes
    3 Posts
    800 Views
    @phenomlab Upgraded, everything works