Got Ruby on Rails all set-up? Created your first app? Dead slow? Speed it all up with mod_fcgid on Ubuntu.
Now, just a big note here that you should use Webrick (the script/server that comes with the Rails install) to develop your RoR’s apps as this is meant for it. It may be slow, but that’s because when you make changes, it reloads the whole app into memory so that you can instantly see changes. With mod_fcgid, to make things faster, it caches stuff – and if you’re developing, that’s bad!
Get fcgi
sudo apt-get install libapache2-mod-fcgid
Enable fcgi
sudo a2enmod fcgid
Ruby fcgi bindings
sudo apt-get install libfcgi-ruby1.8
Modify .htaccess
Change:
AddHandler fastcgi-script .fcgi
To:
AddHandler fcgid-script .fcgi
If the above line isn’t in there, just add it to the top of the .htaccess file.
This line is telling Apache how to handle .fcgi files.
Also, be sure to change the re-write rule from:
RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
To:
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
And this line tells Apache which file to use as default when accessing the public directory of your Rails app – in this case, we want the fcgi dispatcher.
Configure fcgi
sudo cd /etc/apache2/mods-enabled
sudo nano fcgid.conf
You’ll want to include the following in the file:
<IfModule mod_fcgid.c>
AddHandler fcgid-script .fcgi
SocketPath /var/lib/apache2/fcgid/sock
DefaultInitEnv RAILS_ENV production
IdleTimeout 60
ProcessLifeTime 6000
MaxProcessCount 32
DefaultMaxClassProcessCount 2
IPCConnectTimeout 6
IPCCommTimeout 6
</IfModule>
For your set-up, you may need to adjust the values according to your set-up, but these work for me fine so far.
Restart apache2
Just so everything gets updated, restart Apache.
sudo /etc/init.d/apache2 restart
With any other applications you may add, all you’ll need to do is modify the .htaccess file.
Thanks to these sites: - http://claudio.cicali.org/article/74/how-to-install-ruby-on-rails-on-ubuntu-510 - http://fastcgi.coremail.cn/doc.htm - http://fo64.com/articles/2005/10/20/rails-on-breezy
Update
I’ve actually switched back to using standard FastCGI instead of fcgi as I seem to be having problems. I get a message when trying to access Typo saying
“Typo cannot be reached.”
I’m pretty sure this is something to do with the parameters set in fcgid.conf. If anyone knows a better set-up – please leave a comment.