FCGI for Python on HostGator (spoiler alert: it’s a bit annoying, but at least it’s there!)

So, I’m tasked with rewriting a website, and I made some good progress doing it in Slim. At the end of the day, though, nothing’s ideal unless it’s in Python, and it looks like they’re going to keep cloning this site, and I’ll have to keep maintaining all the various iterations. So, I really needed to find a way to make a sane Python app on HostGator.

By the way, HostGator, as a hosting provider is GREAT for beginners. They really do have world-class customer service, so you’ll never be in the lurch. But I can’t use them for production sites because of the CRAP-ASS Python support. Honestly, guys, would it kill you to give us a modern Python? But I digress…

HostGator gives you Python 2.6.6, which is just barely enough to run something sensible. For any clones of this project that get made, I’ll insist on using another hosting provider, but I figure if I can get something running in Python 2.6.6 on the main site, we’re good to go for the clones. As it turns out, HostGator also gives you fcgi and flup, which is enough to run a Python cgi script without the performance penalty. Great!

To get an fcgi script running, you just do this in your .htaccess:

<IfModule mod_fcgid.c>
   AddHandler fcgid-script .fcgi
   <Files ~ (\.fcgi)>
       SetHandler fcgid-script
       Options +FollowSymLinks +ExecCGI
   <Files>
</IfModule>

<IfModule mod_rewrite.c>
   Options +FollowSymlinks
   RewriteEngine On
   RewriteBase /
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^(.*)$ {scriptname}.fcgi/$1 [QSA,L]
</IfModule>

And of course replace {scriptname} with the name of your fcgi middleware script.

To get Flask to work with fcgi, you can just more or less copy the example script given on the Flask man page.

One problem: apparently HostGator hasn’t configured apache to restart apps when files change, so once your app is running it’s just kind of … running. It seems that every so often Apache restarts and notices the changes, but there is going to be some downtime, so be sure to develop entirely locally and get everything exactly right before pushing it to the server.

Grrr…HostGator! See this StackOverflow post to reassure yourself that it is, in fact, HostGator that’s being ridiculous. (Read the comments – I’m getting the same behavior. I can restart my app once, and after that fcgi just always uses the old version, even if I temporarily rename the fcgi file. VERY FRUSTRATING HOSTGATOR!!!)

Here’s what works: rename your fcgi middleware file, and also rename it in the .htaccess file. Then name it back. Again, in both places.

In conclusion, HostGator is staffed by frustrating people who are really good at customer service.

2 thoughts on “FCGI for Python on HostGator (spoiler alert: it’s a bit annoying, but at least it’s there!)

  1. Hey Joshua,

    Thanks for this post man. I’m also creating a flask app on hostgator. I would appreciate your input on the following issue I’m having. Even though my application is working, I’m getting these errors when I run python MYAPP.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!

    Did you experience this, and is this something to worry about?

    Thank you so much.

    • Yes, those are (more or less) required. Unfortunately it’s been some years since I’ve tried anything on HostGator, so I’m no longer sure how they send CGI variables along. It looks like maybe they don’t anymore. Which sort of kills the enterprise, unfortunately. Or, erm, well, depends on what you’re trying to do. If you’re trying to do any kind of REST operation at all, you’ll need REQUEST_METHOD, unfortunately.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>