Feed on

Once in a while, I catch the startup bug due to people like Sean Burgess.  So this post is based on hours of frustration that I had from setting up multiple osx machines to deploy rails.  Much of this is for my own reference and borrows heavily from these sites and my own attempts on 4 different macs:

  1. http://www.frederico-araujo.com/2010/08/19/installing-rails-enviroment-on-snow-leopard-with-macports-mysql-and-modrails-passenger/
  2. http://2tbsp.com/content/install_apache_2_and_php_5_macports
  3. https://trac.macports.org/wiki/howto/MAMP

1. Preparing System

In general, just easiest to start from scratch if you haven’t used ports in a while (or have never used it). Also make sure that web sharing is OFF under System Preferences as we are going to install the apache version from macports instead.

sudo rm -rf \
    /opt/local \
    /Applications/DarwinPorts \
    /Applications/MacPorts \
    /Library/LaunchDaemons/org.macports.* \
    /Library/Receipts/DarwinPorts*.pkg \
    /Library/Receipts/MacPorts*.pkg \
    /Library/StartupItems/DarwinPortsStartup \
    /Library/Tcl/darwinports1.0 \
    /Library/Tcl/macports1.0 \

1.1 Download and Install Xcode & Macports


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

1.3.1 Update macports

sudo port -v selfupdate
port upgrade outdated

2. Apache, php5, mysql & PhpMyAdmin

2.1 Install Apache, php5, PhpMyAdmin and misc

sudo port -v install phpmyadmin php5 php5-mysql apache2 mysql5-server mysql5 memcached libmemcached wget sqlite3 +apache2 +pear

2.2 Make apache, mysql autoload on startup

sudo -u mysql mysql_install_db5
sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist
sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
sudo port load memcached

2.3 Activating PHP5

cd /opt/local/apache2/modules
sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so

2.4 Add index.php to the dir_module directive

Open /opt/local/apache2/conf/httpd.conf in your favorite text editor.

<IfModule dir_module>
    DirectoryIndex index.html index.php

2.5 Add mod_php and phpmyadmin

Also, at the end of the httpd.conf file, add the following lines so that Apache includes the mod_php and phpmyadmin file

#Include PHP configurations
Include conf/extra/mod_php.conf
# Local access to phpmyadmin installation
Include conf/extra/httpd-phpmyadmin.conf

Set up your PHP configuration files

cd /opt/local/etc/php5
sudo cp php.ini-development php.ini

Setup the MySQL default socket to use the CentOS MySQL socket location by editing php.ini


2.6 Building my.cnf and changing the MySQL default socket to conform to CentOS (optional)

Open /opt/local/apache2/conf/httpd.conf in your favorite text editor. Add a new mimetype so that Apache will direct files ending in .php to the PHP module for processing. Add the following within the block. Without this, all you’ll see is the text of your PHP scripts

sudo cp /opt/local/share/mysql5/mysql/my-medium.cnf /etc/my.cnf
sudo mkdir /var/lib/mysql/
sudo chown -R _mysql:_mysql /var/lib/mysql

Change the TWO lines in /etc/my.cnf that says

socket = /opt/local/var/run/mysql5/mysqld.sock


socket = /var/lib/mysql/mysql.sock

2.7 Create the phpmyadmin configuration file

create a file /opt/local/apache2/conf/extra/httpd-phpmyadmin.conf containing this text:

AliasMatch ^/phpmyadmin(?:/)?(/.*)?$ "/opt/local/www/phpmyadmin$1"

<Directory "/opt/local/www/phpmyadmin">
  Options -Indexes
  AllowOverride None
  Order allow,deny
  Allow from all

  LanguagePriority en de es fr ja ko pt-br ru
  ForceLanguagePriority Prefer Fallback

2.8 Setup aliases to control the two services

sudo  ln -s /opt/local/apache2/bin/apachectl /usr/sbin/apache2
sudo ln -s /opt/local/share/mysql5/mysql/mysql.server /usr/sbin/mysql.server
sudo apache2 restart
sudo mysql.server restart

2.9 Setup the mysql root password

Set the MySQL root password. Where is your new desired root password. You will be prompted for your existing password (“Enter password:”); since it’s empty, just press Return.

mysqladmin5 -u root -p password <new-password>

Then log into the mysql connection and use these commands (replacing your root password of course) to update all the root accoutns

shell> mysql -u root -p
mysql> UPDATE mysql.user SET Password = PASSWORD('newpwd')
    ->     WHERE User = 'root';

2.10 Test the PHP server

Make an index.php in /opt/local/apache2/htdocs and put the following in it:


2.11 Install and Test phpmyadmin

First make these changes in /opt/local/www/phpmyadmin/config.inc.php

$cfg['blowfish_secret'] = WHATEVER YOU WANT THIS TO BE

You should see quite a bit of information regarding your php installation.

3. Ruby, Rubygems, Rails, other gems

3.1 Install Ruby from macports

sudo port -v install ruby
$ ruby -v
ruby 1.8.7 (2010-08-08 patchlevel 302) [x86_64-darwin10]

3.2 download rubygems from http://rubygems.org/pages/download

cd /tmp
wget http://production.cf.rubygems.org/rubygems/rubygems-1.3.7.tgz
tar -xvf rubygems-1.3.7.tgz
cd rubygems-1.3.7
sudo ruby setup.rb

3.3 Install rails, rake, rspec etc.

sudo gem install rake rails thin tzinfo capistrano ruby-debug rspec mysql sqlite3-ruby

4. ImageMagick, Rmagick and mini_magick (Optional)

lets install ImageMagick with support for JPEG, TIFF, WMF, PDF, and PNG images, and for Postscript and TrueType fonts.

sudo port -v install tiff -macosx imagemagick +q8 +gs +wmf
sudo gem install mini_magick rmagick

Test rmagick

$ irb -rubygems -r RMagick
>> puts Magick::Long_version
This is RMagick 2.13.1 ($Date: 2009/12/20 02:33:33 $) Copyright (C) 2009 by Timothy P. Hunter
Built with ImageMagick 6.6.3-0 2010-08-19 Q8 http://www.imagemagick.org
Built for ruby 1.8.7
Web page: http://rmagick.rubyforge.org
Email: rmagick@rubyforge.org
=> nil

5. Passenger

5.1 Install Passenger gem

sudo gem install passenger

5.2 Check Passenger path

passenger-config --root
 -> /opt/local/lib/ruby/gems/1.8/gems/passenger-2.2.15

5.3 Build passenger for apache

and don’t worry about the other steps, we will cover them directly in the guide

sudo passenger-install-apache2-module

5.4 Enable Passenger on apache:

create /etc/apache2/extra/httpd-passenger.conf and change wherever necessary for your directories.
LoadModule passenger_module /opt/local/lib/ruby/gems/1.8/gems/passenger-2.2.15/ext/apache2/mod_passenger.so
PassengerRoot /opt/local/lib/ruby/gems/1.8/gems/passenger-2.2.15
PassengerRuby /opt/local/bin/ruby
PassengerMaxPoolSize 6  # maximum global rails servers
PassengerMaxInstancesPerApp 2  # maximum rails servers per application
RailsFrameworkSpawnerIdleTime 1800
RailsAppSpawnerIdleTime 600
PassengerPoolIdleTime 600
PassengerMaxRequests 1000  # after 1000 requests will restart server, to skip memory leak :) 

# Enabling NameBased Virtualhost

# my rails app virtual host 1
    ServerName my-rails-app.local
    DocumentRoot "/Users/jackpo/rails/my-rails-app/public" # change this to match your folder
    RailsEnv "development"
   <Directory /Users/jackpo/rails/my-rails-app/public>
        # change this to match your folder
        # MultiViews must be turned off
        Options -MultiViews
        AllowOverride All
        Order allow,deny
        Allow from all
    # logs are optional, change this to match your folder
    CustomLog  "/Users/jackpo/rails/my-rails-app/log/access_log" combined
    ErrorLog   "/Users/jackpo/rails/my-rails-app/log/error_log"

5.5 Enable Virtual host on apache:

Edit /opt/local/apache2/conf/httpd.conf and add this new line at the bottom:

# Include Passenger ModRails config file
Include conf/extra/httpd-passenger.conf

5.6 Add your .local domain to /etc/hosts/

edit /etc/hosts and Add this line to that file and change to the name of your choosen application

You might add as many as you want, each line for each that ServerName on your VirtualHost blocks my-rails-app.local myapp-xyz.local

5.6 Restart Apache

sudo apache2 restart

6. Final

go to your browser and open the url of your application http://my-rails-app.local

If it works, congratulations. If it didn’t.. uh.. try and google. I maybe able to help, but I don’t have that much time!

  • http://rob-lambert.myopenid.com/ Rob

    Thanks! was a great help in getting me up and running

  • Ming Jack Po

    Nice! I’m glad that was useful to someone. My first install was
    super painful because there were no real documentations online!

  • Joeri

    Super painful?? I installed my server, using mod_passenger (Phusion Passenger) and the special ruby enterprise edition, to support rails apps without the need of a separate web server like WebRick or Mongrel. This must be around five years ago now so even less to find about configurations. I compiled all from source, that is to say: my PHP, MySQL and PostgreSQL, full server side aspell integration for spell check in FckEditor etc… I initially started with a clean CentOS.

    Because it was my first time setting up a complete physical Linux server I followed parts and bits of guides, fora, blogs, etc… until I managed to set up a full physical Linux server with RoR, PHP and Java web hosting possibilities but also cPanel and even a fully functional File Server (FTP), Mail server (courier) with Webmail and all included….

    THE ONLY THING THAT WAS VERY GOOD DOCUMENTED WAS HOW TO INSTALL AND USE RubyEnterprise instead of the default ruby AND HOW TO INSTALL apache_mod_passenger using Phusion Passenger or the ruby passenger gem. They even provided the possibility to copy the, for you custom generated, passenger.conf configuration and the full Virtual Host configuration.

    So can I ask Ming Jack Po what exactly was so super painful about your first installation experience?

    Best regards!