Skip to content

Instantly share code, notes, and snippets.

@brutasse
Last active December 25, 2015 13:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brutasse/6981790 to your computer and use it in GitHub Desktop.
Save brutasse/6981790 to your computer and use it in GitHub Desktop.
Setting application environment variables on production servers

A great tool is envdir

apt-get install envdir
mkdir /etc/mysite.d/env
echo "postgres://postgres:@localhost:5432/mysite" > /etc/mysite.d/env/DATABASE_URL
echo "s3ce3t K3Y" > /etc/mysite.d/env/SECRET_KEY
echo "/srv/www/mysite/static" > /etc/mysite.d/env/STATIC_ROOT
echo "mysite.settings" > /etc/mysite.d/env/DJANGO_SETTINGS_MODULE

etc. etc. for all variables to set. Then prefix all commands with envdir /etc/mysite.d/env. If you have a site deployed in a virtualenv located in /srv/www/mysite/venv, here is how to run gunicorn to serve this site:

envdir /etc/mysite.d/env /srv/www/mysite/venv/bin/gunicorn mysite.wsgi

And to run a management command:

envdir /etc/mysite.d/env /srv/www/mysite/venv/bin/python manage.py migrate --list

It's a bit of typing for cron entries and initscripts / supervisor scripts but works very well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment