# RubyonRails with Lighttpd and MySQL mini-HOWTO

## cchee

Background

In my attempt to setup RubyOnRails for software development, I search through the forum and can't find much information on setting up RubyOnRails with Lighttpd and MySQL on Gentoo specifically. It took me small amount of time to figure out how to get thing setup right but I believe share my experience with others helps to advance the adoption of gentoo distribution (I love this distribution!!) as well as hoping to help out newbie to save time. Your constructive criticism/suggestion/feedback are most welcome, especially in the light of network security related configuration.

Installation

Setting the appropriate USE flags

The USE flags I have in my /etc/portage/package.use are the following:

```

dev-db/mysql                       big-tables cluster utf8 perl readline ssl tcpd extraengine

dev-lang/ruby                      -doc socks5 threads cjk tcltk

dev-lang/swig                      -X -doc -guile java perl php python ruby tcltk

dev-ruby/actionmailer              -examples

dev-ruby/actionpack                -examples

dev-ruby/actionwebservice          -examples

dev-ruby/activerecord              -examples

dev-ruby/activesupport             -examples

dev-ruby/color-tools               -examples

dev-ruby/mysql-ruby                -examples

dev-ruby/pdf-writer                -examples

dev-ruby/rails                     fastcgi mysql -examples -postgres -sqlite

dev-ruby/rmagick                   -examples

dev-ruby/rubygems                  -examples

dev-ruby/transaction-simple        -examples

www-servers/lighttpd               php xattr ipv6 mysql php ssl

```

Some of these use flags may not apply to your situation, so just use the above as guideline.

MySQL, RubyOnRails, Lighttpd

To install MySQL, Rails and Lighttpd, it is relatively simple, just do:

```
emerge -DuvN rails pdf-writer mysql lighttpd
```

Portage will sort out most of the dependencies, if you want to make sure all your USE flags are exactly the way you want, you may need to iterate

```
 emerge -DuvN rails pdf-writer mysql lighttpd -pt
```

 couple times to sort out the USE flags before you actually do the install. Since my project needs some chart and graph, so after all this installation, I did some extra ruby modules via gem because they are not in portage.

```
gem install gruff
```

After all the install, I want to make sure I have all the updates, so I did

```
gem update
```

and my final gem list is the following:

```

*** LOCAL GEMS ***

actionmailer (1.1.4)

    Service layer for easy email delivery and testing.

actionpack (1.11.1)

    Web-flow and rendering framework putting the VC in MVC.

actionwebservice (0.9.4)

    Web service support for Action Pack.

activerecord (1.13.1)

    Implements the ActiveRecord pattern for ORM.

activesupport (1.2.4)

    Support and utility classes used by the Rails framework.

color-tools (1.3.0)

    color-tools provides colour space definition and manpiulation as

    well as commonly named RGB colours.

gruff (0.0.7)

    Beautiful graphs for one or multiple datasets.

pdf-writer (1.1.3)

    A pure Ruby PDF document creation library.

rails (0.14.4)

    Web-application framework with template engine, control-flow layer,

    and ORM.

rake (0.6.2)

    Ruby based make-like utility.

sources (0.0.1)

    This package provides download sources for remote gem installation

transaction-simple (1.3.0)

    Simple object transaction support for Ruby.

```

If you see multiple versions of certain gem, you can cleanup the older version by doing:

```
gem clean <gem name without the angle bracket>
```

For more gems, you can check out http://rubyforge.net/ for a list of ruby packages, and then try to emerge it, if it is not in portage, then try to install it via gem as:

```
gem install <package without angle bracket>
```

 to see if it install. If not, then you will have to install it via tarball.

Configuration

Assuming my rails environment are setup under /var/www/rails and I did my rails command to create the environment underneath this directory. All the configuration will based upon this assumption. If you have a different environment setup, you can improvise.  :Smile:  Since the configuration of MySQL is pretty standard, you can find help by searching thru this forum to setup the database for your RubyOnRails app, or follow the instruction already written by RubyOnRails team on the website. Same thing applies to your RubyOnRails apps. So this left with configuration for Lighttpd. lighttpd is relatively easy to configure, you can find the details on configuration parameters in http://www.lighttpd.net/documentation/configuration.html. I know, we are all lazy bum, so here is the sample configuration for you to based on to save you sometime.

```

server.modules              = (

                               "mod_redirect",

                                "mod_access",

                               "mod_fastcgi",

                               "mod_simple_vhost",

                                "mod_accesslog" )

server.document-root        = "/var/www/localhost/htdocs/"

server.errorlog             = "/var/log/lighttpd/error.log"

server.indexfiles           = ( "index.php", "index.html",

                                "index.htm", "default.htm" )

mimetype.assign             = (

  ".pdf"          =>      "application/pdf",

  ".sig"          =>      "application/pgp-signature",

  ".spl"          =>      "application/futuresplash",

  ".class"        =>      "application/octet-stream",

  ".ps"           =>      "application/postscript",

  ".torrent"      =>      "application/x-bittorrent",

  ".dvi"          =>      "application/x-dvi",

  ".gz"           =>      "application/x-gzip",

  ".pac"          =>      "application/x-ns-proxy-autoconfig",

  ".swf"          =>      "application/x-shockwave-flash",

  ".tar.gz"       =>      "application/x-tgz",

  ".tgz"          =>      "application/x-tgz",

  ".tar"          =>      "application/x-tar",

  ".zip"          =>      "application/zip",

  ".mp3"          =>      "audio/mpeg",

  ".m3u"          =>      "audio/x-mpegurl",

  ".wma"          =>      "audio/x-ms-wma",

  ".wax"          =>      "audio/x-ms-wax",

  ".ogg"          =>      "application/ogg",

  ".wav"          =>      "audio/x-wav",

  ".gif"          =>      "image/gif",

  ".jpg"          =>      "image/jpeg",

  ".jpeg"         =>      "image/jpeg",

  ".png"          =>      "image/png",

  ".xbm"          =>      "image/x-xbitmap",

  ".xpm"          =>      "image/x-xpixmap",

  ".xwd"          =>      "image/x-xwindowdump",

  ".css"          =>      "text/css",

  ".html"         =>      "text/html",

  ".htm"          =>      "text/html",

  ".js"           =>      "text/javascript",

  ".asc"          =>      "text/plain",

  ".c"            =>      "text/plain",

  ".cpp"          =>      "text/plain",

  ".log"          =>      "text/plain",

  ".conf"         =>      "text/plain",

  ".text"         =>      "text/plain",

  ".txt"          =>      "text/plain",

  ".dtd"          =>      "text/xml",

  ".xml"          =>      "text/xml",

  ".mpeg"         =>      "video/mpeg",

  ".mpg"          =>      "video/mpeg",

  ".mov"          =>      "video/quicktime",

  ".qt"           =>      "video/quicktime",

  ".avi"          =>      "video/x-msvideo",

  ".asf"          =>      "video/x-ms-asf",

  ".asx"          =>      "video/x-ms-asf",

  ".wmv"          =>      "video/x-ms-wmv",

  ".bz2"          =>      "application/x-bzip",

  ".tbz"          =>      "application/x-bzip-compressed-tar",

  ".tar.bz2"      =>      "application/x-bzip-compressed-tar"

 )

accesslog.filename          = "/var/log/lighttpd/access.log"

url.access-deny             = ( "~", ".inc" )

$HTTP["url"] =~ "\.pdf$" {

  server.range-requests = "disable"

}

server.pid-file            = "/var/run/lighttpd.pid"

simple-vhost.server-root   = "/var/www/rails/myapp/"

simple-vhost.default-host  = "127.0.0.1"

$SERVER["socket"] == "127.0.0.1:4000" {

  simple-vhost.document-root = "/var/www/rails/myapp/webapp1/public"

  server.document-root = "/var/www/rails/myapp/webapp1/public/"

  accesslog.filename = "/var/log/lighttpd/00rail_access_webapp1.log"

  errorlog.filename = "/var/log/lighttpd/00rail_error_webapp1.log"

  server.indexfiles = ( "dispatch.fcgi", "index.html" )

  server.error-handler-404 = "/dispatch.fcgi"

  server.error-handler-500 = "/dispatch.fcgi"

  ## rails stuff

  ### fastcgi module

  fastcgi.server = (

    ".fcgi" => (

      "127.0.0.1" => (

        "socket" => "/tmp/rail_webapp1.sck",

        "bin-path" => "/var/www/rails/myapp/webapp1/public/dispatch.fcgi",

        "min-procs" => 1,

        "max-procs" => 5

      )

    )

  )

}

$SERVER["socket"] == "127.0.0.1:3000" {

  simple-vhost.document-root = "/var/www/rails/myapp/webapp2/public"

  server.document-root = "/var/www/rails/myapp/webapp2/public/"

  accesslog.filename = "/var/log/lighttpd/00rail_access_webapp2.log"

  errorlog.filename = "/var/log/lighttpd/00rail_error_webapp2.log"

  server.indexfiles = ( "dispatch.fcgi", "index.html" )

  server.error-handler-404 = "/dispatch.fcgi"

  server.error-handler-500 = "/dispatch.fcgi"

  ## rails stuff

  ### fastcgi module

  fastcgi.server = (

    ".fcgi" => (

      "127.0.0.1" => (

        "socket" => "/tmp/rail_webapp2.sck",

        "bin-path" => "/var/www/rails/myapp/webapp2/public/dispatch.fcgi",

        "min-procs" => 1,

        "max-procs" => 5

      )

    )

  )

}

debug.log-request-header   = "enable"

debug.log-response-header  = "enable"

debug.log-request-handling = "enable"

debug.log-file-not-found   = "enable"

server.username            = "lighttpd"

server.groupname           = "lighttpd"

fastcgi.server             = ( ".php" =>

                               ( "localhost" =>

                                 (

                                   "host" => "127.0.0.1",

                                   "port" => 1026,

                                   "bin-path" => "/usr/bin/php-cgi"

                                 )

                               )

                            )

```

Couple things I want to point out, since I develop multiple software at a time, hence I want to have my lighttpd to do virtual hosting based upon different ports. That's why I am using $SERVER["socket"] == <ip:port> construct for conditional virtual hosting configuration. And I have two different rails apps setup (webapp1 and webapp2 in this case). This should give you some ideas on how to setup lighttpd.

Get to work

After you have all your configuration setup (i.e. MySQL configuration, database already setup, your rails app configuration [database.yml, environment.rb, etc...], and lighttpd configuration), it's time to try out the setup and see if everything works! So we will make sure all the needed server processes are started up if they are not already started.

```

/etc/init.d/mysql restart

/etc/init.d/lighttpd restart

```

Once they startup OK, then bring up your browser (assuming you are setting this environment on your workstation/desktop), goto http://127.0.0.1:3000/ and/or http://127.0.0.1:4000/ and you should be able to load your webapp1 and/or webapp2 in your browser. OK.. nobody is perfect, something went wrong, so we are into troubleshooting mode now.

Development Environment

For development environment, I use Eclipse 3.1.1 with Ruby Developer Tools. To install Eclipse 3.1.1, you will need to add 

```
dev-utils/eclipse-sdk ~x86
```

 into your /etc/portage/package.keywords, and then iterate 

```
emerge -DuvN eclipse -pt
```

 until you get all the USE flag setup for it and its dependencies, then you do the actual install by 

```
emerge -DuvN eclipse
```

 After eclipse is installed, I just follow the instruction in http://www.napcs.com/howto/railsonwindows.html to setup Eclipse with Ruby Developer Tools. I personally skipped the DBEdit part since I have PHPeclipse installed as well using instruction provided in http://www.plog4u.org/index.php/Using_PHPEclipse_:_Installation_:_Installing_PHPEclipse, and I just use the QuantumDB module for DB accessing.

Troubleshooting

Q: I am getting complete blank page when I load up the rail app. What is going on?

A: Relax, it's not end of the world. First place to check is your development.log under ${RAIL_APP}/log/ directory. Most like you will see there is a problem with database connectivity and/or table/database doesn't exist. Make sure you have those in place and try again.

Q: Your troubleshooting guide here suck!

A: Thanks, I know. One way to fix it is for you to let me know what question you have, and may be when I can find some free time, I will try to help you.  :Smile: Last edited by cchee on Sun Dec 18, 2005 2:27 am; edited 3 times in total

----------

## pjp

Moved from Portage & Programming

----------

## nife

Thanks I will try this later tonigt.

Looks like a really great how to guide.

Where can I get a description of all the possible gem's.

----------

## cchee

 *nife wrote:*   

> Thanks I will try this later tonigt.
> 
> Looks like a really great how to guide.
> 
> Where can I get a description of all the possible gem's.

 

You can check out http://rubyforge.net/

----------

## jrich013

I have used the original configuration, altered a bit for my own use. I had never seen the $SERVER["socket"] in a config before, and I really like the idea. Everything works great for me at my machine, however I can't access the services at sockets other than 80 from any other host. Any ideas on how to fix this? I know $HTTP["host"] is what is most commonly used, except I do not have a domain name.

----------

## johtib

I have some permission problems, after starting lighttpd with the init script I get this:

```
2006-04-06 09:59:30: (server.c.857) WARNING: unknown config-key: errorlog.filename (ignored)

Couldn't write to "/home/johtib/wwwroot/cookbook/public/../config/../log/fastcgi.crash.log": starting

  Errno::EACCES: Permission denied - /home/johtib/wwwroot/cookbook/public/../config/../log/fastcgi.crash.log

Couldn't write to "/home/johtib/wwwroot/cookbook/public/../config/../log/fastcgi.crash.log": starting

  Errno::EACCES: Permission denied - /home/johtib/wwwroot/cookbook/public/../config/../log/fastcgi.crash.log

Couldn't write to "/home/johtib/wwwroot/cookbook/public/../config/../log/fastcgi.crash.log": starting

  Errno::EACCES: Permission denied - /home/johtib/wwwroot/cookbook/public/../config/../log/fastcgi.crash.log

Couldn't write to "/home/johtib/wwwroot/cookbook/public/../config/../log/fastcgi.crash.log": starting

  Errno::EACCES: Permission denied - /home/johtib/wwwroot/cookbook/public/../config/../log/fastcgi.crash.log

Couldn't write to "/home/johtib/wwwroot/cookbook/public/../config/../log/fastcgi.crash.log": starting

  Errno::EACCES: Permission denied - /home/johtib/wwwroot/cookbook/public/../config/../log/fastcgi.crash.log
```

It worked fine before I added the rails vhost. Here's my lighttpd.conf:

```
var.basedir  = "/var/www/localhost"

var.logdir = "/var/log/lighttpd"

var.statedir = "/var/lib/lighttpd"

server.modules = (

    "mod_rewrite",

    "mod_redirect",

    "mod_access",

    "mod_simple_vhost",

    "mod_accesslog"

)

include "mime-types.conf"

include "mod_fastcgi.conf"

server.username = "lighttpd"

server.groupname = "lighttpd"

server.document-root = var.basedir + "/htdocs"

server.pid-file = "/var/run/lighttpd.pid"

server.errorlog = var.logdir  + "/error.log"

server.indexfiles = ("index.php", "index.html",

                                                "index.htm", "default.htm")

server.follow-symlink = "enable"

server.stat-cache-engine = "fam"

static-file.exclude-extensions = (".php", ".pl", ".cgi", ".fcgi")

accesslog.filename = var.logdir + "/access.log"

url.access-deny = ("~", ".inc")

$HTTP["host"] == "redwood" {

        simple-vhost.document-root = "/home/johtib/wwwroot/cookbook/public/"

        server.document-root = "/home/johtib/wwwroot/cookbook/public/"

        accesslog.filename = "/var/log/lighttpd/00rail_access_cookbook.log"

        errorlog.filename = "/var/log/lighttpd/00rail_error_cookbook.log"

        server.indexfiles = ( "dispatch.fcgi", "index.html" )

        server.error-handler-404 = "/dispatch.fcgi"

        fastcgi.server = (

                ".fcgi" => (

                        "127.0.0.1" => (

                                "socket" => "/tmp/rail_cookbook.sck",

                                "bin-path" => "/home/johtib/wwwroot/cookbook/public/dispatch.fcgi",

                                "min-procs" => 1,

                                "max-procs" => 5

                        )

                )

        )

}
```

Everything in the /home/johtib/wwwroot directory belongs to the user "johtib" and the group "users". From the config file above I figured that lighttpd must be running as lighttpd/lighttpd (this is the default) and thus can't access the files. However, running lighttpd as root sounds like a bad idea. Any suggestions? Here are the ebuild versions and use flags:

```

dev-ruby/rails-1.0.0  -examples +fastcgi +mysql -postgres -sqlite

www-servers/lighttpd-1.4.10-r1  +bzip2 -doc +fam +fastcgi -gdbm -ipv6 -ldap -lua -memcache -minimal +mysql +pcre -php -rrdtool +ssl -test -webdav -xattr

```

----------

## Fanatic

 *cchee wrote:*   

> 
> 
> Couple things I want to point out, since I develop multiple software at a time, hence I want to have my lighttpd to do virtual hosting based upon different ports. That's why I am using $SERVER["socket"] == <ip:port> construct for conditional virtual hosting configuration. And I have two different rails apps setup (webapp1 and webapp2 in this case). This should give you some ideas on how to setup lighttpd.
> 
> 

 

Isn't there any lighty equivalent to apache's aliases?

----------

## Fanatic

mod_alias exists for lighty aswell  :Very Happy: 

Here is a sample RoR config to set up an alias for your rails app (make sure to uncomment mod_alias from server.modules):

lighttpd.conf:

```

$HTTP["url"] =~ "^/yourrailsapp/" {

server.errorlog      = "/var/www/localhost/htdocs/rails/yourrailsapp/log/error.log"

server.document-root = "/var/www/localhost/htdocs/rails/yourrailsapp/public"

alias.url =("/yourrailsapp/" => "/var/www/localhost/htdocs/rails/yourrailsapp/public")

server.error-handler-404 = "dispatch.fcgi"

fastcgi.server =  (".fcgi" => (

    ( "min-procs"       => 1,

      "max-procs"       => 5,

      "idle-timeout"    => 120,

      "socket"          => "/tmp/yourrailsapp-railsapp.fastcgi",

      "bin-path"        => "/var/www/localhost/htdocs/rails/yourrailsapp/public/dispatch.fcgi",

      "bin-environment" => ( "RAILS_ENV" => "development" )

  )

))

}

```

yourrailsapp/config/environment.rb:

```

# Include your application configuration below

ActionController::AbstractRequest.relative_url_root = "/yourrailsapp"

```

Last edited by Fanatic on Thu Apr 20, 2006 6:15 pm; edited 1 time in total

----------

## Fanatic

 *johtib wrote:*   

> I have some permission problems, after starting lighttpd with the init script I get this:
> 
> ```
> 2006-04-06 09:59:30: (server.c.857) WARNING: unknown config-key: errorlog.filename (ignored)
> 
> ...

 

```

touch log/fastcgi.crash.log

chmod 666 log/fastcgi.crash.log

```

----------

## JustYourHosting

I just installed this on one of my servers. It is a great pice of software and would recomend it. Good jub on the guide!  :Smile: 

----------

## Doubleletter

[quote="Fanatic"] *johtib wrote:*   

> I have some permission problems, after starting lighttpd with the init script I get this:
> 
> ```
> 2006-04-06 09:59:30: (server.c.857) WARNING: unknown config-key: errorlog.filename (ignored)
> 
> ...

 

I faced the same problem. The solution to this is to the change the ownership of the 'log' directory in your rails directory:

```

 chown lighttpd:lighttpd -R log

```

----------

## unaos

tip: in one moment i didn't get that way of configuration and take a couple of hours googling to find out that there is no any need in one type multiple directives. all is handled by single specification, e.g:

alias.url ("alias0" => "dir_where",

"alias1" => "dir_where",

"alias2" => "dir_where",

)

----------

