Linux – How to setup Apache with FastCGI and Ruby

apache-2.2fastcgilinuxruby

I've been reading alot of forum posts, tutorials, etc., about setting up apache under linux with fastcgi. I'm trying to run fcgi for some of my ruby development (not rails), but I figure it should apply to any language. Please note that I already have apache, fastcgi, ruby-fcgi, and the like already installed on my web box.

Conceptually, I'm having a hard time with it. Does apache spawn the specified fcgi processes as needed? Do I start them along side apache? Basically, I'm trying to get to a point where I am able to ask the proper questions, so apologies if the title is misleading.

Best Answer

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.

Related Topic