views:

1497

answers:

2

Firstly, here's my script:

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

sys.path.append('/home/username/python')
sys.path.append("/home/username/python/flup")
sys.path.append("/home/username/python/django")
# more path stuff

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

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

As was described here.

And here's the error I get when trying to run it from shell:

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: 404 NOT FOUND
Content-Type: text/html


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html lang="en">
<!-- more html which looks to be the correct output -->

My question is, why aren't those params passed automatically by FastCGI? What am I doing wrong? Running the script from my web server just gives me an internal server error.


Instead of the last two lines of my script, I can use

from flup.server.fcgi import WSGIServer
from django.core.handlers.wsgi import WSGIHandler
WSGIServer(WSGIHandler()).run()

But I still get the exact same error...

+2  A: 

The script expects those params to be passed as environment variables. Since they are not present in your shell environment, and the script is not running in the apache fastcgi environment (which provides them), it complains.

Do you have access to apache error logs? What do they say?

Does your host have mod_wsgi support? If so, you could use Django's wsgi handler:

import sys
import os

base = os.path.dirname(os.path.abspath(__file__)) + '/..'
sys.path.append(base)

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

import django.core.handlers.wsgi

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

Further instructions can be found on the modwsgi wiki, and the Django docs.

vezult
This gives me no output at all when running from shell, and the usual internal server error when running from my browser. I'm not sure if my server supports WSGI, nor do I know how to check.
Mark
Also, I'm not sure what you mean exactly but "not running in a fastcgi environment". My host claims they support FastCGI, and I can save stuff as .fcgi and have it run... what exactly is going on if it's not FastCGI, and how do I phrase this so I can ask my hosting providers to enable it?
Mark
What I mean is that Apache passes information to your fastcgi process via environment variables. Your shell does not contain that information unless you specifically add it. Since you did not set those variables in your shell environment when you ran your script from the commandline, your fasgcgi script does not have the information that it requires, hence the error. The error you describe at the commandline is not neccessarily related to whatever error is preventing your script from running under apache.
vezult
Ohh..right.. commandline = no apache. Well, is there anyway I can get some more feedback than "internal server error", or check what apache modules are installed?
Mark
Sorry without your config info, or error logs I can't help.
vezult
+1  A: 

Solved it. This .htaccess file did the trick, for whatever reason. I swear I tried all this before...

AddHandler fcgid-script .fcgi
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(adminmedia/.*)$ - [L]
RewriteCond %{REQUEST_URI} !(cgi-bin/myproject.fcgi)
RewriteRule ^(.*)$ cgi-bin/myproject.fcgi/$1 [L]
Mark