How to fix FastCGI timeouts for long running scripts in Apache

I have been dealing with long running tasks timing out for quite a while now and I wanted to share the solution I finally tracked down that worked for me.  AppRabbit runs on Ubuntu Linux, with Nginx handling static content and acting as a reverse proxy and Apache as the app server, using mod_fcgi to run the actually Python/Django code.  The problem first arose during data imports, because they can sometimes take a while to run if users are importing a lot of data, but I resolved that issue by moving the processing to a back end process using RabbitMQ and Django-Celery.  But alas the timeout issue reared its ugly head again, this time when exporting large amounts of data, because it just took a while to prepare the data for exporting.  

The error messages that showed up in the logs were "mod_fcgid: read data timeout in 40 seconds" and "Premature end of script headers", neither of which are very helpful, but at least we know we're dealing with an issue of mod_fcgi timing out because the script is taking too long.  The solution is to edit your fcgid.conf file (which for me is located at /etc/apache2/mods-enabled/fcgid.conf) and add this line: 

FcgidIOTimeout 300

Once you've finished editing fcgid.conf and restart apache, this will allow your scripts to run for up to 300 seconds (5 minutes), and lets face it, at that point maybe it's time to refactor something.