Updating Ruby on Rails on Mac OS X Leopard 1
Introduction
The purpose of this post is to quickly and efficiently update you Ruby on Rails stack on Mac OS X Leopard.
Requirements
Caution: Before this procedure is start you must have a cursory knowledge of using the Terminal in Mac OS X.
- Fully Patched Install of Mac OS X Leopard
- Apple Developer Tools
Procedure
Open Terminal
Run the following
sudo gem update –system
Run the following command to update all your installed gems to the latest version
sudo gem update
That is it!
Note: All the previously installed gems will stay installed as a backup but ruby will use the latest gems that are installed on the system.
Ruby on Rails Development Stack on Mac OS X Leopard
Introduction
The main purpose of this howto is to offer instructions on how to setup a development stack for Mac OS X Leopard. There will be no beefy server backend so the web hosting portion of this tutorial will stop at mongrel. I will be using the built in rails stack and just updating the necessary packages. Now lets get this solid foundation patched and development ready. Enjoy.
Environment
Leopard ships with Ruby 1.8.6 patchlevel 36 with extra goodies that got merged in. The reason why Ruby was not the latest which is available today for download from Ruby-Lang is because it was released after the deadline. Do not fret the security vulnerabilities were patched. Since the version that ships with Leopard is “framework-ized” we will stick with that version and updating the Ruby on Rails installation around it.
Pre-Installed Gems
Below is a list of all the gems that come installed on a default Leopard install. The stack below is before anything has been done to a clean OS X install. We will use this as the base of our stack and build from here.
- actionmailer (1.3.3)
- actionpack (1.13.3)
- actionwebservice (1.2.3)
- activerecord (1.15.3)
- activesupport (1.4.2)
- acts_as_ferret (0.4.1)
- capistrano (2.0.0)
- cgimultiparteof_fix (2.2)
- daemons (1.0.7)
- dnssd (0.6.0)
- fastthread (1.0)
- fcgi (0.8.7)
- ferret (0.11.4)
- gem_plugin (0.2.2)
- highline (1.2.9)
- hpricot (0.6)
- libxml-ruby (0.3.8.4)
- mongrel (1.0.1)
- needle (1.3.0)
- net-sftp (1.1.0)
- net-ssh (1.1.2)
- rails (1.2.3)
- rake (0.7.3)
- RedCloth (3.0.4)
- ruby-openid (1.1.4)
- ruby-yadis (0.3.4)
- rubynode (0.1.3)
- sources (0.0.1)
- sqlite3-ruby (1.2.1)
- termios (0.9.4)
Updating Current Environment
First things first we need to update RubyGems is 0.9.5 which is fully supported by Leopard. To complete this step please run the following command in the terminal.
sudo gem update --system
Once this completes RubyGems will be fully patched and you will be ready to update the Ruby on Rails gems to their latest version.
Upgrading Installed Gems
Now that RubyGems is updated we are ready to update all the installed gems on the system.
sudo gem update
Once that has completed you will not have the latest version of stable rails as well as all the other fabulous gems that ship with Mac OS X.
Testing Your New and Updated Ruby on Rails Stack
cd ~/Sites
rails Test
cd Test
./script/server &
open http://localhost:3000
Adding Support For PostgreSQL Databases
In the next post on this topic I will be going over how to add support for PostgreSQL 8.2.5 as well as compiling PostgreSQL with DTrace support under Mac OS X.
Stay Tuned…
Ruby on Rails Deployment
Introduction
Ruby on Rails is a wonderful platform for web applications and rapid web development. That being said when running an application using the ruby interpreter as well as the plethora of deployment options one can get confused quite quickly. I am writing this post to try and clear the muddy water of deploying Ruby on Rails web applications.
Platform Choices
Disclaimer: None of the recommendations below are tested for the Win 32 platform and there is a high probability that nothing will work correctly or at all.
All of the following deployment recommendations I will be writing about are achievable on most if not all of the UNIX/Linux/BSD operating systems that are available today. Now that we have those very important logistics out of the way lets get started with the foundation for rails deployment!
Note: Mac OS X Leopard (The next release slated for October) will ship with the Ruby on Rails stack already installed and ready to start development.
If you are running Mac OS X Tiger then head over to my Mac OS X Ruby on Rails setup How-To to get yourself up and running with an optimized server-grade Ruby on Rails installation.
Don't be too thrown off by that title because it can be used on any UNIX/BSD Operating System. Just switch up to a source based installation or use the package manager built into your OS.
Foundation
I have two recommendations for optimized Ruby on Rails hosting. I will go over the benefits of each and when to choose one over the other. Both options require an external listening web server and a process to access the Ruby on Rails web application.
Web Servers
I have two favorite WebServers that I use currently. One which runs the blog and one that I run on my internal servers. Each of the below web servers each offer their own advantages to the mix which you can weigh and choose the one based on your requirements.
- Nginx (Pronounced "Engine X")
- LiteSpeed
For example LiteSpeed is not open source and there are two version one which is "more optimized" which costs quite a bit of money. Both the free and commercial versions are managed via a web interface. There is also an option at the time of installation to enable AWstats to monitor the traffic on your website. This is quite simple to setup and maintain. The upgrade process detailed blow is extremely simple to do and requires minimal time and effort.
Ruby on Rails Dispatcher
There are two methods of dispatching the Ruby on Rails web application that I use. One which is handled though an API which is installed via a gem (Ruby LSAPI) and one that runs a backend server which handles the dynamic Ruby content (Mongrel).
- LiteSpeed Ruby LSAPI
- Mongrel HA Cluster
If you have never setup a High-Available cluster before don't worry mongrel makes this process very simple. The configuration for a mongrel cluster is also extremely easy to create and understand. The config file itself can be as small as three lines. I would recommend against this as there are some options that would allow for a more secure mogrel cluster. As you can see below mine is a little bit longer but still you can agree with me that it is quite easy.
---
cwd: /path/to/railsapp/
log_file: log/mongrel.log
port: "8000"
environment: development
address: 127.0.0.1
pid_file: tmp/pids/mongrel.pid
servers: 3
Comparison
Both of the above options are very good choices when deploying Rails web applications. Each have drawbacks. For example if you are on a resticted VPS or other RAM limited server then the choice would be to use the Ruby-LSAPI because it requires only one ruby process to be run thus saving you tons of memory. On average a ruby process takes about 32MB of RAM. This can be quite detrimental if you only have 256MB on a VPS if you are running a 3 server HA mongrel cluster which would require (you guessed it) 96 MB of RAM! That is without the web proxy running which is required by mongrel cluster to proxy between the mongrel servers.
On to the comparison of the web servers that we use as a reverse proxy, Litespeed and Nginx. Both are wonderful web servers that are efficient and have a very small memory footprint. Each taking about only 2MB of RAM to run with minimal load. As the load increases the RAM usage does not increase that much. That being said Litespeed with Ruby-LSAPI is a wonderful choice when RAM is a consideration. When running development mongrel and Nginx might seem more responsive depending on the load and the amount of mongrel servers you have running in the cluster.
Mac OS X Ruby on Rails Setup Howto
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