Skip to content

Environment Variables

Solved Linux
8 3 1.7k 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? Support 💗 Sudonix with a coffee
If your organisation needs deeper expertise around infrastructure, security, or technology leadership, learn more about Phenomlab Ltd. Many of the deeper technical guides behind Sudonix are published there.

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

    Pinned Linux arch linux windows endoflife
    22
    19 Votes
    22 Posts
    2k Views
    @phenomlab said: @Madchatthew ouch. Sounds nasty. Did you get to the bottom of why it happened? I believe it is due to not everything getting upgraded because i wasn’t checking on the different packages I had installed from the AUR. Then when I ran yay it was like, hey would you like to update all of these things that you haven’t updated in months, perhaps years or ever for that matter and I was like yes please If you don’t have yay there are no notifications that you need more updates than what you realize. Chrome was staying updated because it would give me a notification, but there was the nvidia package that needed to be upgraded as well and I had never upgraded it. I didn’t realize it and should have. Then some of those packages use cmake and that needed to be updated as well. So using yay is beneficial to make sure you get all the updates you need.
  • Arch Linux | Reflector

    Linux arch linux pacman reflector updates
    4
    4 Votes
    4 Posts
    2k 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.
  • Linux on a Stick

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

    Linux linux certs
    4
    2 Votes
    4 Posts
    787 Views
    @Madchatthew You might be sorry you asked This is the mentoring category below. Presently, it’s unused, but we have a dedicated area for it. https://sudonix.org/category/8/mentoring Other services https://sudonix.org/services
  • 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
    5k Views
    @DownPW any update?
  • Error certification on virtualmin/Nginx

    Solved Linux cert virtualmin nginx
    17
    2
    0 Votes
    17 Posts
    3k Views
    @DownPW anytime
  • Avoid network traffic jam with BBR

    Linux ftp speed
    2
    1 Votes
    2 Posts
    847 Views
    @DownPW interesting. Does this have any effect on data transfers over SCP