簡體   English   中英

目標 WSGI 腳本無法加載為 Python 模塊 + 沒有名為 Django 的模塊

[英]Target WSGI script cannot be loaded as Python module + no module named Django

我知道這里已經有人問過這個問題了。 我已經閱讀了大量的答案,但沒有任何幫助。

我正在使用 virtualenv (/root/eb-virt); 我的 Django 項目被稱為媒體。 所有公共文件都存儲在 /var/www/html/xxx.com/public_html/

從錯誤日志:

[Sun Oct 16 18:40:11.737747 2016] [:error] [pid 27659] [client 128.75.240.205:60486] mod_wsgi (pid=27659): Target WSGI script '/var/www/html/xxx.com/django.wsgi' cannot be loaded as Python module.
[Sun Oct 16 18:40:11.737808 2016] [:error] [pid 27659] [client 128.75.240.205:60486] mod_wsgi (pid=27659): Exception occurred processing WSGI script '/var/www/html/xxx.com/django.wsgi'.
[Sun Oct 16 18:40:11.745983 2016] [:error] [pid 27659] [client 128.75.240.205:60486] Traceback (most recent call last):
[Sun Oct 16 18:40:11.746153 2016] [:error] [pid 27659] [client 128.75.240.205:60486]   File "/var/www/html/xxx.com/django.wsgi", line 9, in <module>
[Sun Oct 16 18:40:11.746165 2016] [:error] [pid 27659] [client 128.75.240.205:60486]     import django.core.handlers.wsgi
[Sun Oct 16 18:40:11.746191 2016] [:error] [pid 27659] [client 128.75.240.205:60486] ImportError: No module named 'django'

我的 apache2.conf:

Mutex file:${APACHE_LOCK_DIR} default

PidFile ${APACHE_PID_FILE}

Timeout 300

KeepAlive Off

MaxKeepAliveRequests 100

KeepAliveTimeout 5

User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

HostnameLookups Off

ErrorLog ${APACHE_LOG_DIR}/error.log

LogLevel warn

IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf

Include ports.conf

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>

<Directory /usr/share>
    AllowOverride None
    Require all granted
</Directory>

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

#<Directory /srv/>
#   Options Indexes FollowSymLinks
#   AllowOverride None
#   Require all granted
#</Directory>

AccessFileName .htaccess

<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>

LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

IncludeOptional conf-enabled/*.conf

IncludeOptional sites-enabled/*.conf

<IfModule mpm_prefork_module>
    StartServers 4
    MinSpareServers 20
    MaxSpareServers 40
    MaxClients 200
    MaxRequestsPerChild 4500
</IfModule>


LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so

WSGIScriptAlias / /var/www/html/xxx.com/public_html/mediar/mediar/wsgi.py
WSGIPythonPath /var/www/html/xxx.com/public_html:/root/eb-virt/lib/python3.4/site-packages/

<Directory /var/www/html/xxx.com/public_html/mediar/mediar/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

我的 xxx.com.conf:

<VirtualHost *:80>
  ServerAdmin xxx@xxx.com
  ServerName  xxx.com
  ServerAlias www.xxx.com

  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.html index.php
  DocumentRoot /var/www/html/xxx.com/public_html
  # Log file locations
  LogLevel warn
  ErrorLog  /var/www/html/xxx.com/log/error.log
  CustomLog /var/www/html/xxx.com/log/access.log combined
WSGIScriptAlias / /var/www/html/xxx.com/django.wsgi
</VirtualHost>

最后,我的 django.wsgi:

#!/usr/bin/python 
import os, sys 

PROJECT_ROOT = '/var/www/html/xxx.com/' 
sys.path.append(PROJECT_ROOT) 

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

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

我試過了

$ sudo apt-get remove libapache2-mod-python libapache2-mod-wsgi
$ sudo apt-get install libapache2-mod-wsgi-py3

,它沒有幫助。

我該怎么辦?

我會首先嘗試在您的 wsgi 文件中使用正確的 python shebang。 不應該是下面這樣的嗎?

#!/root/eb-virt/bin/python

然后第二件事是將 virtualenv 顯式添加到 wsgi 文件中的sys.path附加

sys.path.append('/root/eb-virt/lib/python3.4/site-packages')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM