【问题标题】:Error while uploading Django project to a Apache server via mod_wsgi通过 mod_wsgi 将 Django 项目上传到 Apache 服务器时出错
【发布时间】:2015-10-21 17:36:19
【问题描述】:

我在 DigitalOcean 上创建了一个 droplet(云服务器),并使用 no-ip.com 我给了它主机名 - project.ddns.net。通过 ssh(ing) 进入我安装了 pip 和 virtualenv 的 droplet。

在 /var/www/ 我创建了一个 virtualenv 并从我的项目的 github 克隆了存储库。目录结构是 -

project_revamped  (root of the project)
->requirements
  ->base.txt
  ->dev.txt
->project (django project)
   ->static
   ->media
   ->apps (folder which contains apps)
   ->manage.py
   ->project
      ->urls.py
      ->settings
          ->base.py
          ->dev.py  

按照官方 Django 文档,我在 /etc/apache2 路径中创建了 httpd.conf 并将其包含在 apache2.conf 中。

我的 httpd.confs 是这样写的 -

WSGIScriptAlias / /var/www/project_revamped/project/project/wsgi.py
WSGIPythonPath     /var/www/project_revamped/project:/var/www/.virtualenvs/projectenv/local/l    ib/python2.7/site-packages
<Directory /var/www/project_revamped/project/project>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

我的 wsgi.py 是这样写的 -

import os
import sys

#Add the app's directory to the python path
sys.path.append('/var/www/project_revamped/project')
sys.path.append('/var/www/project_revamped/project/project')

os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings.dev'

#Activate your virtualenv
activate_env =     os.path.expanduser('/var/www/.virtualenvs/typesetenv/bin/activate_this.py')
execfile(activate_env, dict(__file__=activate_env))

from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()

更改文件后,我终于给出了命令 - 服务 apache2 重新加载 服务 apache2 重启

但是,在正确完成这些事情之后,相应的 ip 说服务器存在问题并发送 500 错误。我猜问题出在我的配置中,因为 apache 服务器响应正常。在我包含 django 项目后问题开始。

检查错误日志后,我发现了这些错误消息 -

mod_wsgi (pid=29458): Target WSGI script '/var/www/project_revamped/project/project/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=29458): Exception occurred processing WSGI script '/var/www/project_revamped/project/project/wsgi.py'.
 File "/var/www/project_revamped/project/project/wsgi.py", line 28, in     <module>
[:error] [pid 29458:tid 140073924572928] [client 103.16.70.147:33613]     application = get_wsgi_application()

有人可以帮我配置一下吗?在过去的 2 天里,我陷入了困境,互联网上的每一篇不同的文章都讲述了不同的故事。

【问题讨论】:

  • 我认为你的virtualenv在httpd.conf文件中的路径可能有问题,因为最近在部署过程中我们也遇到了这个问题,这是由于路径问题。
  • 你确定这是所有记录的消息。除了回溯之外,通常还会记录异常类型和消息。

标签: python django apache mod-wsgi


【解决方案1】:

将此行添加到您的 wsgi.py 文件中:sys.path.append('/var/www/project_revamped/project')os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings.dev' 之前并检查它是否有效。

【讨论】:

  • 好吧,我的错!该行已经存在于我的 wsgi.py 文件中。我将更新问题
【解决方案2】:

您可以查看我的带有 mod_wsgi 的 Apache 虚拟主机配置。此模板可以在项目创建期间自动填充,但您可以填充/替换变量并将其剥离到您的需要:

<VirtualHost *:80>
    # This is name based virtual hosting. So place an appropriate server name
    #   here. Example: django.devsrv.local
    ServerName  [[SERVER_NAME]]
    ServerAdmin webmaster@localhost

    # This alias makes serving static files possible.
    #   Please note, that this is geared to our settings/common.py
    #   In production environment, you will propably adjust this!
    Alias /static/  {{ project_directory }}/run/static/

    # This alias makes serving media files possible.
    #   Please note, that this is geared to our settings/common.py
    #   In production environment, you will propably adjust this!
    Alias /media/  {{ project_directory }}/run/media/

    # Insert the full path to the wsgi.py-file here
    WSGIScriptAlias /   {{ project_directory }}/{{ project_name }}/wsgi.py

    # PROCESS_NAME specifies a distinct name of this process
    #   see: https://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIDaemonProcess
    # PATH/TO/PROJECT_ROOT is the full path to your project's root directory, 
    #   containing your project files
    # PATH/TO/VIRTUALENV/ROOT: If you are using a virtualenv specify the full
    #   path to its directory.
    #   Generally you must specify the path to Python's site-packages.
    WSGIDaemonProcess   {{ project_name }}  python-path={{ project_directory }}:{{ project_directory }}/../lib/python2.7/site-packages

    # PROCESS_GROUP specifies a distinct name for the process group
    #   see: https://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIProcessGroup
    WSGIProcessGroup    {{ project_name }}

    # Serving static files from this directory
    #   Please note, that this is geared to our settings/common.py
    #   In production environment, you will propably adjust this!
    <Directory {{ project_directory }}/run/static>
        Options -Indexes
        Order deny,allow
        Allow from all
    </Directory>

    # Serving media files from this directory
    #   Please note, that this is geared to our settings/common.py
    #   In production environment, you will propably adjust this!
    <Directory {{ project_directory }}/run/media>
        Options -Indexes
        Order deny,allow
        Allow from all
    </Directory>

    LogLevel warn

    # PROJECT_NAME is used to seperate the log files of this application
    ErrorLog    ${APACHE_LOG_DIR}/{{ project_name }}_error.log
    CustomLog   ${APACHE_LOG_DIR}/{{ project_name }}_access.log combined
</VirtualHost>

您可以在GitHubhere 上看到我的项目骨架是 RTD.org 上的 Apache2-conf 文档

【讨论】:

    猜你喜欢
    • 2012-01-15
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 2017-01-16
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多