January 20, 2007

Rails LightTPD and fastCGI Startup Error

After a fresh installation of the Ruby on Rails development environment on my Powerbook and a migration of my previous applications into that environment, I was unable to start any of them. There are two ways of starting up an RoR application. The first and simplest way is to use the WebRick HTTP server included in your Rails installation. The command for it is as follows:

  • Windows: ruby script/server webrick
  • Unix/Mac/Linux: script/server webrick
After running the command, you should be able to see your server starting and by directing your browser to [http://localhost:3000], access your application's main page.

The second way of starting up an RoR application is by not specifying the webrick argument on the command line. It would look like this:

  • Windows: ruby script/server
  • Unix/Mac/Linux: script/server
What happens in this case is that the script will lookup your LightTPD installation and try to startup the rails application using it. Most of the time, this would work right out of the box. Sometimes however, if you've installed fastCGI with LightTPD, you might run into the following error:


=> Booting lighttpd (use 'script/server webrick' to force WEBrick)
=> Rails application started on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server (see config/lighttpd.conf for options)
/Users/jeanpaul/webworkspace/ToDo/public/../config/boot.rb:18:in `require': No such file to load -- rubygems (LoadError)
from /Users/jeanpaul/webworkspace/ToDo/public/../config/boot.rb:18
from /Users/jeanpaul/webworkspace/ToDo/public/../config/environment.rb:11:in `require'
from /Users/jeanpaul/webworkspace/ToDo/public/../config/environment.rb:11
from /Users/jeanpaul/webworkspace/ToDo/public/dispatch.fcgi:21:in `require'
from /Users/jeanpaul/webworkspace/ToDo/public/dispatch.fcgi:21
2007-01-20 00:51:56: (mod_fastcgi.c.1048) the fastcgi-backend /Users/jeanpaul/webworkspace/ToDo/public/dispatch.fcgi failed to start:
2007-01-20 00:51:56: (mod_fastcgi.c.1052) child exited with status 1 /Users/jeanpaul/webworkspace/ToDo/public/dispatch.fcgi
2007-01-20 00:51:56: (mod_fastcgi.c.1055) if you try do run PHP as FastCGI backend make sure you use the FastCGI enabled version.
You can find out if it is the right one by executing 'php -v' and it should display '(cgi-fcgi)' in the output, NOT (cgi) NOR (cli)
For more information check http://www.lighttpd.net/documentation/fastcgi.html#preparing-php-as-a-fastcgi-program
2007-01-20 00:51:56: (mod_fastcgi.c.1060) If this is PHP on Gentoo add fastcgi to the USE flags
2007-01-20 00:51:56: (mod_fastcgi.c.1356) [ERROR]: spawning fcgi failed.
2007-01-20 00:51:56: (server.c.834) Configuration of plugins failed. Going down.



If you took a chance and directed your browser to [http://localhost:3000], you might see a [500 - Internal Server Error] being displayed.

What all these errors suggest is that your rubygems path is wrong or that you did not install rubygems at all. After spending quite sometime struggling with this, I found out that all that needed to be done was to change the path to ruby in your [dispatch.fcgi] file. In order to get that done, run the following command on your terminal / command prompt:
  • which ruby
In my case, the returned result was [/usr/local/bin/ruby]. It might be slightly different in yours but this does not really matter. Take note of the path to ruby as displayed on your terminal.

The next step is to open the [dispatch.fcgi] file and change the path to ruby at the beginning of the file to what had been displayed on the terminal. The [dispatch.fcgi] file usually sits in the [public] folder of your rails application. When you open the file, you should see something similar to the following line at the beginning of it:
  • #!/usr/bin/ruby
You will need to change that line from whatever was there to the following:
  • #!/your/path/to/ruby/as/known/by/your/computer
  • In my case, that would be #!/usr/local/bin/ruby
Save the file you just edited and restart your server. If you do not get any further warnings or errors displayed on your terminal after starting up the server, you can safely direct your browser to [http://localhost:3000] and you should be able to see your application running.

Voila! You just got Rails to run with LightTPD and mod_fcgi. ;-)

4 comments:

  1. Thanks for your post. I'm still getting this error, even after updating my config file's shebang line to my correct path.

    Here's the output I receive:

    tickle:~/Development/drewnoakes.com/trunk/rails/root Drew$ script/server
    => Booting lighttpd (use 'script/server webrick' to force WEBrick)
    => Rails application starting on http://0.0.0.0:3000
    => Call with -d to detach
    => Ctrl-C to shutdown server (see config/lighttpd.conf for options)
    2007-01-30 15:04:28: (mod_fastcgi.c.1022) execve failed for: /Users/Drew/Development/drewnoakes.com/trunk/rails/root/public/dispatch.fcgi Permission denied
    2007-01-30 15:04:28: (mod_fastcgi.c.1048) the fastcgi-backend /Users/Drew/Development/drewnoakes.com/trunk/rails/root/public/dispatch.fcgi failed to start:
    2007-01-30 15:04:28: (mod_fastcgi.c.1052) child exited with status 13 /Users/Drew/Development/drewnoakes.com/trunk/rails/root/public/dispatch.fcgi
    2007-01-30 15:04:28: (mod_fastcgi.c.1055) if you try do run PHP as FastCGI backend make sure you use the FastCGI enabled version.
    You can find out if it is the right one by executing 'php -v' and it should display '(cgi-fcgi)' in the output, NOT (cgi) NOR (cli)
    For more information check http://www.lighttpd.net/documentation/fastcgi.html#preparing-php-as-a-fastcgi-program
    2007-01-30 15:04:28: (mod_fastcgi.c.1060) If this is PHP on Gentoo add fastcgi to the USE flags
    2007-01-30 15:04:28: (mod_fastcgi.c.1356) [ERROR]: spawning fcgi failed.
    2007-01-30 15:04:28: (server.c.834) Configuration of plugins failed. Going down.
    Exiting
    Couldn't find any pid file in '/Users/Drew/Development/drewnoakes.com/trunk/rails/root/tmp/pids' matching 'dispatch.[0-9]*.pid'
    (also looked for processes matching "/Users/Drew/Development/drewnoakes.com/trunk/rails/root/public/dispatch.fcgi")

    Any thoughts appreciated!

    ReplyDelete
  2. Hi Drew,

    from what it seems, Lightpd cannot (or does not have) the permission to access your rails directory. So you might want to change the current permissions by `chmod -R 777 your_rails_directory` just to check and see what happens.

    If it does not work, this would require further investigation.

    Please let me know.

    Regards,

    Jean-Paul H.

    ReplyDelete
  3. Hello,
    rails (on lighttpd) worked fine untill i did an upgrad to 1.2.2. then i started to receive the following error
    $ script/server
    => Booting lighttpd (use 'script/server webrick' to force WEBrick)
    => Rails application starting on http://0.0.0.0:3000
    => Call with -d to detach
    => Ctrl-C to shutdown server (see config/lighttpd.conf for options)
    /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/routing.rb:954:in `initialize': wrong number of arguments (0 for 1) (ArgumentError)
    from /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/routing.rb:954:in `build'
    from /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/routing.rb:1172:in `add_route'
    from /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/routing.rb:986:in `connect'
    from /home/nira/archi_2/trunk/public/../config/../config/routes.rb:15
    from /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/routing.rb:1139:in `draw'
    from /home/nira/archi_2/trunk/public/../config/../config/routes.rb:1
    from /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:488:in `load'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:342:in `new_constants_in'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:488:in `load'
    from /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/routing.rb:1165:in `load_routes!'
    from /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/routing.rb:1157:in `reload'
    from /usr/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/initializer.rb:287:in `initialize_routing'
    from /usr/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/initializer.rb:111:in `process'
    from /usr/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/initializer.rb:43:in `run'
    from /home/nira/archi_2/trunk/public/../config/environment.rb:14
    from /home/nira/archi_2/trunk/public/dispatch.fcgi:21
    new rails application work fine but my old dont:(
    i`m trying to figure it out for hours!
    any suggestions?

    ReplyDelete
  4. Thanks so much for posting this. I was afraid I was going to be battling this all night.

    ReplyDelete