CherryPy (or other Python framework) with FastCGI on shared host

2.2k views Asked by At

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.

4

There are 4 answers

0
arshaw On BEST ANSWER

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).

1
Nuno Maltez On

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).

1
fumanchu On

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.

1
Ken Kinder On

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?