Ruby on Rails Installation How To
Ruby on Rails and Lighttpd(SSL) Installation
The goal of this document is to show you how to get Ruby on Rails up and running on Lighttpd(SSL enabled) with MySQL running on the back. We will go through proper configuration of your database. We will also cover how to configure Rails for sending e-mail. And finally, we will walk through a sample Rails application that uses partials for rendering a basic website template where the header, content, and footer are all separate thereby adhering to the DRY (Don't Repeat Yourself) principle.
I found it difficult to find all of this information in one single location on the internet. Thus, I hope that this is helpful for anyone who is configuring a Rails development environment and/or their first Rails application in a Linux environment.
First and foremost, Ruby is an interpreted, dynamically typed, object-oriented programming language. Rails is a web development framework that utilizes the Model View Controller software design idealogy for web application design. Rails applications are written in Ruby. Lighttpd is a fast and light web server. Ruby on Rails provides a very nice abstraction of the back-end database. In our case, we will be using MySQL for the back end database.
If you have any questions or comments, please send me an e-mail by filling out the contact form on this Web site.
I. Check your system
Your system should look relatively similar to my system. Prior to installation, Let's check our system to make sure that we have the necessary tools.
$ echo $SHELL /bin/bash $ uname -a Linux dash.bryanhinton.com 2.6.18-8.1.10.el5 #1 SMP Thu Aug 30 20:43:15 EDT 2007 \ i686 i686 i386 GNU/Linux $ echo $PATH /usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/bin:/bin: \ /usr/bin:/home/bryan/bin:/usr/local/bin:/usr/sbin $ echo $LD_LIBRARY_PATH /usr/local/lib:/usr/lib:/lib $ make -v GNU Make 3.81 $ autoconf -V autoconf (GNU Autoconf) 2.59 $ gcc -v Using built-in specs. Target: i386-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info \ --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib \ --enable-__cxa_atexit --disable-libunwind-exceptions \ --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada \ --enable-java-awt=gtk --disable-dssi --enable-plugin \ --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre \ --with-cpu=generic --host=i386-redhat-linux Thread model: posix gcc version 4.1.1 20070105 (Red Hat 4.1.1-52)
II. Check your host configuration on the network
If you have not already done so, you can go ahead and change the hostname on your computer. You do not need to do this if your host name already has an acceptable value. If you have a public IP address directly bound to your nic card, then you will need to properly setup an A record in your domain's zone file. I use TinyDNS and DNSCache for my nameserving needs and I highly recommend them.
$ su ******* # hostname WCLI404892341 # hostname dash.bryanhinton.com # hostname dash.bryanhinton.com
Now Open /etc/sysconfig/network and /etc/hosts and make sure that they look like the following:
# cat /etc/sysconfig/network NETWORKING=yes NETWORKING_IPV6=yes HOSTNAME=dash.bryanhinton.com # cat /etc/hosts 127.0.0.1 localhost.localdomain localhost 74.53.242.146 dash.bryanhinton.com dash ::1 localhost6.localdomain6 localhost6
III. Install Ruby 1.8.6 patchlevel 110, RubyGems 0.9.5, and Ruby FastCGI Bindings
As of November 24, 2007, 1.8.6-p110 is the recommended, stable version and 0.9.5 is the recommended, stable version of RubyGems. RubyGems is the Ruby package management system and a gem is a packaged Ruby application or library. We will be using it later to install Rails libraries. Feel free to install Ruby via your package managment system. However, I prefer to remove pre-installed Ruby and then build my own. Typically, you will encounter less problems later on.
# exit $ mkdir $HOME/src $ cd $HOME/src $ wget http://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.6.tar.gz $ tar -xzvf ruby-1.8.6.tar $ cd ruby-1.8.6 $ ./configure $ make $ su ******* # make install # exit $ which ruby /usr/local/bin/ruby $ ruby -v ruby 1.8.6 (2007-09-23 patchlevel 110) [i686-linux] $ cd $HOME/src $ wget http://rubyforge.org/frs/download.php/28174/rubygems-0.9.5.tgz $ tar -xzvf rubygems-0.9.5.tgz $ cd rubygems-0.9.5 $ su ****** # ruby setup.rb # exit # gem -v 0.9.5 # gem install fcgi
IV. Install Rails
# gem install rails --include-dependencies
V. Install Lighttpd with SSL
# openssl version OpenSSL 0.9.8b 04 May 2006 # exit $ cd $HOME/src $ wget http://www.lighttpd.net/download/lighttpd-1.4.18.tar.gz $ tar -xzvf lighttpd-1.4.18.tar.gz $ cd lighttpd-1.4.18 $ ./configure --with-openssl --with-openssl-libs=/usr/lib/openssl ... ... ... Plugins: enabled: mod_access mod_accesslog mod_alias mod_auth mod_cgi mod_compress mod_dirlisting mod_evhost mod_expire mod_extforward mod_fastcgi mod_flv_streaming mod_indexfiles mod_proxy mod_redirect mod_rewrite mod_rrdtool mod_scgi mod_secdownload mod_setenv mod_simple_vhost mod_ssi mod_staticfile mod_status mod_trigger_b4_dl mod_userdir mod_usertrack mod_webdav disabled: mod_cml mod_magnet mod_mysql_vhost Features: enabled: auth-crypt compress-bzip2 compress-deflate compress-gzip large-files network-ipv6 network-openssl regex-conditionals disabled: auth-ldap stat-cache-fam storage-gdbm storage-memcache webdav-locks webdav-properties $ make $ su ****** # make install # mkdir /etc/lighttpd/ # cp doc/lighttpd.conf /etc/lighttpd/ # cp doc/rc.lighttpd /etc/init.d/lighttpd # chmod +x /etc/init.d/lighttpd # mkdir /var/www # mkdir /var/www/html # groupadd lighttpd # useradd -g lighttpd -d /var/www -s /bin/false lighttpd # chown -R lighttpd.lighttpd /var/www # chown -R lighttpd.lighttpd /var/log/lighttpd
Now let's Configure Lighttpd. As stated earlier, there is a public IP address bound to the network card. We are going to bind the lighttpd daemon to the public IP address on port 80 and port 443. This assumes that you have correctly configured an SSL certificate. The following lines in /etc/init.d/lighttpd.conf should look like the following:
server.username = "lighttpd"
server.groupname = "lighttpd"
accesslog.filename = "/var/log/lighttpd/access.log"
$HTTP["host"] =~ "bryanhinton\.com$" {
server.document-root = "/var/www/html/bryanhinton/public/"
accesslog.filename = "/var/www/html/bryanhinton/log/access.log"
server.indexfiles = ( "dispatch.fcgi", "index.html" )
server.error-handler-404 = "/dispatch.fcgi"
# rails stuff
#### fastcgi module
fastcgi.server = (
".fcgi" => (
"test" => (
"socket" => "/tmp/test1.socket",
"bin-path" => "/var/www/html/bryanhinton/public/dispatch.fcgi",
"min-procs" => 1,
"max_procs" => 2
)
)
)
}
$SERVER["socket"] == "74.53.242.146:443" {
ssl.engine = "enable"
ssl.pemfile = "/etc/lighttpd/ssl/bryanhinton.com/bryanhinton.pem"
server.name = "bryanhinton.com"
server.document-root = "/var/www/html/bryanhinton/public/"
}
VI. Start Lighttpd
# /etc/init.d/lighttpd start
VII. Configure a Rails Application
First, let's make sure our standard login ID is associated with the lighttpd group. Open /etc/groups and make sure that there is a line that looks like the following:
lighttpd:x:521:lighttpd,YOUR_LOGIN_ID
Configure a Rails Application
$ cd /var/www/html $ rails bryanhinton $ cd bryanhinton $ ruby script/generate controller Content $ rm public/index.html
edit config/routes.rb
# add this to line 21 map.connect ':action', :controller => 'Content' # original line 24 #map.connect '', :controller => "welcome" # change to this on line 24 map.connect '', :controller => 'Content'
Setup smtp so that you can send e-mail from Rails. Modify config/environments/development.rb so that it looks like the following:
config.action_mailer.raise_delivery_errors = false
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.smtp_settings = {
:address => "smtphost.bryanhinton.com"
:port => 25,
:domain => "bryanhinton.com"
:authentication => :log
:user_name => "INSERT USERNAME HERE"
:password => "INSERT PASSWORD HERE"
}
IIX. Create a development database in MySQL and then Configure in Rails
$ mysqladmin -u myusername create bryanhinton_development -p $ gem install mysql
Edit config/database.yml and ensure that it looks similar to the following. We are only setting up a development database
development: adapter: mysql database: bryanhinton_development username: MY_MYSQL_USER_NAME password: MY_MYSQL_PASSWORD socket: /var/lib/mysql/mysql.sock
IX. Test the database connection
$ cd /var/www/html/bryanhinton $ rake db:migrate
X. Create site layout
$ cd /var/www/html/bryanhinton/app/views/layouts
Place the following lines in application.rhtml
<html> <body> <%= render :partial => "layouts/header" %> <%= render :partial => "layouts/leftnav" %> <%= render :partial => "layouts/rightnav" %> <%= yield :layout %> <%= render :partial => "layouts/footer" %> </body> </html>
Create the files that application.rhtml references
$ echo "My Header" > _header.rhtml $ echo "Left Menu" > _leftnav.rhtml $ echo "Right Col News" > _rightnav.rhtml $ echo "My Footer" > _footer.rhtml
XI. Create an index method in the Content controller
Place the following lines in /var/www/html/bryanhinton/app/controllers/content_controller.rb
class ContentController < ApplicationController def index end
XII. Create a view for the index method
Create a view for the index method called index.rhtml in /var/www/html/bryanhinton/app/views/content
$ cd /var/www/html/bryanhinton/app/views/content $ echo "Hello World!" > index.rhtml
XIII. Test your installation!
Open up a web browser and go to your Web site. i.e. http://bryanhinton.com OR http://bryanhinton.com/index
