Premature end of script headers - Ruby on Rails with Phusion Passenger on Apache error
If you are experiencing the following problems:
- your application works great in development environment, but once you deploy it to production server using Phusion Passenger on Apache you get the "Error 500 - Internal server error" error
- and, when you check the Apache error log /var/log/apache2/error.log you find the actual error which says: "Premature end of script headers"
Maybe this short write up will clear up things and solve your problem too, because I found Google results a little bit misleading on this one.
"Premature end of script headers" error basically means that script stopped for whatever reason before it returned any output to the web server. And, the problem in my case was that Apache user did not have read/write access to the directory where Rails application is located. So, when it tries to write to the log file (for example: paperclip gem logs image resizes or restrul_authentication gem logs wrong logins), the application fails on those form submits.
All you have to do is change the privilegies of Apache user (www-data on Debian/Ubuntu) and Apache group (www-data) to the folder where rails app is located:
chown -R www-data:www-data /var/rails
Update: As Hongli Lai notes in the comments, this is actually the case when app/apache is run as root user, and user switching is turned on (the default).