3
votes

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

I would like to deploy the application with Django. Therefore, I changed all settings according to the documentation (especially, created a folder from where all collected static files can be served) and tried it out on my local development machine.

Because the site is now ready I pushed the whole project to the virtual server. I use Ubuntu 14.04 LTS both on my development machine and on the virtual host. Although I tested the project on my local machine with the 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 under /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()

The project structure under /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 the dependencies for this project I used the virtualenvwrapper to create a env under /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 reloading the apache2 server and refreshing the page I get a 500 Internal Server error. I looked it up in the debug file I specified 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

The import of django.core.handlers.wsgi seems to fail. I checked my python path specified behind the WSGIDaemonProcess but everything seems to be fine. But the import is still failing. Does anybody know how to fix this?

1
check out my question I posted and the answer I got. This may be what you are looking for: stackoverflow.com/questions/24739643/…Aaron Lelevier
But you have/had a different approach. I tried to use WSGIDaemonProcess which is the recommended method according to the official django documentation. Isn't there a solution for this approach. It worked on my local machine.Nick Lehmann
I don't think it's good idea to put your virtualenv in /root because your server processes will not be able to access it unless they are running with superuser permissions.kchan
I have only one user, root. So my processes will also run under rootNick Lehmann

1 Answers

2
votes

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()