Friday, March 13, 2009

Ruby/Rails With Apache/MySQL on Mac OS (Leopard)

I'm a new Mac user after many years of working on both Linux and Microsoft systems. This last week I went through the process of setting up my system for web development using the Ruby/Rails/Apache/MySQL platform. I was a little surprised to find out it was not as straightforward as working on Linux, given the Mac OS has a BSD core and is the development platform of a lot of high profile programmers.

I'm writing this blog post primarily for my own reference. However, if it can help any others, all the better. Plus I welcome any feedback that may improve my setup.


Terminal Application

All command line actions should be done through the Terminal application. This can be found at Application/Utilities/Terminal.app.


MacPorts

MacPorts is a faculty similar to Aptitude in Ubuntu/Debian for installing software packages from the command line.

MacPorts software is installed in a separate location under /opt/local/. Therefore, you can always determine whether or not an executable is part of MacPorts by running a which [command] and seeing if it's located under this directory.

The current Leopard .dmg installer is version 1.7.0 and can be downloaded here:

http://www.macports.org/install.php


Once installed, open up a Terminal window and run the following command to make sure MacPorts is up to date


$ sudo port selfupdate



The Terminal PATH should have the MacPorts directories (/opt/local/bin, /opt/local/sbin) preceding the standard directories. The MacPorts installer set this for me automatically. If this is not the case for you, be sure to change this in the /etc/profile file.


$ echo $PATH
/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin



Ruby

Install Ruby and RubyGems via MacPorts.


$ sudo port install ruby
$ sudo port install rb-rubygems



The Leopard Mac OS includes older versions of both of these programs which we do not want to use. Check to make sure that the MacPorts version is the version called from the command line:


$ which ruby
/opt/local/bin/ruby

$ which gem
/opt/local/bin/gem



These are the current versions


$ ruby ‐v
ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-darwin9]

$ gem ‐v
1.3.1



Rails Framework

Install Rails framework files via RubyGems package manager.


$ sudo gem install rake
$ sudo gem install rails
$ sudo gem install capistrano
$ sudo gem install mongrel



This will install these packages with all dependencies. The following files should be installed


$ gem list

*** LOCAL GEMS ***

actionmailer (2.3.2)
actionpack (2.3.2)
activerecord (2.3.2)
activeresource (2.3.2)
activesupport (2.3.2)
capistrano (2.5.5)
cgi_multipart_eof_fix (2.5.0)
daemons (1.0.10)
fastthread (1.0.1)
gem_plugin (0.2.3)
highline (1.5.0)
mongrel (1.1.5)
net-scp (1.0.2)
net-sftp (2.0.2)
net-ssh (2.0.11)
net-ssh-gateway (1.0.1)
rails (2.3.2)
rake (0.8.4)



MySQL

MySQL can be installed through MacPorts or through a dmg installer. Since it’s not that important to me to have the latest version, I chose the dmg installer for simplicity.

The installer can be downloaded here. The current version is 5.1.32.

http://dev.mysql.com/downloads/mysql/5.1.html#macosx-dmg


The installer includes three parts:

Database: mysql-5.1.32-osx10.5-x86.pkg
Preference Pane: MySQL.prefPane
Launch Manager: MySQLStartupItem.pkg

I installed all three parts.

Now install the Ruby MySQL interface package:


$ sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql



Add the MySQL bin folder to the PATH variable for command line use. Append the following line to the local profile file (~/.profile).


# MySQL Command Line
export PATH=$PATH:/usr/local/mysql/bin



Apache

For Apache, I used the version included with the Mac OS.

The Passenger gem package integrates Apache with the Rails framework. Install using the following commands.


$ sudo gem install passenger
$ sudo passenger-install-apache2-module



The output of the last command should list some config settings to add to the Apache configuration. I added a separate file to put these in.


/etc/apache2/other/Passenger.conf

LoadModule passenger_module \
/opt/local/lib/ruby/gems/1.8/gems/passenger-2.0.6/ext/apache2/mod_passenger.so
PassengerRoot /opt/local/lib/ruby/gems/1.8/gems/passenger-2.0.6
PassengerRuby /opt/local/bin/ruby



I create a config file for each separate Rails project. The following example is for a project called ‘project1’.


/etc/apache2/other/project1.conf

<VirtualHost *:80>

ServerName www.project1.com
ServerAdmin admin@project1.com
DocumentRoot /Users/username/Work/web/project1/public
ServerSignature On

CustomLog /var/log/apache2/project1_access.log combined
ErrorLog /var/log/apache2/project1_error.log
LogLevel info

<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /Users/username/Work/project1/public/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>

</VirtualHost>



In order to view my web site in a local browser, I need to add a setting in the hosts file to redirect to the local host. See the last line of the following file. Of course, you need to make sure to remove this once you deploy your web site to a real server.


/etc/hosts

#
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost

127.0.0.1 www.project1.com project1.com

1 comments:

Avichal said...

Chris,

Thanks for the tutorial! It was a life saver.

Avichal

Followers

About Me