Main Content:

Posts tagged as 'mod_rails'

8 mod_rails on Mac 0 comments

May 22

Prerequisites

You should have a Mac, preferably with Leopard installed. I haven't personally tested this on Tiger, however Neil may be doing so soon. If you are using Tiger make sure you have Rails already installed, as Leopard comes with it built in.

Initial Install

Boot up terminal and run the following commands to install mod_rails:

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

It tells you to add some lines to your Apache config so switch to finder, hit cmd+shift+g and enter "/etc/apache2/" then open "httpd.conf" in TextMate (or your favorite editor).

Scrolling down, there should be a massive section with loads of lines starting with "LoadModule" at the end of these add the lines mod_rails told you in the Terminal. Example:

# RAILS LoadModule passenger_module /Library/Ruby/Gems/1.8/gems/passenger-1.0.5/ext/apache2/mod_passenger.so RailsSpawnServer /Library/Ruby/Gems/1.8/gems/passenger-1.0.5/bin/passenger-spawn-server RailsRuby /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby

Switch back to terminal and create your test Rails project:

cd sites rails test

And finally boot up apache. You can find the "Web Sharing" option in the Sharing section of your System Preferences, simply check the box. You'll be provided with some URLs, take the one with your username and add "test/public" on the end. Example:

http://192.168.0.15/~ryantownsend/test/public

Navigate to this in your browser, and you should see the Rails page.

Nicer URLs

You could stop at this point, but in reality, your website won't have your username, the rails application name or the public folder in it's URL. To tidy this up you need to configure Apache a little more.

Back to Terminal, enter the following commands:

cd /etc/apache2/ sudo cp other/php5.conf other/rails.conf mate other/rails.conf

Note: If you don't have TextMate, simply go to the "/etc/apache2/" directory in Finder again and open the "other" folder then edit the rails.conf file.

Delete all the text within this file and replace it with:

<VirtualHost *:80> ServerName local.test.com DocumentRoot /Users/YOUR_USERNAME_HERE/Sites/test/public/ </VirtualHost>

The server name is whatever you want to type in your browser for the local site to come up, and the document root is the path to your Rails application's public folder.

Because local.test.com isn't a real live site, we need to force our DNS to know it's local to our machine. Back to your "/etc" folder in Terminal or Finder, open up the "hosts" file.

You should see a few entries already there for a few hostnames, and the format is: IP address(tab)Hostname. So on a new line add:

127.0.0.1 local.test.com

"127.0.0.1" is the local IP address for any machine, so you don't need to change that, just change "local.test.com" to whatever hostname you used in your "rails.conf" file earlier.

And we're done, you should now be able to access your application from the hostname you've decided upon.

Any problems, leave a comment and I'll do my best to help.