Notes and anecdotes

Techstuff rambling

Running Django with nginx (on webfaction)

Case: You’re using virtualenv and have used apache with a config somewhat like this.

Problem: Apache ‘goes to sleep’

Apache caches your wsgi application, and if it’s not used within a certain amount of time, it has to reload. And this takes like.. 10-15 seconds.

Solution: Use nginx instead:

    • Set up a custom application, and install nginx + uwsgi to point to your original application like this.
    • Potential new problem: Doesn’t find python – Can happen if you’ve installed the python version manually. line 41: python3.4: command not found

      Solution: specify path to python instead of python version in (you can find it with ‘which python3.4’ if you’re unsure)

    • Potential new problem: Doesn’t find application. Can happen if you’re using virutalenv. Website responds with:
      uWSGI Error
      Python application not found

      Solution: activate virtualenv in like this

      import sys, os
      virtualenv_root = os.path.expanduser('~/webapps/your_app/env')
      activate_this = "%s/bin/" % virtualenv_root
      # Python 2
      # execfile(activate_this, dict(__file__=activate_this))
      # Python 3
      with open(activate_this) as f:
       code = compile(, activate_this, 'exec')
       exec(code, dict(__file__=activate_this))
      workspace = os.path.expanduser('~/webapps/your_app/your_app')
      os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_app.settings')
      # Older django
      # import django.core.handlers.wsgi
      # application = django.core.handlers.wsgi.WSGIHandler()
      # Django 1.7+
      from django.core.wsgi import get_wsgi_application
      # Python 2
      # application = get_wsgi_application()
      # Python 3
      from dj_static import Cling
      application = Cling(get_wsgi_application())
    • Potential disappointment: nginx was not really faster – if this happens, you’ve probably not enabled threading support for uwsgi.
    • Solution: add the flag
      –enable-threadingFor more flags, e.g. –workers to set number of processes spawned, see uwsgi-docs.

tomfa • 2015-01-06

Previous Post

Next Post