Go to content Go to menu

I’ve been working on learning Ruby and getting Rails all figured out. It’s been a lot of fun and I’m pretty happy with the way it’s working out so far. For the last couple of years it’s been all Java all the time and it’s nice to take a break.

My first big task was to get Ruby and Rails working on my workstation. That sounds pretty easy and it is, sort of. There are many introductory tutorials and they get you to the point where you can run some Ruby code and put together a simple Rails project. But that’s where they leave you and it’s a little bit shy of actually deploying a real application. To quote Apple:

To do any serious Rails development you’ll want to install a production-quality web server, database server, and a few other goodies.

And I do. In fact, now that I’m thinking about it, I’d like to get as close to my deployment environment as possible. It’s a real server, it’s running FreeBSD and Apple’s always bragging on the “UNIX underpinnings” so this shouldn’t be too hard. So, yeah, I’d like to install Ruby and Rails, then Apache 2 and MySQL 5. That’d be totally kick ass, I could even move data between my deployment server and my workstation to troubleshoot.

Most of the tutorials don’t deal with these issues. Apple gives it a shot, but they recommend Locomotive. It’s a pretty nice product and I like a nice GUI just as much as the next guy, but this is the sort of thing I’ve become used to dealing with at the prompt. My server is managed from the prompt and maybe this makes me some kind of backwards knuckle-dragger, but I’ve grown to like it that way. I decided that these introductory articles simply are not for me and I needed to write my own (you are reading it now).

The FreeBSD server that I’ll be deploying my fancy new Ruby apps onto has this nice, relatively painless tool/system called port that manages installing and upgrading software. It took me a while to get used to, at first I would tell port to update this or that critical chunk of infrastructure (like PHP) and I’d literally bite my nails as my stomach did somersaults. After doing this for a while and nothing bad happening, I finally learned to de-clench and trust port to do it’s thing.

On Mac OS X we have two choices in this space:

Fink is definitely the road more traveled. I spent some time with Fink back in the day when I was running Panther. It’s modeled after the Debian system, I know very little about the Debian system for managing packages. MacPorts is closer to the FreeBSD port system that I know, so I picked that one (it used to be called “DarwinPorts”, if that rings a bell).

Install MacPorts

Installing MacPorts is pretty easy as long as you have the Xcode tools installed (install them now if you haven’t already). I won’t go over the steps for either here. Before you run over to their installation page and start clicking about willy-nilly, here’s a tip for you: skip directly to step 4.

Installing MacPorts

Once you’ve done that, add the two directories for the MacPorts commands to your path. Open up the terminal and bring up your profile. I used “vi” to do this, if you use TextMate (which you should) use “mate”. instead.

vi .profile

You might have an entry where you export your PATH already. Add this line or alter your existing path.

export PATH="/opt/local/bin:/opt/local/sbin:$PATH"

This puts the MacPorts commands (in /opt/local/…) first in your path. If you install something that Apple already has installed (like Ruby), you’ll use the MacPorts version instead of Apple’s. Exit your Terminal session and open another so that your change takes effect.

Now that MacPorts is installed, give it a chance to update itself and it’s tree of software.

sudo port selfupdate

MacPorts will phone home and get the current tree of software and set it up on your Macintosh. If necessary, it’ll download a new version of itself as well.

Install Apache

Now that MacPorts is all updated and running, you’re all set to install some software. We’ll install Apache 2 first.

sudo port -v install apache2

If you leave off the “-v” flag, you won’t see the output from the compile process. I like to see it so that I know something is actually happening. Port will download the source code for Apache 2, apply patches from the MacPorts project, compile it and then install it. If you haven’t done so already, make sure that “Personal Web Sharing” isn’t running on your Macintosh. You can click it off in the “Sharing” control panel inside the System Preferences application.

Port doesn’t setup a httpd.conf file as part of the install process. It does, however, leave us with a working sample one. We’ll copy that over and use it as a starting point.

cd /opt/local/apache2/conf cp httpd.conf.sample httpd.conf

Enable User Home Directory Serving

This configuration is pretty good, but it won’t serve up files in the Sites folder in your Home folder. You probably want that, I know that I do. Open up the httpd.conf file and scan for a couple lines that read:

# User home directories

#Include conf/extra/httpd-userdir.conf

Remove that hash-mark in front of the “Include” and Apache will start serving up your personal Sites folder. This is very nearly what we want except that the default rules for the user directories are a little too restrictive. Open up that file and take a look…

vi extra/httpd-userdir.conf

I am the master of my workstation, so I’ve configured mine like so:

<directory> AllowOverride ALL Options ALL </directory>

Order allow,deny Allow from all

This lets me do pretty much whatever I want via .htaccess files in my Sites folder. You want to do this too, when you deploy Rails applications they have a bunch of mod_rewrite rules to keep the URL’s looking pretty.

With that done, let Mac OS X know that you want it to startup Apache 2 when the Macintosh starts up.

sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist

That will instruct launchd to read in the configuration file for Apache 2. If you want to start and stop Apache on command, you can still do that the old fashioned way.

sudo apachectl start

Install mod_fcgid

Myself, I’ll be deploying my Rails applications with mod_fcgid. I’ve had nothing but problems with FastCGI and, lets be honest, it doesn’t look like anyone is working on it. Still, Ruby needs to link against FastCGI. Lucky for us, MacPorts will let us install just the development kit.

sudo port -v install fcgi

With that out of the way, we can install mod_fcgid.

sudo port install mod_fcgid

Port will download, patch, and then compile the package. It’ll do everything but actually update your apache configuration file. Open that file up.

vi /opt/local/apache2/conf/httpd.conf

Scan down to the end of the “LoadModule” section and add one for mod_fcgid to the end.

LoadModule fcgid-module modules/mod-fcgid.so

Those dashes should be underscores (Grrr…) Restart Apache so that these changes take effect. If you’re using launchd, you do it like so…

sudo launchctl stop /Library/LaunchDaemons/org.macports.apache2 sudo launchctl start /Library/LaunchDaemons/org.macports.apache2

Most launchd scripts also have a “restart”, but not this one. I’m sure they’ll get that straightened out eventually. In fact, it might be fixed by the time your read this.

Install MySQL

We’ll need MySQL to provide database services to our Ruby applications. This is very similar to the steps for installing Apache.

sudo port -v install mysql5 +server

Once port has installed MySQL5, you have to initialize the database. Port lists the couple of commands you should run to finish up the MySQL installation. First, initialize the database used by MySQL itself.

sudo /opt/local/lib/mysql5/bin/mysql-install-db –user=mysql

Again, the dashes should be underscores. Damn you, WordPress!

Now that MySQL is installed, you have to tell launchd to load it and start it.

sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5 sudo launchctl start /Library/LaunchDaemons/org.macports.mysql5

With MySQL started and running, you can change the root password. You’ll want to do this twice, once for the root account at localhost and again for root at your hostname. Port should list these commands as well.

mysqladmin5 -u root password ‘mumble2837’ mysqladmin5 -u root -h emma.nervestaple.com password ‘mumble2837’

Install Ruby and Rails

With all of that legwork done, We’re all set to actually install Ruby!

sudo port -v install -v ruby

How easy is that? To install Ruby software and libraries, we’ll want to install gem as well.

sudo port -v install rb-rubygems

We’ll also want MySQL support.

sudo port -v install rb-mysql

And fcgi support.

sudo port -v install rb-fcgi

Lastly, we’ll want to install Rails. We can use gem for that, Rails is more of a Ruby thing and isn’t as dependent on locally installed libraries.

sudo gem install rails

Update: It’s also possible to install the Ruby/MySQL bindings with Gem, but you have to pass in the location of the MySQL library and header files. When you call gem, if your first argument is “––”, subsequent arguments are passed onto the script that builds the C library.

sudo gem install mysql -- --with-mysql-include=/opt/local/include/mysql5/mysql --with-mysql-lib=/opt/local/lib/mysql5/mysql

Adjusting Your Rails Application

With this setup, your Rails application really do not need to be altered very much. The only thing that needs to be changed is the .htaccess and the dispatch.fcgi files in the public folder of the app.

The .htaccess file contains the mod_rewrite rules that keep the URL’s clean and push requests to the dispatch script. Go ahead and pull that file up in your editor of choice. The first step is to comment out the handlers that we aren’t using. The top of your file should look something like this:

# General Apache options AddHandler fastcgid-script .fcgi AddHandler cgi-script .cgi AddHandler fcgid-script .fcgi Options +FollowSymLinks +ExecCGI

We’ll be using the third handler to invoke mod_fcgid. Go ahead and comment out the first two handlers.

Scan down towards the end of the file, you should see a hunk of text that looks similar to this:

# If your Rails application is accessed via an Alias directive,
# then you MUST also set the RewriteBase in this htaccess file.
#
# Example:
# Alias /myrailsapp /path/to/myrailsapp/public
# RewriteBase /myrailsapp

RewriteRule ^$ index.html [QSA] RewriteRule ^([^.]+)$ $1.html [QSA] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

The Rails application is being served from your home directory and that is very similar to an alias. We have to add a “Rewrite Base” line directly underneath the comments, this will be immediately before the first “RewriteRule”.

RewriteBase /~miles/r4rmusic1/public

My home directory is “miles” and my Rails application is called “r4rmusic”. Now, this won’t actually hide the other folders in your Rails application. That would require an Alias directive to be placed in the VirtualHost for your site and we’re not using an actual VirtualHost.

The last change to your application is to edit the “dispatch.fcgi” file, also in the “public” folder. We’re editing the first line. It should read:

#!/usr/bin/ruby

That will invoke the Ruby instance that came with Mac OS X. We want to use the one that we installed with MacPorts. Change that line to read:

#!/opt/local/bin/ruby

That’s it, everything is all set. To access this Rails app in my web browser, I’d load up an URL like this:

http://emma.local/~miles/r4rmusic1/public

There may be a way to get around the Alias restriction with mod_rewrite rules, but that would complicate this particular setup. For me, this is more than good enough.

That’s it! I hope you find this helpful. I know I’ll be coming back to this article the next time I have to setup another workstation. :)

Ed gave be a Vista Beta 2 DVD yesterday, the packaging is pretty nice. The install process is a bit lengthy, but not particularly arduous. The only tricky bit was removing the EFI partition, for some reason the Vista bootloader has a real problem with this extra partition. When the Vista installer prompts you to choose a partition, click on the EFI partition (it’s usually 199MB or 200MB in size) and then delete it.

After the installation process completes, the computer will reboot and startup from the new Vista volume. On my machines, there was an immediate error:

File: Windowssystem32winload.exe

Status: 0xc0000225

Info: The selected entry could be be loaded because the application is missing or corrupt.


This information is documented elsewhere on the internet, but the solution is simple: reboot from the Vista Beta 2 installation DVD and repair the volume. From the Vista “Install Now” screen, click the link towards the bottom that reads “System Restore Options”. The System Restore application will startup, ask you for your preferred language, then scan all of your volumes. In my case, it found my Vista volume and asked if I wanted to repair it and that took care of it.

Everything seems to be working, I have an Intel Core Duo Mac Mini and not all of my hardware is recognized. I’m going to scour the internet for drivers, the Boot Camp CD won’t install the drives. I’ll post some links if I have any success.

Update

I’ve decided that Vista Beta 2 isn’t really worth the hassle. The UI is substantially different, but I’m not convinced that translates into substantially better. It was fun to look at and mess around with for a couple of days, but I’m wiping it and putting XP back later today. I don’t want to harsh on Vista, but I’m not seeing why it took so long to get this puppy out the door.

technorati tags:, ,


Sun hasn’t released a JDK that works on OS X and it doesn’t look like they have any plans to, Apple doesn’t seem particularly interested either. You might think that your only option is to develop on a PC, but after some poking around on the internet I figured out a way to get a decent development environment setup. Yes, you can do 2.x development.

The only real speed-bump here is that you need a PC to unpack the files from Sun. After that, you copy them to the Mac and you never have to touch that PC again.

[Setting up to Develop for J2ME on Mac OS X](http://twitch.nervestaple.com/wiki/index.php/DoingMidletOnOsx)

AppleTalk IP On Linux

Feb 14, 10:34 AM

Running AppleTalk on Linux isn’t anything new, [Netatalk](http://netatalk.sourceforge.net/) has been around for a while. A long time ago, in a job far, far away I used it on an in-house server to support a couple aging Macs that management couldn’t seem to get rid of (and not for lack of trying). I remember that it worked well, but since I left that job I haven’t thought much about it.

A friend of mine recently noticed that [Dreamhost](http://netatalk.sourceforge.net/) (a hosting company) is running AppleTalk IP on there servers, though it is undocumented. I had thought that running AppleTalk over the internet was pretty much a dead idea. Whenever asked I would reply, “Why not just use WebDAV, like Apple does?” After trying it out, however, my mind was quickly changed.

WebDAV is really just a thin layer over the web server and it shows. Progress bars are sporadic and inaccurate, there’s a lot of beach-ball spinning, etc. Running AppleTalk IP through Netatalk, however, was far more responsive and all of the usual features of the Finder are supported.

Security is always my concern, but Netatalk supports encrypted passwords and SSH connections; what more could you ask? PAM support is good and I had it working with my LDAP back-end in no time. Right now I’m a big fan of Netatalk, my OS X clients couldn’t be happier.

The only glitch I came across was when installing the PAM configuration. The installation process installs it for you, but on my system it wasn’t working. I copied the SSHd configuration over the one for Netatalk and everything started working.

If I have any problems, I will be sure to post. If anyone has had any problems with Netatalk, I would be very interested to hear about them.