Skip to content

Environment Variables

Solved Linux
8 3 3.1k 1
  • wrote on last edited by phenomlab
    #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.

    wrote on last edited by
    #7

    @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.

    wrote on last edited by gotwf
    #2

    @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.

    wrote on last edited by
    #3

    @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.

  • wrote on last edited by
    #4

    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.

  • wrote on last edited by
    #5

    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.

    wrote on last edited by
    #6

    @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.

    wrote on last edited by
    #7

    @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!

    wrote on last edited by
    #8

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

  • phenomlab 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
    8k Views
    Madchatthew
    @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.
  • Linux on a Stick

    Linux linux usb arch
    29
    16 Votes
    29 Posts
    12k Views
    Madchatthew
    @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
    2k Views
    phenomlab
    @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
    3k Views
    phenomlab
    @Madchatthew hmm, yes, that might be problematic.
  • 3 Votes
    30 Posts
    12k Views
    phenomlab
    @DownPW any update?
  • Error certification on virtualmin/Nginx

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

    Linux ftp speed
    2
    1 Votes
    2 Posts
    1k Views
    phenomlab
    @DownPW interesting. Does this have any effect on data transfers over SCP
  • Ubuntu Upgrade All Packages

    Solved Linux mangodb sudonix
    3
    3 Votes
    3 Posts
    2k Views
    cagatay
    @phenomlab Upgraded, everything works