Introduction

This howto will discuss a full rails installation on a Mac. This machine can be used strictly for development or used as a server. The install can scale so well because of its design. It might be a little overkill for a development box but what better than to test your web app on the same setup as your server environment.

Installation Method

I used MacPorts to install, I selected this process because of the easy upgrade path. This project has been solid for some time now. The packages offered by this port system are updated with a very quick turnaround.

Packages Used

  • Ruby 1.8.6 (Latest)
    • libiconv
    • readline
    • openssl
    • zlib
    • ncurses
  • Nginx 0.5.29 (0.5.31 Latest Stable)
    • pcre
    • zlib
  • PostgreSQL 8.2.4 (Latest)
  • Ruby Gems 0.9.4 (Latest)
    • ruby
  • Subversion 1.4.4 (Latest)
    • expat
    • neon
    • apr
    • apr-util
    • db44
    • gettext
    • libiconv
  • Capisrtano 2.0.0 (Latest)
    • net-ssh (>= 1.0.10)
    • net-sftp (>= 1.1.0)
    • highline (> 0.0.0)
  • Rails 1.2.3 (Latest)
    • rake (>= 0.7.2)
    • activesupport (= 1.4.2)
    • activerecord (= 1.15.3)
    • actionpack (= 1.13.3)
    • actionmailer (= 1.3.3)
    • actionwebservice (= 1.2.3)

Service Topology

HTTP Request
    |
    |
Nginx (Web Server)
    |
    |
Mongrel Cluster-----[SVN(Capistrano)]
| | |                                   
| | +mongrel=\ 
| +mongrel====[Rails App]
+mongrel======/

Installation

In this section I will go over the installation of the packages required for this rails deployment environment. I will try and discuss my reasons behind my choices. Please post any comments below. First and foremost we must install the package manager used in this tutorial. MacPorts is a delightful package manager for Mac OS X. It is psuedo-endorsed by Apple, Inc. (They host the web page) http://www.macports.org/

Nginx

At first I was quite skeptical about using this product, I did not believe the numbers I saw in performance so I decided to check it out. Really at the end of the day it doesnt matter what you use here as long as it is not LightTPD (because of incompatibilities between LightTPD and Mongrel) as long as the web server you use support some sort of proxy.

Installation

sudo port install nginx

IMPORTANT MacPorts will prompt you at the end of the installation and ask if you want to install a launchd service to start nginx on boot. I recommend this highly because it makes managing the server very easy.

Nginx Config File

worker_processes  1;

events {
    worker_connections  1024;
}

http {
  include       etc/nginx/mime.types;
  default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] $request '
  #                  '"$status" $body_bytes_sent "$http_referer" '
  #                  '"$http_user_agent" "$http_x_forwarded_for"';

  #access_log  logs/access.log  main;

  sendfile        on;
  #tcp_nopush     on;
    tcp_nodelay        on;

  #keepalive_timeout  0;
  keepalive_timeout  65;

  gzip  on;

  upstream mongrel {
    server 127.0.0.1:8000;
    server 127.0.0.1:8001;
    server 127.0.0.1:8002;
  }

  server {
    listen       80;
    server_name  localhost;

    root /Users/rvalente/Sites/snoompa/public;
    index  index.html index.htm;

    location / {
      proxy_set_header  X-Real-IP  $remote_addr;
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect false;
      if (-f $request_filename/index.html) {
        rewrite (.*) $1/index.html break;
      }
      if (-f $request_filename.html) {
        rewrite (.*) $1.html break;
      }
      if (!-f $request_filename) {
        proxy_pass http://mongrel;
        break;
      }
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
  }
}

Ruby

Ruby is self explanatory, if you are questioning why I am installing this you should prolly be reading http://www.ruby-lang.org. I have always used the latest version of Ruby and have always had the best luck with that stradegy. That being said it is quick and easy when using a package manager.

Installation

sudo port install ruby

Verification

To make sure you are running the correct version of Ruby run the following command:

ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.10.1]

Ruby Gems

Ruby gems is the package manager used to install all the software used within ruby. For example if you wanted to find a gem that dealt with snmp communication you could run the following command: gem search --remote snmp Once the rubygem package manager is installed you can use with MacPorts to update it or you could use itself by running the command port update rubygems

Installation

sudo port install rb-rubygems

Hint: Open a terminal window and run gem_server and then navigate in your web browser to http://localhost:8808 This is the rdoc for each of the packages installed via the gem package manager.

Ruby on Rails

Here is the money application right here. Ruby on Rails is the magic that pulls all this together. Just with a quick one-line command you will have all the power you need to create web applications.

Installation

sudo gem install -y rails

Note: You may need to run the previous command twice. There is some sort of bug which makes the rails install fail the first time you run this command.

PostgreSQL 8.2.4

PostgreSQL is a superior database built for speed and scalability. This database in my humble opinion beats just about everything that MySQL might have to offer. PostgreSQL 8 has a plethora of benefits over mysql including offering a better user experience. PostgreSQL has been made extremely easy to install on the mac. It has been packaged and maintained by Andy Satori. The sourceforge link to download this package installer is below.

http://sourceforge.net/project/showfiles.php?group_id=133151&package_id=146535\

After the PostgreSQL database server is installed you will need the API to connect Ruby to the database server. This is accomplished with the following command:

sudo gem install postgres -- --with-pgsql-dir=/Library/PostgreSQL8/

Mongrel

Mongrel is a wonderful and fast web server for hosting rails web applications. The mongrel_cluster suite of applications makes creating High Availability web applications very easy. The configuration file has an easy syntax to follow and write yourself.

Installation

sudo gem install -y mongrel_cluster

Configuration

The configuration for mongrel is actually quite easy, below is an example of the mongrel_rails command to configure the cluster portion on mongrel. Since rails does not support thread synchronization it is highly recommended to run multiple mongrel clusters even on a development box. I currently run three as you saw by the upstream directive in the nginx configuration.

mongrel_rails cluster::configure -h
Usage: mongrel_rails <command> [options]
    -e, --environment ENV            Rails environment to run as
    -p, --port PORT                  Starting port to bind to
    -a, --address ADDR               Address to bind to
    -l, --log FILE                   Where to write log messages
    -P, --pid FILE                   Where to write the PID
    -c, --chdir PATH                 Change to dir before starting (will be expanded)
    -t, --timeout SECONDS            Timeout all requests after SECONDS time
    -m, --mime PATH                  A YAML file that lists additional MIME types
    -r, --root PATH                  Set the document root (default 'public')
    -n, --num-procs INT              Number of processor threads to use
    -B, --debug                      Enable debugging mode
    -S, --script PATH                Load the given file as an extra config script.
    -N, --num-servers INT            Number of Mongrel servers
    -C, --config PATH                Path to cluster configuration file
        --user USER
                                     User to run as
        --group GROUP
                                     Group to run as
        --prefix PREFIX
                                     Rails prefix to use
    -h, --help                       Show this message
        --version                    Show version

Now that we see all the options possible in the mongrel_rails configuration of the cluster here is an example of a simple configuration file.

--- 
log_file: log/mongrel.log
port: 8000
pid_file: tmp/pids/mongrel.pid
servers: 3
debug: false
docroot: public
address: 127.0.0.1
cwd: /path/to/rails/app

Subversion

Subversion is a superb version control system that is used abundantly in the rails world. Capistrano leverages this when deploying applications over ssh to a server. Subversion is the version control system we use here at SysAdmins' Chronicles for our development.

Installation

sudo port install subversion

Configuration

Subversion is quite easy to configure. Depending on the use of the subversion server changes a few aspects. If the server will be run locally then there is no need for a server and can be accessed via the file:/// prepend. One thing to note about that is the lack of support from capistrano using that method. Subversion repositories can be served up through many other ways included the shipped svnserve application as well as svn+ssh:// which is automatically accessible if a subversion server is running and uses user accounts as authentication, lastly you can use http/https via the DAV protocol to host the repository. For this purpose I will use the svn+ssh:// method because there is no configuration other than the initial creation of the repository.

svnadmin create --fs-type=fsfs ~/repo

Note: I recommend using the --fs-type=fsfs flag because it is superior to the berkeley db implementation originally used in the older versions of subversion and now using the fsfs file structure for the repository is recommended.

Capistrano

Capistrano is an amazing solution for application deployment not only for web applications and ruby on rails but also scripts and system administration tasks with any server. More on that topic later. For now just enjoy the simplistic capistrano deployment workflow for Ruby on Rails.

Installation

sudo gem install -y capistrano

Leave a Reply