Reputation: 15824
Is there a way to set system wide/globally visible environment variables on an Azure VM (service management) with Ubuntu OS? I'm stuck in a situation where setting them inside ubuntu's /etc/environment
/etc/profile
and /etc/bash.bashrc
isn't being picked up by my application code. However, they do show up on printenv
. My guess is they're somehow getting bypassed because of the way my webserver is set up (Gunicorn + Nginx reverse proxy).
But maybe there's a way to set up env variables on Azure VMs that that takes precedence over everything? I know Heroku has that option in their dashboard (I've been using it), so do Azure Web Apps (which I'm not able to use due to a variety of well-documented compatibility issues).
Upvotes: 2
Views: 5849
Reputation: 24138
As references, I post my steps on Azure VM. You can check them and compare with yours.
ssh [email protected]
Install the tool pip
and virtualenv
:
sudo apt-get update
sudo apt-get install python-pip
sudo pip install virtualenv
Preparing the virtualenv to install gunicorn
and django
mkdir environments
virtualenv environments/experiment/
cd environments/experiment/
source bin/activate
pip install gunicorn django
Create a django project and run it by using gunicorn
and try to access it:
django-admin startproject mysite
bin/gunicorn --chdir=mysite -w 3 --env DJANGO_SETTINGS_MODULE=mysite.settings mysite.wsgi:application
# using w3m to access http://localhost:8000
w3m http://localhost:8000
Install Nginx and configure reverse proxy:
sudo apt-get install nginx
sudo cp /etc/nginx/site-available/default /etc/nginx/site-available/default.bak
sudo vim /etc/nginx/site-available/default
The configured content in the default
file for nginx below:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
include proxy_params;
proxy_pass http://unix:/home/<user>/environments/experiment/mysite/mysite.sock;
# I also try to config `http://localhost:8000;`, it's ok too.
}
}
I also try to config proxy_pass http://localhost:8000;
, it's ok too.
Restart nginx
service and restart gunicorn
:
sudo service nginx restart
bin/gunicorn --chdir=mysite --bind unix:/home/<user>/environments/experiment/mysite/mysite.sock -w 3 --env DJANGO_SETTINGS_MODULE=mysite.settings mysite.wsgi:application
And I found the application can't get the environment variables set up after application startup. So please run the command source /etc/...
before gunicorn
startup.
Any concern, please feel free to let me know.
Upvotes: 1