【问题标题】:Deploying Django on Shared Hosting (1and1) throws 500在共享主机(1and1)上部署 Django 会抛出 500
【发布时间】:2014-09-05 06:21:23
【问题描述】:

我在 Django 上开发了一个项目,该项目在本地使用经典可以顺利运行:

python manage.py runserver

虽然python manage.py runfcgi 命令会抛出我网站的html 代码,但我在尝试通过创建的子域访问我的网站时遇到了一些问题。

场景:给定正确安装的 django 和 Flup (python -c "import django") 版本,我的 1and1 服务器的主文件夹中有以下结构:

/bla/bla/bla/test_django
    |-> manage.py
    |-> test_django
    |   |-> __init__.py
    |   |-> settings.py
    |   |-> urls.py
    |   |-> wsgi.py
    |-> dispatch.fcgi
    |-> .htaccess

虽然我以前认为.htaccess 应该放在我的“根”文件夹中,但我也将它复制到了test_django 文件夹中。内容如下:

AddHandler fcgid-script .fcgi RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ dispatch.fcgi/$1 [QSA,L]

我的dispatch.fcgi(已经改成755)的内容如下:

import sys
import os

sys.path.insert(0, '~/.local/lib/python2.6/site-packages')
os.environ['DJANGO_SETTINGS_MODULE'] = 'test_django.settings'

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")

sys.path.insert 行是安装的本地版本的python(不确定是否正确链接。但是,我不确定它的用途。无论如何,如前所述,使用python manage.py runfcgi 或使用./dispatch.fcgi,显示的输出是网站的HTML。

我现在的问题是:除了将我的子域 test.whateverdomain.com 指向我的/bla/bla/bla/test_django 文件夹之外,是否有必要做其他事情?这会引发 500 错误。我害怕错过对我来说最重要的一点,那就是将用户重定向到将加载内容的可执行文件的事实。

【问题讨论】:

  • 你真的应该考虑使用 WSGI 和 Gunicorn 而不是快速 cgi 进行部署
  • 查看您的主机教程,了解如何部署 django 应用程序。
  • 不幸的是,1and1 缺少这样的文档。我将尝试找到有关 WSGI 的新教程。我是否必须将我的域指向 /bla/bla/bla/test_django/test_django 文件夹中的 wsgi.py 文件?

标签: python django .htaccess shared-hosting


【解决方案1】:

您应该将 .fcgi 文件放在名为 cgi-bin 的文件夹中。有关更多说明,请参阅Installing a Django app on 1and1 Linux shared hosting

【讨论】:

  • @makerGeek 使用了 Internet 存档缓存。也许原来的网站稍后会再次启动,但这会起作用。
【解决方案2】:

确保您修改系统路径的调用正确,以便可以访问必要的 python 包,这一点绝对重要。

FCGI 文件中的那些行应该类似于:

sys.path.insert(0, "/home#/username/local_usr/lib/python2.7/site-packages")
sys.path.append("/home#/username/django_projects/mynewsite")

第一行是你的python环境,第二行是你的Django项目。我建议使用 virtualenv,因此(将 python 环境的位置更改为包含在 dir '.env' 中,virtualenv 名为 'env')你的整个 FCGI 文件应该如下所示:

#!/home#/username/.env/env/bin/python
import sys
import os

sys.path.insert(0, "/home#/username/.env/env/lib/python2.7/site-packages")
sys.path.append("/home#/username/django_projects/test_django")
os.environ['DJANGO_SETTINGS_MODULE'] = 'test_django.settings'

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")

注意:第一行不是评论。

信用

http://www.binaryadventure.com/2013/10/23/django-on-bluehost-in-five-minutes/ https://help.asmallorange.com/index.php?/Knowledgebase/Article/View/305/0/installing-django-using-virtualenv

【讨论】:

    猜你喜欢
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多