Deploying Django with Virtualenv and Apache

I would like to deploy a site built with Django. The production environment is a rented virtual server.

I would like to deploy an application with Django. So I changed all the settings according to the documentation (especially, created a folder from which to load all the collected static files) and tried it on my local development machine.

Since the site is now complete, I moved the entire project to a virtual server. I am using Ubuntu 14.04 LTS on both my development machine and virtual host. Although I tested the project on my local machine using apache, I experienced some difficulties during the deployment phase. The project is called kleyboldt. My virtualenv is stored in the / root directory and the project lives in / var / www . Here are the important files:

/etc/apache2/sites-available/mks.conf

WSGIDaemonProcess mathias-kleyboldt-stiftung.de python-path=/var/www/kleyboldt_homepage$
WSGIProcessGroup mathias-kleyboldt-stiftung.de

<VirtualHost *:80>
        DocumentRoot /var/html/kleyboldt_homepage
        WSGIScriptAlias / /var/www/kleyboldt.wsgi
        ServerName mathias-kleyboldt-stiftung.de
        ServerAlias www.mathias-kleyboldt-stiftung.de

        <LocationMatch "\.(jpg|css|gif|pdf|ico)$">
                SetHandler None
        </LocationMatch>

        Alias /media/ /var/www/kleyboldt_homepage/static/media/
        Alias /static/ /var/www/kleyboldt_homepage/static/static-only/

        <Directory /var/www/kleyboldt_homepage/>
                Require all granted
                Order allow,deny
                Allow from all
        </Directory>

        <Directory /var/www/kleyboldt_homepage/static/static-only>
                Require all granted
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog /var/www/kleyboldt_homepage/apache_error.log
        LogLevel debug
</VirtualHost>

      

/var/www/kleyboldt.wsgi

import os
import sys

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

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

      

Project structure in / var / www / kleyboldt_homepage :

root@somewhere:/var/www/kleyboldt_homepage# ls
apache_error.log  homepage      index.html          manage.py  static
db.sqlite3        homepage.log  kleyboldt_homepage  site.txt

      

To manage dependencies for this project, I used virtualenvwrapper to create an env in / root / virtualenvs called kleyboldt-homepage:

root@somewhere:~/virtualenvs/kleyboldt-homepage/lib/python2.7/site-packages# ls
crispy_forms                               markdown2.pyc
django                                     markdown_deux
Django-1.6.5.dist-info                     _markerlib
django_crispy_forms-1.4.0-py2.7.egg-info   pagedown
django_grappelli-2.5.3-py2.7.egg-info      pip
django_markdown_deux-1.0.4-py2.7.egg-info  pip-1.5.4.dist-info
django_pagedown-0.1.0-py2.7.egg-info       pkg_resources.py
easy_install.py                            pkg_resources.pyc
easy_install.pyc                           setuptools
grappelli                                  setuptools-2.2.dist-info
markdown2-2.2.1-py2.7.egg-info             south
markdown2.py                               South-1.0-py2.7.egg-info

      

After restarting the apache2 server and refreshing the page, I get a 500 Internal Server error. I looked at it in the debug file listed in the apache conf file.

/var/www/kleyboldt_homepage/apache_error.log

[Mon Aug 18 17:04:50.226000 2014] [authz_core:debug] [pid 966:tid 139697743423232] mod_authz_core.c(802): [client 92.224.193.119:56235] AH01626: authorization result of Require all granted: granted
[Mon Aug 18 17:04:50.226104 2014] [authz_core:debug] [pid 966:tid 139697743423232] mod_authz_core.c(802): [client 92.224.193.119:56235] AH01626: authorization result of <RequireAny>: granted
[Mon Aug 18 17:04:50.226227 2014] [authz_core:debug] [pid 966:tid 139697743423232] mod_authz_core.c(802): [client 92.224.193.119:56235] AH01626: authorization result of Require all granted: granted
[Mon Aug 18 17:04:50.226239 2014] [authz_core:debug] [pid 966:tid 139697743423232] mod_authz_core.c(802): [client 92.224.193.119:56235] AH01626: authorization result of <RequireAny>: granted
[Mon Aug 18 17:04:50.241584 2014] [:info] [pid 965:tid 139697924556544] [remote 92.224.193.119:14076] mod_wsgi (pid=965, process='mathias-kleyboldt-stiftung.de', application='mathias-kleyboldt-stiftung.de|'): Loading WSGI script '/var/www/kleyboldt.wsgi'.
[Mon Aug 18 17:04:50.242108 2014] [:error] [pid 965:tid 139697924556544] [remote 92.224.193.119:14076] mod_wsgi (pid=965): Target WSGI script '/var/www/kleyboldt.wsgi' cannot be loaded as Python module.
[Mon Aug 18 17:04:50.242118 2014] [:error] [pid 965:tid 139697924556544] [remote 92.224.193.119:14076] mod_wsgi (pid=965): Exception occurred processing WSGI script '/var/www/kleyboldt.wsgi'.
[Mon Aug 18 17:04:50.242137 2014] [:error] [pid 965:tid 139697924556544] [remote 92.224.193.119:14076] Traceback (most recent call last):
[Mon Aug 18 17:04:50.242161 2014] [:error] [pid 965:tid 139697924556544] [remote 92.224.193.119:14076]   File "/var/www/kleyboldt.wsgi", line 7, in <module>
[Mon Aug 18 17:04:50.242215 2014] [:error] [pid 965:tid 139697924556544] [remote 92.224.193.119:14076]     import django.core.handlers.wsgi
[Mon Aug 18 17:04:50.242233 2014] [:error] [pid 965:tid 139697924556544] [remote 92.224.193.119:14076] ImportError: No module named django.core.handlers.wsgi

      

Importing django.core.handlers.wsgi seems to fail. I checked my python path listed behind WSGIDaemonProcess but everything seems to be fine. But the import still doesn't work. Does anyone know how to fix this?

+3


source to share


1 answer


Two possible mistakes

Django settings file must be a Python module

Based on the 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'

      



The above means that .py files in the / var / www / kleyboldt_homepage folder go to the top Python namespace. For example. The settings.py file is the "settings" of the module, not "kleyboldt_homepage.settings".

The virtual path must be in sys.path

Here's an example django.wsgi

. Please take this as an example guide and not a tested solution specific to 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()

      

+2


source







All Articles