views:

523

answers:

4

I am trying to configure the Python mini-framework CherryPy with FastCGI (actually fcgid) on Apache. I am on a shared host, so I don't have access to httpd.conf, just htaccess. I have followed these tutorials to no avail:

I keep getting 500 errors w/ the Apache logs saying "Premature end of script headers". I have tried everything (permissions/shebangs/full-paths/deamonized/not-daimonized). I know Apache is correctly executing my .fcgi, because I am able to print to the error log from python, but that's it. Has anyone out there successfully installed CherryPy or any other framework on a shared host before? Your help would be greatly appreciated. Thanks.

+1  A: 

Apache + Bluehost + fastcgi + cherrypy + wsgi is unfortunately a lot of pieces. I wish I had a year to write the Definitive Guide for you, but alas. You might gain some insight from the rather long mailing list thread which resulted in those links you posted.

fumanchu
I've been debugging on my own linux box first, to control the situation as much as possible.Thanks for the link. It is a rather long conversation. Hope they can make things more seamless in the next version
arshaw
A: 

An idea: make sure your .fcgi file has a reference to the correct python executable in the initial line:

#!/usr/bin/python

I had to get Django running with fcgi on Bluehost and apache using the wrong python environment was my problem (worked from the shell, but not from the web/apache).

Other than that, if you can print to the error log from your code, can you confirm that the your code is correctly executed, without any exceptions, when you access the web page? (not when running from the shell).

yup i made sure the shebang was correct, as well as the permissions.my python was definitely running, but apache wasn't hooking fastcgi up to it correctly. (see my solution above). thanks
arshaw
A: 

In your webserver's log file, it should actually show what the output was that confused it. Are you sure you're looking in the error log as well as the access log?

Ken
it would normally just hang (no output). i then found i needed daemonize=False, and then things stopped hanging. But then it tried to start the fcgi process on port 4000, which apache didn't know about, so there would be no output. looks like the 'dynamic mode' solves my problems (see my solution). thanks
arshaw
+1  A: 

The Bluehost article has been the best resource, but I didn't carefully read the part about getting the latest patches (the beginning of step 3). At the time of the article, and even now with CherryPy version 3.1.2, you can't do 'dynamic mode' fcgi (when apache spawns the process). more here. Dynamic mode is basically essential if you are on a shared host.

I have checked out the trunk (3.2.0rc1), and after jumping through some hoops, got it to work. I followed step 5, method C in the bluehost article. Here was the stuff in the main of my cherryd.fcgi:

if __name__ == '__main__':
    cherrypy.config.update({
        'server.socket_port': None,
        'server.socket_host': None,
        'server.socket_file': None
    })
    start( daemonize=False, fastcgi=True, imports=["hello"])

Also, in cherrypy/process/servers.py, I had to change the following line:

# from this
# if not hasattr(socket.socket, 'fromfd'):

# to this
if not hasattr(socket, 'fromfd'):

So, it is possible to get it to work, but it feels kind of hacky. You should wait for the final release of version 3.2.0, or do what I did and check out Web.py. I was able to get it working with my shared host very easily (docs explain fastcgi/htaccess well).

arshaw