[Django]-Deploying Django with Virtualenv and Apache

2👍

Two potential mistakes

Django settings file must be a Python module

Based on input you give, in your case it is not a Python module and your folder structure is wrong

 sys.path.append('/var/www/kleyboldt_homepage')
 os.environ['DJANGO_SETTINGS_MODULE'] = 'kleyboldt_homepage.settings'

Above means that .py files in folder /var/www/kleyboldt_homepage go to top level Python namespace. E.g. settings.py file is module “settings”, not ‘kleyboldt_homepage.settings’.

Virtualenv path must be in sys.path

Here is an example django.wsgi. Please take this as guidelining example, not a tested solution specific your deployment:

# Must be in the project root or production deployment does not work
import os
import sys

from os.path import abspath, dirname, join

# This is /srv/django/yoursite
PROJECT_PATH=abspath(join(dirname(__file__), "."))

import site
import os

# Assume virtualenv is in relative subdirectory "venv" to the project root
vepath = PROJECT_PATH+'/venv/lib/python2.7/site-packages'

prev_sys_path = list(sys.path)
# add the site-packages of our virtualenv as a site dir
site.addsitedir(vepath)


# reorder sys.path so new directories from the addsitedir show up first
new_sys_path = [p for p in sys.path if p not in prev_sys_path]
for item in new_sys_path:
        sys.path.remove(item)
sys.path[:0] = new_sys_path

# import from down here to pull in possible virtualenv django install
from django.core.handlers.wsgi import WSGIHandler
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
application = WSGIHandler()

Leave a comment