How do I setup Apache with FastCGI and Ruby?

Solution 1:

Well, in the meantime, I figured this one out. Since there was no concise writeup on this subject, here is the beginning of one. When I have all the bugs ironed out I'll submit this to some tutorial sites.

  1. Make sure that the base fastcgi apache module is installed. If you use ArchLinux as I do, mod_fcgid on the aur works. In other distributions, install either mod_fcgid or mod_fastcgi. (For those who wonder the difference, I don't think there is much, so I went for the one with more activity.)

  2. In your httpd.conf file, activate the module by adding a line to beginning of your LoadModule definitions. Not sure if it matters which distributions, but on Arch it was Loadmodule fcgid_module modules/mod_fcgid.so

  3. Add a new section to your httpd.conf. I put this just under the cgid_module section, but I'm pretty sure location would be arbitrary.

    <IfModule fcgid_module>
      AddHandler fcgid-script .fgci
    </IfModule>
    
  4. Now from my understanding, any file having the .fcgi extension in a directory marked ExecCGI will now be handled by the fastcgi module that was installed. I installed this package to give Ruby the fastcgi bindings, an inside an ExecCGI directory, I wrote this test script test.rb.fcgi

    #!/usr/bin/ruby -w
    require 'fcgi'
    
    count = 0
    FCGI.each_cgi do
      puts "Content-type: text/html\n\n"
      puts "Hello, Ruby! FastCGI(#{count})"
      count = count + 1
    end
    

Now when you go to that page in your browser, since count is outside the fastcgi loop, it should increment with each page view. If that is so, fcgi is setup correctly.

Solution 2:

You should look into using Passenger to handle your rails application (even if you are not using any rails features like ActiveRecord etc). There are many websites running on it (plus they have Rails Enterprise, less memory, better performance)