How to Setup Apache With FastCGI And Ruby Bindings

转载 2007年09月24日 21:54:00

How to install Apache, Ruby, RubyGems, Rails, and FastCGI? under Linux

  1. Become root and go to a directory to work in
    su root
    cd /usr/local/src
  2. Download all of the needed files. These versions may be out of date, so you might want to go find the latest.
  3. Install Ruby
    tar -zxvf ruby-1.8.5.tar.gz
    cd ruby-1.8.5
    make test
    make install
    cd ..
  4. Install Ruby Gems
    tar -zxvf rubygems-0.9.0.tgz
    cd rubygems-0.9.0
    ruby setup.rb
    cd ..
  5. Install Apache (if needed—if you have an existing Apache installation & know what you are doing this step can be skipped)
    tar -zxvf httpd-2.2.3.tar.gz
    cd httpd-2.2.3
    ./configure --enable-rewrite --enable-cgi --enable-so
    make install
    cd ..
  6. Install FastCGI?
    tar -zxvf fcgi-2.4.0.tar.gz 
    cd fcgi-2.4.0
    make install
  7. Install mod_fcgi
    mod_fastcgi is heavily outdated and very hard to install it is better to use mod_fcgid
    tar -zxvf mod_fcgid.1.10.tar.gz cd mod_fcgid.1.10 # in Makefile set top_dir to the top path of you apache i.e. /usr/local/apache2 make make install
  1. If fcgid is not working for you try installing mod_fastcgi for Apache (note that this assumes you are using Apache-2.x, etc.; if you are using Apache 1.3, DSOs, etc. read the INSTALL file.
  2. Install Rails
    gem install rails
    Install required dependency rake? [Yn]  y
    Install required dependency activesupport? [Yn]  y
    Install required dependency activerecord? [Yn]  y
    Install required dependency actionpack? [Yn]  y
    Install required dependency actionmailer? [Yn]  y
    Install required dependency actionwebservice? [Yn]  y
  3. Install the fcgi gem
    gem install fcgi
    On FreeBSD, If you receive “ERROR: While executing gem” stating “checking for fcgiapp.h… no” then you will have to run the gem install with alternate syntax:
    gem install fcgi -- --with-fcgi-include=/usr/local/include --with-fcgi-lib=/usr/local/lib

    If you receive an error which contains “can’t find header files for ruby.”, then please install the ruby-devel package.
  4. Edit your Apache configuration (typically found somewhere like /usr/local/apache2/conf/httpd.conf or /etc/httpd/conf/httpd.conf) and add these lines:
    <Directory /var/www/>
        AllowOverride all
    LoadModule fastcgi_module modules/
    AddHandler fastcgi-script .fcgi
    <VirtualHost *:80>
        DocumentRoot /var/www/rails/testapp/public
        ErrorLog /var/log/httpd/testapp-error_log
        CustomLog /var/log/httpd/testapp-access_log common
        Options Indexes ExecCGI FollowSymLinks
        RewriteEngine On
  5. Edit your Apache configuration (with fcgid instead of fastcgi)
    FastCGI is severly outdated, so it is recommended that you use FCGID instead. If you use FCGID two lines in the configuration file will be different:
    LoadModule fcgid_module modules/
    # in case of mod_fcgid you may want to add 
    <IfModule mod_fcgid.c>
        AddHandler fcgid-script .fcgi
        IPCCommTimeout 40
        IPCConnectTimeout 10
        DefaultInitEnv RAILS_ENV production
        SocketPath /tmp/fcgidsock
  6. Start Apache
    /usr/local/apache2/bin/apachectl start
  7. Go to and see the Rails welcome page.
  8. Set up a database for Rails in database.yml (or see HowToUseRailsWithoutADatabase).
  9. Create a simple controller for testing Rails
    cd /var/www/rails/testapp/public
    rm index.html
    cd ..
    script/generate controller home index
  10. Go to and see a default view, running with normal cgi.
  11. Enable FastCGI?
    1. Edit /var/www/rails/testapp/public/.htaccess and change dispatch.*cgi* to dispatch.*fcgi*
    2. Edit /var/www/rails/testapp/public/dispatch.fcgi (in rails 0.13.1 this is within the rails installation: lib/fcgi_handler.rb) and change
      require 'fcgi'

      require 'rubygems'
      require_gem 'fcgi'
  12. Go to and see a default view again, but with fcgi

Troubleshooting Suggestions
Make sure that you delete any ruby session files in your /tmp directory before switching to dispatch.fcgi. If you tested with cgi, there might be some with different permission that what apache (fastcgi) can read and will cause issue.

If you want to see if fastcgi is working with ruby, try pasting the following search into test.fcgi (in your rails/public dir). You will need to make sure the file has 755 permissions (chmod 755 test.fcgi).

require 'cgi'
require 'rubygems'
require_gem 'fcgi'

 FCGI.each_cgi do |cgi|
    content = ''
    env = []
    cgi.env_table.each do |k,v|
      env << [k,v]
    env.each do |k,v|
      content << %Q(#{k} => #{v}<br>/n)

Make sure that (when using Apache) your www or apache user has write access to your rails/tmp and rails/log directory.

If not you will get many 500 errors!

The above test was working but I didn´t get the dispatcher running under apache2 (self built) until i changed the dependencies of rails-0.14.1/lib/fcgi_handler.rb to:

require 'cgi'
require 'rubygems'
require_gem 'fcgi'
require 'logger'
require 'dispatcher'
require 'rbconfig'

class RailsFCGIHandler


I don´t know why this is like this (why the dependencies in the dispatcher should be wrong) but it helped.

I’ve been bitten by two things with the Apache configuration: I had MultiViews on, and I was redirecting /some/url.html to /some/url because I don’t want file extensions in my URLs. The second rule causes endless redirect looping with standard Rails’ .htaccess, and MultiViews interferes badly with Rails’ caching. So, keep MultiViews off, and be careful with those redirects. —Matijs van Zuijlen

I have yet to get Apache fcgi working Windows XP SP2. I’ve got Apache working with Rails but it’s slow as mud. I’ve downloaded all the software that I can find and put in the http.conf file the recommended Apache configuration but can’t tell if fcgi is working or not.

Does anyone have a simple step by step recipe to get Apache (v2) on Windows XP Prof running fcgi? I found a test page for Rails but it doesn’t render the link

    <title>Ajax Demo</title>
    <%= javascript_include_tag "prototype" %>
    <h1>What time is it?</h1>
    <div id="time_div">
      I don't have the time, but
      <%= link_to_remote( "click here",

                         :update => "time_div",
                         :url =>{ :action => :say_when }) %>
      and I will look it up.


I get the page minus the “click here” link????

For anyone trying to get fastcgi working after installing the gem version of the ruby fastcgi lib, I found uninstalling it, then loading the library version and building it, fixed all problems. Download from here read the README and build and install, and fastcgi starts working.

Jewelry Blog

I’ve got lots of help by reading dev411
article on Installing Typo: MySQL, Apache, lighttpd and FastCGI

Don’t foreget to comment out

AddHandler fast_cgi-script .fcgi

and add

AddHandler fcgid-script .fcgi

to your .htaccess or it wont work.

If you are running on freebsd/mysql and your database.yml is fine but you hit.

Mysql::Error: Lost connection to MySQL server during query: SELECT version FROM schema_info

update your ports and install the ruby-mysql port.

no need for mysql gem and all the grief that causes :)

If you get an error installing ruby at make install that looks like

fileutils.rb:1239:in `copy’: unknown file type: ./.ext/. (RuntimeError)

try running make first (so ./configure, then make, then make test, make install)

If you get this error on ruby setup.rb when installing gems

/usr/local/lib/ruby/site_ruby/1.8/rubygems/remote_fetcher.rb:4:in `require’: no such file to load — zlib (LoadError)

make sure zlib and zlib-devel are installed, then follow these instructions:



在cloudzm的blog上看到这篇文章,照着做了一遍,发现有一两处遗漏,写出来以免自己忘记:我用的是windows2003, server 用的是wamp server(apache 2.0 , p...
  • joliny
  • joliny
  • 2007年09月24日 21:55
  • 3371

Apache下 FastCGI 配置小结 一 .下载   FastCGI模块   http://www.fastcgi...
  • bytxl
  • bytxl
  • 2012年11月07日 15:06
  • 1307


1. 克服 CGI 的瓶颈 1.1 令人头痛的效率问题 1.2 一些解决之道 1.3 更好的方法 - FastCGI 2. 安装 FastCGI 2.1 在apache服务器上安装 Fast...
  • wangyong0921
  • wangyong0921
  • 2013年07月22日 14:27
  • 1003


geos库交叉编译生成ARM平台库 在解压产生的geos-3.4.2/目录下,修改configure文件693行,配置成成交叉编译模式...
  • u013686019
  • u013686019
  • 2015年07月01日 16:05
  • 1369

Ubuntu下的apache2 + fastcgi配置

  • lesky
  • lesky
  • 2007年01月30日 22:52
  • 4812


一、版本简介    其实网上有很多关于这方面的配置,但是我根据一部分的配置不能完成测试(可能因为配置有误),但是还要注意的就是版本问题,先说说几个软件的版本;    1、Apache   ver 2....
  • networkwx
  • networkwx
  • 2010年10月28日 00:17
  • 6930

windows 下apache 以fastcgi形式运行php

以xampp为例讲解,步骤如下: 1:下载对应的mod_fastcgi ,下载参考网站: 2:拷贝mod_fas...
  • ikscher
  • ikscher
  • 2015年06月30日 14:54
  • 2640


在一台服务器上部署应用,发现已有的php版本较老,直接升级又怕影响现有应用,于是决定两个版本并存,并通过fastcgi方式调用php。安装和设置过程简要如下:# 安装fastcgi模块 yum ins...
  • skyshore
  • skyshore
  • 2016年04月20日 19:41
  • 704


环  境:Linux RedHat Enterprise Linux Server release 5源码包:httpd-2.2.15.tar.gz  php-5.2.13.tar.gz mod_fa...
  • sflsgfs
  • sflsgfs
  • 2011年04月14日 10:37
  • 3991


centos安装php: yum install php php-devel centos自带apache2,查看apache版本rpm -qa | grep httpd, 安装apache工具(a...
  • u012374229
  • u012374229
  • 2016年01月01日 20:39
  • 698
您举报文章:How to Setup Apache With FastCGI And Ruby Bindings