-->

Django 2.0.7 with fastcgi gives 404 in browser but

2019-08-28 07:55发布

问题:

I'm trying to set up a small django project on a bluehost shared server and am having trouble on what I think should be the last step - getting it running with fastcgi.

I installed python 3.7.0 and django 2.0.7 using miniconda and was able to create a project/app but i can't get it to display in a browser (FWIW, I have successfully done this with another bluehost site on a similar plan, though that site is under the "shared plus" plan while this one is just the basic, but I don't know if that's the issue)

In my ~/public_html/myproject/.htaccess file I have:

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

And in myproject.fcgi I have:

#!/home/username/miniconda3/bin/python
# -*- coding: utf-8 -*-
import sys, os
project_name = "myproject"

sys.stdout=open('/home/username/public_html/myproject/test.out','w')
print(project_name)

# Add a custom Python path.
sys.path.insert(0, "/home/username/miniconda3/bin/python")
sys.path.insert(0, "/home/username/public_html/myproject")
sys.path.insert(0, os.getcwd() + "/" + project_name)

os.environ['DJANGO_SETTINGS_MODULE'] = project_name + '.settings'

from django_fastcgi.servers.fastcgi import runfastcgi
from django.core.servers.basehttp import get_internal_wsgi_application

wsgi_application = get_internal_wsgi_application()
runfastcgi(wsgi_application, method="prefork", daemonize="false", minspare=1, maxspare=1, maxchildren=1)

This configuration worked for me on the other site, but here I am only able to run ./myproject.fcgi successfully on the command line, but I get a 404 in the browser.

I'm unable to access my server logs so I added a line to the fcgi script to direct stdout to a file and find that the file is not produced, so I'm not sure if something's wrong with accessing my python install when I try to load the page in the browser, or what (I'm pretty new to this!)

I found practically my exact question here, unfortunately without an answer: Django with FastCGI gives 404 in browser but works on command line

Thanks!!

回答1:

Update: I ended up determining that the issue I was facing was due to the limitations of my shared hosting environment. For starters, installation and troubleshooting was difficult without root access or access to apache error logs, but I was able to get an installation working, and made additional progress when I realized that my bluehost plan required cgi scripts to run from the cgi-bin dir (rather than adding handlers in .htaccess as most tutorials suggested).

However, the final hurdle I couldn't overcome seemed to be a limit on the number of spawned threads - when I tried to load the site (django test page) in the browser, it would just hang and I would get an error in my shell session: -jailshell: fork: Resource temporarily unavailable, as described here. … it's possible that someone with more experience could have figured it out but I finally decided it would be easier to switch. It took me longer to give up than it should have, since I have successfully done the exact same thing on another bluehost shared server account, but I finally learned that bluehost has updated their plans and the other one only worked since it was on an older version of the plan.

I switched to dreamhost (with their shared server plan) and was able to get everything running easily!