# FastCGI files just appearing as text

## Turnip

Hi,

I'm trying to use Ruby on Rails with FastCGI (to make it errr... faster). I have installed mod_fastcgi, apache, rails, and everything else I "should" need. I am using "AddHandler fastcgi-script .fcgi" and "Options +FollowSymLinks +ExecCGI". Yet still when I go to the site (it is a local VHost), the file just appears as plain text. I'm no Apache whiz, so I'm guessing I've left out some directive somewhere.

Any help would be appreciated.

Thanks,

Turnip

----------

## Pythonhead

My modified /etc/apache2/conf/commonapache2.conf

```

<IfModule mod_fastcgi.c>

    AddHandler fastcgi-script .fcgi .fpl

    FastCgiIpcDir /tmp/fcgi_ipc/

</IfModule>

```

And in /etc/apache2/conf/apache2.conf

```
LoadModule fastcgi_module                /usr/lib/apache2-extramodules/mod_fastcgi.so
```

[edit] Removed FastCgiServer directive.Last edited by Pythonhead on Thu Mar 24, 2005 6:49 pm; edited 1 time in total

----------

## Turnip

Aha, thanks a lot! I realised along the way that I didn't have "-D FASTCGI" in the apache config. It's now _working_, but not properly. The page takes ages to load, when it does it's incomplete, I have lots of stuff like this in the log file:

[Sat Mar 12 20:34:03 2005] [error] [client 127.0.0.1] FastCGI: comm with (dynamic) server "/home/turnip/test/public/stylesheets/more.css" aborted: (first read) idle timeout (30 sec), referer: http://test/

[Sat Mar 12 20:34:03 2005] [error] [client 127.0.0.1] FastCGI: incomplete headers (0 bytes) received from server "/home/turnip/test/public/stylesheets/more.css", referer: http://test/

Any ideas?

Thanks a lot,

Turnip

----------

## Pythonhead

How are you linking to the stylesheet in your .rhtml?

Are you using something like:

```
<%= stylesheet_link_tag('mystyles') %>
```

I noticed fastcgi is much pickier about accessing anything in /public

----------

## Turnip

No, the stylesheet is called using a plain old <link> element with an absolute path.

However, I'm testing this with Hieraki (http://dev.hieraki.org/), which defaults to FCGI anyway, so it's unlikely to be a problem with the code. I tried doing it with that tag anyway, but it made no difference.

FWIW, I'm also getting the same sort of error on public/500.html

----------

## Pythonhead

Does this directory exist and is it owned by apache? 

 /tmp/fcgi_ipc/

I have had problems when switching between dispatchers (mod_ruby->fcgi) when there were extra /tmp/ruby_sess* files laying around. Try deleting those after shutting down apache.

Also, even if you shut down apache you may still have fcgi processes running you might want to kill before restarting apache:

```
ps aux|grep dispatch
```

Yesterday I had a similar problem to yours. fastcgi was trying to execute any file in /public (.css, images etc).

I don't know why its working today, but it may be because I removed the fcgi startup line from above:

```
FastCgiServer /home/rob/ScriptTest/public/dispatch.fcgi 
```

----------

## Turnip

I've done all that, but it still doesn't work  :Sad: 

----------

## Pythonhead

Just for kicks how about uping the timeout to see if it finally succeeds:

Add this:

```
FastCgiConfig -idle-timeout 120
```

To this section:

```
<IfModule mod_fastcgi.c>

...
```

----------

## Turnip

Nope, it only appears after the timeout -- broken.

----------

## Micand

I'm experiencing the same issue. Please be gentle, for I am a Rails noobie working his way through Four Days on Rails. The basic ToDo application listed in "Day 1" worked fine when running as a CGI. After switching to FastCGI by changing "RewriteRule ^(.*)$ dispatch.cgi [QSA,L]" in public/.htaccess to "RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]", the app would error out in the browser with "Application error: Rails application failed to start properly". Apache's error log contained the same error message that Turnip received:

```
[Thu Mar 24 05:17:19 2005] [error] [client 127.0.0.1] FastCGI: comm with (dynamic) server "/var/www/todo/public/dispatch.fcgi" aborted: (first read) idle timeout (30 sec)

[Thu Mar 24 05:17:19 2005] [error] [client 127.0.0.1] FastCGI: incomplete headers (0 bytes) received from server "/var/www/todo/public/dispatch.fcgi"
```

I'm running Apache 2.0.53, mod_fastcgi-2.4.2-r1, fcgi-2.4.0, and ruby-fcgi-0.8.5-r1. Any assistance will be much appreciated.

Thanks,

Jeff

----------

## Pythonhead

Did you also try upping the timeout value? Some people suggest 240 because of slow startup times.

Do you get any error message when you run this as apache?

```
sudo -u apache /var/www/todo/public/dispatch.fcgi
```

You have this in your .htaccess?

```
AddHandler fastcgi-script .fcgi
```

EDIT What version of rails are you using?

I found a bug in dispatch.fcgi 0.11.0 that could be your problem. If you try running dispatch.fcgi from the shell and it complains about  RAILS_ROOT, you can try this patch (I'll add it to portage a.s.a.p):

```
--- public/dispatch.fcgi

+++ public/dispatch.fcgi

@@ -1,6 +1,5 @@

 #!/usr/bin/ruby18

 

-FASTCGI_CRASH_LOG_PATH = "#{RAILS_ROOT}/log/fastcgi.crash.log"

 

 def dispatcher_error(e, msg = "")

   error_message = "[#{Time.now}] Dispatcher failed to catch: #{e} (#{e.class})\n  #{e.backtrace.join("\n  ")}\n#{msg}"

@@ -14,6 +13,8 @@

   require 'dispatcher'

   require 'fcgi'

 

+  FASTCGI_CRASH_LOG_PATH = "#{RAILS_ROOT}/log/fastcgi.crash.log"

+

   FCGI.each_cgi do |cgi| 

     begin

       Dispatcher.dispatch(cgi)
```

Last edited by Pythonhead on Thu Mar 24, 2005 11:28 pm; edited 1 time in total

----------

## Pythonhead

Turnip, looking again at your message, its making me think fcgi is trying to execute every file type in /public.

Heres my .htaccess, using non-vhost in case some of the ReWrite{rules,cond}'s help:

```

RewriteBase /script/

AddHandler fastcgi-script .fcgi

Options +FollowSymLinks +ExecCGI

RewriteEngine On

RewriteRule ^$ index.html [QSA]

RewriteRule ^([^.]+)$ $1.html [QSA]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ dispatch.fcgi?$1 [QSA,L]

ErrorDocument 500 /500.html

```

----------

## Turnip

This is the .htaccess I'm using, which is th same:

```
# General Apache options

AddHandler fastcgi-script .fcgi

AddHandler cgi-script .cgi

Options +FollowSymLinks +ExecCGI

# Redirect all requests not available on the filesystem to Rails

RewriteEngine On

RewriteRule ^$ index.html [QSA]

RewriteRule ^([^.]+)$ $1.html [QSA]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ dispatch.cgi?$1 [QSA,L]

# In case Rails experiences terminal errors

ErrorDocument 500 /500.html
```

I don't think it's a problem with Apache -- I made a test app (instead of using Hieraki, which is what I'm having the problem with), and FastCGI worked fine. I'm not working with Hieraki at the moment, but if I find a solution sometime then I'll post back here.

----------

## Micand

 *Pythonhead wrote:*   

> 
> 
> EDIT What version of rails are you using?
> 
> I found a bug in dispatch.fcgi 0.11.0 that could be your problem. If you try running dispatch.fcgi from the shell and it complains about  RAILS_ROOT, you can try this patch (I'll add it to portage a.s.a.p):
> ...

 

Aha, that fixed it. Thanks for the prompt reply. I am indeed running Rails 0.11.0, and I had the proper AddHandler line in my .htaccess. Upping the timeout value to 240 did nothing except lengthen the time it took for the script to error out to 240 seconds. Running "sudo -u apache /var/www/todo/public/dispatch.fcgi" produced the error message relating to RAILS_ROOT that you mentioned. However, after applying your patch and restarting Apache, everything began working. Thanks again!

----------

## Pythonhead

Ok, great. I added the patch to 0.11.0-r1, thanks.

----------

## Turnip

I'm pretty sure my problem is not with Ruby, FastCGI, Apache or Rails, but with Hieraki. I created a test rails app just to see if it would work, and that runs fine under FastCGI, so I think the culprit is Hieraki. Incidentally, I don't think Hieraki has been updated for Rails 0.11.0 yet, but I'll keep that in mind for when it gets updated.

BTW, is it better to be taking Ruby stuff like Rails and friends from RubyGems or from Portage? Currently I'm using RubyGems because that's what it said to do on the Rails website.

----------

## Pythonhead

 *Turnip wrote:*   

> 
> 
> BTW, is it better to be taking Ruby stuff like Rails and friends from RubyGems or from Portage? Currently I'm using RubyGems because that's what it said to do on the Rails website.

 

Using the ebuilds actually installs a gem so theres no difference, except you get patches like the one above for fastcgi ahead of everyone else, which upstream says will go in 0.11.1.

Also I added a 'fastcgi' USE flag that pulls in a patch for dev-ruby/ruby-fastcgi that patches a nasty memory leak. So, I think Gentoo is now ahead of everyone using plain gems, as of 0.11.0 anyways.

I noticed a new Gentoo page on their wiki yesterday, which I edited at the bottom with this info:

http://wiki.rubyonrails.com/rails/show/HowtoInstallOnGentooWithApache

----------

## Turnip

Ok. I might try starting over with my installations and use Portage, then see if that makes a difference.

----------

## destiney

I followed this HowTo:

http://wiki.rubyonrails.com/rails/show/HowtoInstallOnGentooWithApache

Everything installed, but now when I go to my rails app, the dispatch.fcgi appears as text.

My vhost looks like this:

Listen 10.0.0.2:8000

<VirtualHost 10.0.0.2:8000>

    ServerName rails

    DocumentRoot /var/www/localhost/htdocs/ruby/public

    ErrorLog /var/www/localhost/htdocs/ruby/ruby.log

    <Directory /var/www/localhost/htdocs/ruby/public>

        Options ExecCGI FollowSymLinks

        AddHandler cgi-script .cgi 

        AllowOverride all

        Order allow,deny

        Allow from all

    </Directory>

</VirtualHost>

I have this in /etc/conf.d/apache2 :

APACHE2_OPTS="-D FASTCGI -D RUBY -D PHP5 -D PYTHON -D PERL"

Apache isn't producing any errors in the logs, and I've stopped it and restarted it numerous times.

Tried changing the line above to AddHandler cgi-script .cgi .fcgi, but that didn't help.

Any ideas?

----------

## Turnip

You need AddHandler fcgi-script .fcgi I think (and it always helps to say what the problem is).

I was actually fiddling with Rails last night too... I uninstalled all my gems, and then installed the packages from portage (which in turn re-installed some gems). I can use FastCGI fine, but it doesn't work with Hieraki, so I guess that must be the fault of the application.

----------

## destiney

 *Turnip wrote:*   

> and it always helps to say what the problem is

 

I said "when I go to my rails app, the dispatch.fcgi appears as text".  Helps if you read the post you're about to reply to.

AddHandler fastcgi-script .fcgi already appears in the /var/www/localhost/htdocs/ruby/public/.htaccess.

----------

## Turnip

I'm sorry, I don't know how I missed that.

With regard to the problem, I can only show you my /etc/apache2/conf/modules.d/20_mod_fastcgi.conf in the hope it might be useful...

```
<IfDefine FASTCGI>

        <IfModule !mod_fastcgi.c>

                LoadModule fastcgi_module     /usr/lib/apache2-extramodules/mod_fastcgi.so

        </IfModule>

        <IfModule mod_fastcgi.c>

                AddHandler fastcgi-script .fcg .fcgi. fpl

                FastCgiIpcDir /tmp/fcgi_ipc/

        </IfModule>

</IfDefine>
```

Other than that I don't know. It might be that the .htaccess file isn't allowed to do AddHandler -- there's no harm trying it out in one of the server configs.

----------

## destiney

After looking at httpd.conf I figured the problem out.  The Include path for the modules.d directory was wrong.  So I copied 20_mod_fastcgi.conf from /etc/apache2/conf/modules.d to /etc/apache2/modules.d.

Then I edited it to fix the broken module path:

#LoadModule fastcgi_module    extramodules/mod_fastcgi.so

LoadModule fastcgi_module    /usr/lib/apache2-extramodules/mod_fastcgi.so

I dunno why my apache2 install has two module.d directories.  But it's all working now.

----------

## navjeet

You still have dispatch.cgi rather than dispatch.fcgi in

RewriteRule ^(.*)$ dispatch.cgi?$1 [QSA,L] 

--Navjeet

 *Turnip wrote:*   

> This is the .htaccess I'm using, which is th same:
> 
> ```
> # General Apache options
> 
> ...

 

----------

