【问题标题】:Python Django webpage works manually but domain gives 404 error on shared hostPython Django 网页手动工作,但域在共享主机上出现 404 错误
【发布时间】:2016-01-17 19:49:36
【问题描述】:

我正在使用 justhost(共享主机)在我的子域 dev.domain_name.com 上测试 python django 网页

我使用以下教程:http://flailingmonkey.com/install-django-justhost/ 获得以下输出。

获取以下内容的命令:python mysite.fcgi

WSGIServer: missing FastCGI param REQUEST_METHOD required by WSGI!
WSGIServer: missing FastCGI param SERVER_NAME required by WSGI!
WSGIServer: missing FastCGI param SERVER_PORT required by WSGI!
WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!
Status: 200 OK
X-Frame-Options: SAMEORIGIN
Content-Type: text/html


<!DOCTYPE html>
<html lang="en"><head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="robots" content="NONE,NOARCHIVE"><title>Welcome to Django</title>
  <style type="text/css">
    html * { padding:0; margin:0; }
    body * { padding:10px 20px; }
    body * * { padding:0; }
    body { font:small sans-serif; }
    body>div { border-bottom:1px solid #ddd; }
    h1 { font-weight:normal; }
    h2 { margin-bottom:.8em; }
    h2 span { font-size:80%; color:#666; font-weight:normal; }
    h3 { margin:1em 0 .5em 0; }
    h4 { margin:0 0 .5em 0; font-weight: normal; }
    table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; }
    tbody td, tbody th { vertical-align:top; padding:2px 3px; }
    thead th {
      padding:1px 6px 1px 3px; background:#fefefe; text-align:left;
      font-weight:normal; font-size:11px; border:1px solid #ddd;
    }
    tbody th { width:12em; text-align:right; color:#666; padding-right:.5em; }
    #summary { background: #e0ebff; }
    #summary h2 { font-weight: normal; color: #666; }
    #explanation { background:#eee; }
    #instructions { background:#f6f6f6; }
    #summary table { border:none; background:transparent; }
  </style>
</head>

<body>
<div id="summary">
  <h1>It worked!</h1>
  <h2>Congratulations on your first Django-powered page.</h2>
</div>

<div id="instructions">
  <p>
    Of course, you haven't actually done any work yet. Next, start your first app by running <code>python manage.py startapp [app_label]</code>.
  </p>
</div>

<div id="explanation">
  <p>
    You're seeing this message because you have <code>DEBUG = True</code> in your Django settings file and you haven't configured any URLs. Get to work!
  </p>
</div>
</body></html>


Tech Stack:
Python 2.7.2
flup 1.0.2
django 1.8.7

**My subdomain dev.domain_name.com returns:**
Not Found The requested URL /mysite.fcgi/ was not found on this server.

Below is .htaccess & mysite.fcgi

[~/public_html/subdomaindirectory]# vi mysite.fcgi
#!/home2/findity/python/bin/python
import sys, os

sys.path.insert(0, "/home2/findity1/python")

path = '/home2/findity1/public_html/findityaardev/mysite'
if path not in sys.path:
   sys.path.append(path)

print(sys.path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")


.htaccess file:
[~/public_html/subdomaindirectory]# vi .htaccess

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

注意:我从 cpanel 添加了 .fcgi apache 处理程序,这使我从 500 内部服务器错误变为 404。看起来它是根据命令手动工作的,但是我无法在我的子域中看到它。如果需要更多信息,请告诉我。

【问题讨论】:

  • “它成功了!恭喜你获得了第一个 Django 驱动的页面。” - 这是你第一次启动django时得到的默认页面,什么不起作用?
  • 它在域 (dev.domain_name.com) 上不起作用,我收到 404 错误 Not Found 在此服务器上找不到请求的 URL /mysite.fcgi/。当我在服务器“python mysite.fcgi”上手动运行它时,它会显示成功输出,包括以下内容: WSGIServer: missing FastCGI param REQUEST_METHOD required by WSGI! WSGIServer:缺少 WSGI 所需的 FastCGI 参数 SERVER_NAME! WSGIServer:缺少 WSGI 所需的 FastCGI 参数 SERVER_PORT! WSGIServer:缺少 WSGI 所需的 FastCGI 参数 SERVER_PROTOCOL!
  • 你能告诉我们一些关于文件夹结构的信息吗?
  • 级别 1:子域的根目录:/home2/username/public_html/subdomain/ 级别 2:子域根目录下的文件和文件夹:/home2/username/public_html/subdomain/: mysite.fcgi .htaccess mysite / 级别 3:mysite 下的文件和文件夹:/home2/username/public_html/subdomain/mysite:manage.py 级别 4:/home2/username/public_html/subdomain/mysite/mysite:init.py settings.py settings.pyc urls.py urls.pyc wsgi.py wsgi.pyc

标签: python django shared-hosting


【解决方案1】:

先看Django 1.8 documentation中的注释:

从 1.7 版开始:
FastCGI 支持已弃用,将在 Django 1.9 中删除。

因此可能会首选另一种方法。

我注意到您的mysite.fcgi 有一个打印声明。删除它,我认为 fastcgi 进程无法访问标准输出 (it goes to the server instead)。这可能会导致问题。

如果这不能解决问题,首先要尝试找出错误所在。我认为它最有可能发生在以下地方之一:

  • 重定向不起作用。
  • 或者 Apache 找不到 mysite.fcgi。
  • 或者 mysite.fcgi 调用返回 404 的 Django。
  • 或者也许 Django 正在工作,但使用返回 404 的视图。

查看 Apache error.log 文件,看看这是否超出了关于其中哪一个是问题区域的任何线索。

【讨论】:

  • 谢谢布赖恩,我删除了评论,但没有区别,我目前在我的网址上收到 [处理此指令时发生错误]。不幸的是,由于共享主机,我无法访问 apache 设置和日志。我正在使用具有 fastcgi 库的 1.8.7 django,在共享主机上运行命令 python mysite.fcgi 也会提供上述输出,因此它能够识别 fastcgi。我相信这可能是我的 .htaccess 文件(尤其是路径)。
【解决方案2】:

接下来是我的 .htaccess 和 django.fcgi 文件。
这些文件在 JustHost 中正常工作。 我目前正在使用这种配置在 Justhost 中运行 3 个由 Django 驱动的网站。

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

我正在使用一个简约的 .fcgi 文件(在我的例子中是 django.fcgi)

#!/home/.../venv/bin/python
# DO NOT USE SOMETHING LIKE THIS #!/usr/bin/env python
import sys, os

# Add a custom Python path.
sys.path.insert(0, "/home/.../django_project_directory")

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "my_project_name.settings"

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

编辑:
我正在使用 Django 1.8.x。我目前没有任何使用 Django 1.9.x 的项目

【讨论】:

  • 感谢 ePi272314 的回复。我的网站仍然显示以下错误:[处理此指令时发生错误] 我的文件 .htaccess: AddHandler fcgid-script .fcgi RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ mysite.fcgi/$1 [QSA,L] 我的文件 mysite.fcgi: import sys, os sys.path.insert(0, "/home2/findity1/public_html/findityaardev/mysite") os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' from django.core.servers.fastcgi import runfastcgi runfastcgi(method="threaded", daemonize="false") 我的文件夹结构或其他什么不正确?
  • 你好。您的 .htaccess 文件看起来不错。
  • 不管 mysite.fcgi 文件: * 确保它是可执行的 "chmod +x mysite.fcgi" * 你错过了我示例的第一行:"#!absolute_path_to_python_executable"。 * 但是,如果您使用的是虚拟环境,那么请指明该虚拟环境中 python 的路径。 * "sys.path.insert" 中的路径指向你的 django 项目目录,而不是你的 public_html/mysite 目录。我假设您的 public_html 中没有您的 django 项目,因为它不安全。
  • 其他可能的问题: * 你必须安装flup:激活你的虚拟环境和“pip install flup==1.0.2”。 * 请记住在设置文件中更改 ALLOWED_HOSTS:ALLOWED_HOSTS = ["localhost", ".mysite.com",]。 * 如果您使用的是 linux 服务器,但您使用 windows 生成文件 .htaccess 和 fcgi 有时您可能会遇到问题。使用“dos2unix .htaccess”和“dos2unix mysite.fcgi”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多