Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put environment variables when using nginx and Passenger on Ubuntu

I was trying to set up a system similar to heroku where I would store secret keys in environmental variables and then access them from my rails app like this:

secret = ENV['EMAIL_PASSWORD'] 

I know heroku lets you do heroku config:add EMAIL_PASSWORD=secret, and I wanted to do something like that for my own ubuntu box running nginx and Passenger.

Should I add these variables as exports in .bashrc or .bash_login so that on system reboot these variables are automatically set?

I'm not sure when each of those files gets read in.

like image 668
Tyler DeWitt Avatar asked Apr 09 '12 22:04

Tyler DeWitt


People also ask

Can Nginx use environment variables?

Out-of-the-box, nginx doesn't support environment variables inside most configuration blocks. But envsubst may be used as a workaround if you need to generate your nginx configuration dynamically before nginx starts.

Where do I put environment variables?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable.


2 Answers

You can use dotenv gem which loads the .env file as environmental variables. You can generate the .env file for different environments, and need not be rather should not checked into your repository.

like image 175
leenasn Avatar answered Oct 08 '22 01:10

leenasn


Keep in mind that nginx may not be running under the same environment as you are, and usually (pronounced "Apache") we add env-vars in the server config file via SetEnv. However, nginx doesn't have such a feature... nor does it need one, I believe.

sudo -E /usr/local/sbin/nginx 

When running nginx for it to be aware of your own user env vars.

Or, check out the env command (see here):

env EMAIL_PASSWORD=secret 

To answer your question, yes, you should use export statements in your shell config files.

like image 34
ybakos Avatar answered Oct 08 '22 02:10

ybakos