0

I try to deploy my Django project to apache. But I getting an error 500response. And in logs I getting information that Django is missing. I'm using virtualenvto run this project. It's the first time when I try to deploy Django project. And from my experience, I know that I probably missing some simple thing. I was looking for solutions on this site but they are for previous versions of Django and python. They don't work for me.

This is my Apache test.conf

WSGIScriptAlias / /home/mariusz/Dokumenty/Projekty/zalien/zalien/wsgi.py 
WSGIDaemonProcess localhost python-path=/home/mariusz/Dokumenty/Projekty/zalien:/home/mariusz/Dokumenty/Projekt/envy/lib/python3.5/site-packages

<Directory /home/mariusz/Dokumenty/Projekty/zalien/zalien>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

This is my wsgi.py

import os
import sys
import site
from django.core.wsgi import get_wsgi_application
application = django.core.handlers.wsgi.WSGIHandler()

# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('/home/mariusz/Dokumenty/Projekty/envy/local/lib/python3.5/site-packages')


# Add the app's directory to the PYTHONPATH
sys.path.append('/home/mariusz/Dokumenty/Projekty/zalien')
sys.path.append('/home/mariusz/Dokumenty/Projekty/zalien/zalien')

os.environ['DJANGO_SETTINGS_MODULE'] = 'zalien.settings'

# Activate your virtual env
activate_env=os.path.expanduser("/home/mariusz/Dokumenty/Projekty/envy/bin/activate_this.py")
exec(activate_env, dict(__file__=activate_env))

Error Log

[Mon May 16 09:44:28.368084 2016] [wsgi:error] [pid 7418:tid 139640747427584] mod_wsgi (pid=7418): Call to 'site.addsitedir()' failed for '(null)', stopping.
[Mon May 16 09:44:28.368132 2016] [wsgi:error] [pid 7418:tid 139640747427584] mod_wsgi (pid=7418): Call to 'site.addsitedir()' failed for '/home/mariusz/Dokumenty/Projekty/zalien/'.
[Mon May 16 09:44:28.369458 2016] [wsgi:error] [pid 7418:tid 139640747427584] [client 127.0.0.1:37494] mod_wsgi (pid=7418): Target WSGI script '/home/mariusz/Dokumenty/Projekty/zalien/zalien/wsgi.py' cannot be loaded as Python module.
[Mon May 16 09:44:28.369493 2016] [wsgi:error] [pid 7418:tid 139640747427584] [client 127.0.0.1:37494] mod_wsgi (pid=7418): Exception occurred processing WSGI script '/home/mariusz/Dokumenty/Projekty/zalien/zalien/wsgi.py'.
[Mon May 16 09:44:28.369724 2016] [wsgi:error] [pid 7418:tid 139640747427584] [client 127.0.0.1:37494] Traceback (most recent call last):
[Mon May 16 09:44:28.369752 2016] [wsgi:error] [pid 7418:tid 139640747427584] [client 127.0.0.1:37494]   File "/home/mariusz/Dokumenty/Projekty/zalien/zalien/wsgi.py", line 23, in <module>
[Mon May 16 09:44:28.369758 2016] [wsgi:error] [pid 7418:tid 139640747427584] [client 127.0.0.1:37494]     from django.core.wsgi import get_wsgi_application
[Mon May 16 09:44:28.369781 2016] [wsgi:error] [pid 7418:tid 139640747427584] [client 127.0.0.1:37494] ImportError: No module named 'django'

Permissions

-rw-rw-r-- 1 mariusz www-data   295 May  9 14:50 app.json
-rw-rw-r-- 1 mariusz www-data 51200 May 13 09:53 db.sqlite3
drwxrwxr-x 4 mariusz www-data  4096 May 13 10:06 games
-rwxrwxr-x 1 mariusz www-data   249 May  9 14:50 manage.py
drwxrwxr-x 4 mariusz www-data  4096 May 13 10:06 portal
-rw-rw-r-- 1 mariusz www-data    39 May  9 14:50 Procfile
-rw-rw-r-- 1 mariusz www-data    45 May  9 14:50 Procfile.windows
-rw-rw-r-- 1 mariusz www-data  1368 May  9 14:50 README.md
-rw-rw-r-- 1 mariusz www-data   298 May  9 14:50 requirements.txt
-rw-rw-r-- 1 mariusz www-data    13 May  9 14:50 runtime.txt
drwxrwxr-x 5 mariusz www-data  4096 May  9 14:50 static
drwxrwxr-x 4 mariusz www-data  4096 May  9 14:50 templates
drwxrwxr-x 4 mariusz www-data  4096 May 13 10:06 userprofile
drwxrwxr-x 3 mariusz www-data  4096 May 16 11:50 zalien
  • Possible duplicate of [django apache configuration with WSGIDaemonProcess not working](http://stackoverflow.com/questions/38284814/django-apache-configuration-with-wsgidaemonprocess-not-working) – e4c5 Jul 19 '16 at 04:32

2 Answers2

0

I got a few apache servers with django and it was pain when i set up them. So minimum working configuration with static files is:

WSGIPythonPath /home/mariusz/Dokumenty/Projekty/zalien

<VirtualHost *:80>
    WSGIScriptAlias / /home/mariusz/Dokumenty/Projekty/zalien/zalien/wsgi.py

    <Directory /home/mariusz/Dokumenty/Projekty/zalien/zalien>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    # Django static files, here you must specify your own path's
    Alias /static/ /var/www/djangointra/static/
    <Directory "/var/www/djangointra/static">
        Require all granted
    </Directory>
</VirtualHost>

Also you should not modify your wsgi.py files in your django projects. Apache configuration is enough to start your web-server.

EDIT: wsgi.py

"""
WSGI config for djago_api project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djago_api.settings")

application = get_wsgi_application()
valex
  • 5,163
  • 2
  • 33
  • 40
  • Can you paste a default wsgi.py file. Mine looks like `import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "zalien.settings") from django.core.wsgi import get_wsgi_application from whitenoise.django import DjangoWhiteNoise application = get_wsgi_application() application = DjangoWhiteNoise(application)` And I still get this same error – Mariusz Łaszewski May 16 '16 at 10:39
  • @MariuszŁaszewski yes :). Watch EDIT. – valex May 16 '16 at 10:41
  • Still the same as u can see above. – Mariusz Łaszewski May 16 '16 at 10:46
  • @MariuszŁaszewski check out file permissions and permissions for path to file. I mean you web server must be able to read '/home/mariusz/Dokumenty/Projekty/zalien/zalien/wsgi.py' file. If you use linux make sure you got o+rx in every dir in your path, and o+r for wsgi.py file. – valex May 16 '16 at 10:50
  • As You can see above. I give all of them a+x – Mariusz Łaszewski May 16 '16 at 10:55
  • And my '-rwxrwxr-x 1 mariusz www-data 913 May 16 11:50 wsgi.py' – Mariusz Łaszewski May 16 '16 at 10:57
  • Full path got a+rx? I mean every dir in this path ''/home/mariusz/Dokumenty/Projekty/zalien/zalien/' ? This error 'Target WSGI script '/home/mariusz/Dokumenty/Projekty/zalien/zalien/wsgi.py' cannot be loaded as Python module' usually means that something wrong with paths or with paths. Or in python packages. Check out first answer here: http://stackoverflow.com/questions/6454564/target-wsgi-script-cannot-be-loaded-as-python-module . – valex May 16 '16 at 10:59
0

Never too late. May be this will help someone else. I had the same error and it is resolved by 2 changes. Below is the working config. I had Django 1.10, Python 3.5 and Apache 2.4

wsgi.pi

import os, sys, site

from django.core.wsgi import get_wsgi_application


site.addsitedir("/home/mariusz/Dokumenty/Projekt/envy/lib/python3.5/site-packages")

sys.path.append("/home/mariusz/Dokumenty/Projekty/zalien")
sys.path.append("/home/mariusz/Dokumenty/Projekty/zalien/zalien")

activate_this = "/home/mariusz/Dokumenty/Projekty/envy/bin/activate_this.py"
with open(activate_this) as f:
    code = compile(f.read(), activate_this, "exec")
    exec(code, dict(__file__=activate_this))

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "zalien.settings")

application = get_wsgi_application()

Apache test.conf

<VirtualHost *:80>
       WSGIDaemonProcess zalien processes=2 threads=12 python-home=/home/mariusz/Dokumenty/Projekt/envy/ python-path=/home/mariusz/Dokumenty/Projekty/zalien:/home/mariusz/Dokumenty/Projekt/envy//lib/python3.5/site-packages
       WSGIProcessGroup zalien
       WSGIScriptAlias / /home/mariusz/Dokumenty/Projekty/zalien/wsgi.py process-group=zalien
       ErrorLog ${APACHE_LOG_DIR}/error.log
       CustomLog ${APACHE_LOG_DIR}/access.log combined

       Alias /static/ /var/www/html/static/
         <Directory /var/www/html/static/>
             Require all granted
         </Directory>

         <Directory /home/mariusz/Dokumenty/Projekty/zalien>
            <Files wsgi.py>
              Require all granted
            </Files>
         </Directory>
</VirtualHost>