Django 1.9 Apache错误导入django.core.handlers.wsgi ImportError:没有名为' django'

时间:2016-05-16 09:24:20

标签: python django apache

我尝试将我的Django项目部署到apache。但我得到一个错误500响应。在日志中我得到Django缺失的信息。我使用virtualenv来运行此项目。这是我第一次尝试部署Django项目时。根据我的经验,我知道我可能错过了一些简单的事情。我在这个网站上寻找解决方案,但它们适用于以前版本的Django和python。他们不为我工作。

这是我的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>

这是我的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))

错误记录

[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'

权限

-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

2 个答案:

答案 0 :(得分:0)

我有一些带django的apache服务器,当我设置它们时很痛苦。因此,使用静态文件的最小工作配置是:

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>

此外,您不应修改django项目中的wsgi.py文件。 Apache配置足以启动您的Web服务器。

编辑: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()

答案 1 :(得分:0)

永远不会太晚。可能这会帮助别人。我有同样的错误,它通过2次更改解决。下面是工作配置。我有Django 1.10,Python 3.5和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>