Aug 192010
 

UPDATE: New version for OS X Lion HERE

I choose the simple and I believe clean way to use everything from macports and passenger.

The advantage is that it’s all isolated from the OS X system and using latest cutting edge version of softwares.

I assume you have textmate, because it’s easy to edit files that need sudo access. Textmate will just ask for your password.

Try and download the 30 days trial for easy of this tutorial. http://macromates.com/

Trust me, you will love Textmate, it’s state of the art editor.

ps: I use “mate” instead of “open -a TextMate” but not everyone might have the mate symlink.

Requirements:

  • Snow Leopard 10.6.4 or greater
  • Xcode 3.2.3, 2.4.1 or greater
  • Admin privileges on your mac.

Software preliminary:

  • Mysql 5.1.49 (macports)
  • Ruby 1.8.7 (macports)
  • Apache 2.2.9 (part of OS X)
  • Rubygems (download)
  • passenger (gem)
  • ImageMagick
  • rmagick

1. Preparing System

If you are upgrading from Leopard to Snow Leopard, I recommend you delet your old installation.

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 \
    ~/.macports

1.1 Prepare PATH environment:

$ open -a TextMate ~/.bash_profile

Edit your ~/.bash_profile file and add these 2 line.
Check if they are not there already.

export PATH=/opt/local/bin:/opt/local/sbin:$PATH
export MANPATH=/opt/local/share/man:$MANPATH

1.2 Download and Install Xcode

http://developer.apple.com/technologies/xcode.html

1.3 Download Macports and install

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

Download Direct Link: http://distfiles.macports.org/MacPorts/MacPorts-1.9.2-10.6-SnowLeopard.dmg

You might use this guide for installing Macports:

Full Install Guide: http://guide.macports.org/#installing

1.3.1 Update macports

sudo port -v selfupdate

2. Mysql

2.1 Intall Mysql

sudo port -v install mysql5-server mysql5

2.2 Make mysql autoload on startup

sudo port load mysql5-server
sudo -u mysql mysql_install_db5

3. Memcached (Optional)

sudo port -v install memcached libmemcached

3.1 Autoload memcached on startup

sudo port load memcached

4. Ruby, Rubygems, Rails, other gems

4.1 Install Ruby from macports

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

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

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

4.3 Install rails, rake, rspec etc.

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

extra:

4.4 install mysql gem

sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/opt/local/lib/mysql5/bin/mysql_config

5. 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

6. Passenger

6.1 Install Passenger gem

sudo gem install passenger

6.2 Check Passenger path

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

6.3 Build passenger for apache

follow instructions on screen

sudo passenger-install-apache2-module

6.4 Enable Passenger on apache:

open -a TextMate /etc/apache2/extra/httpd-passenger.conf

Put this on that file, 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
NameVirtualHost *:80
 
# my rails app virtual host 1
<VirtualHost *:80>
    ServerName my-rails-app.local
    DocumentRoot "/Users/fred/rails/my-rails-app/public" # change this to match your folder
    RailsEnv "development"
   <Directory /Users/fred/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
    </Directory>
    # logs are optional, change this to match your folder
    CustomLog  "/Users/fred/rails/my-rails-app/log/access_log" combined
    ErrorLog   "/Users/fred/rails/my-rails-app/log/error_log"
</VirtualHost>
 
# my rails app virtual host 2
<VirtualHost *:80>
    ServerName myapp-xyz.local
    DocumentRoot "/Users/fred/rails/myapp-xyz/public"  # change this to match your folder
    RailsEnv "development"
    <Directory /Users/fred/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
    </Directory>
    CustomLog  "/Users/fred/rails/myapp-xyz/log/access_log" combined
    ErrorLog  "/Users/fred/rails/myapp-xyz/log/error_log"
</VirtualHost>

6.5 Enable Virtual host on apache:

open -a TextMate /etc/apache2/httpd.conf

Add this new line at the bottom:

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

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

open -a TextMate /etc/hosts

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

127.0.0.1 my-rails-app.local
127.0.0.1 myapp-xyz.local

I have more than 50 .local apps in there.

5 Start Apache

first let’s test apache configuration:

apachectl configtest

if you get “Syntax OK” then you are ready to start it

sudo apachectl start

If you want Apache to autostart when you boot your computer,
then enable Web Sharing from preferences.

7. Final

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

If it works, congratulations.

If didn’t work, let me know here in the comments.

Extras

Nokogiri

  sudo port -v install libxml2 libxslt
  sudo gem install nokogiri -- --with-xml2-include=/opt/local/include/libxml2 --with-xml2-lib=/opt/local/lib --with-xslt-dir=/opt/local

Sqlite

sudo port install sqlite3
sudo gem install sqlite3-ruby

9. Sphinx, thinking-sphinx, sphinxsearchlogic

sudo port install sqlite3
sudo gem install thinking-sphinx sphinxsearchlogic

Update:

From the comments, some people might want to enable php and your Sites folder.

To be able to enable php and others you will have to enable another default vhost with localhost as servername.

Edit the file /etc/apache2/httpd.conf to enable vhosts

# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

then open /private/etc/apache2/extra/httpd-vhosts.conf file,
delete or comment everything from there and add this block only:

#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
 
# Change /Users/fred/ to your appropriate login name
<VirtualHost *:80>
    ServerName localhost
    DocumentRoot /Users/fred/Sites/
    RewriteEngine On
    DefaultType text/html
    <Directory "/Users/fred/Sites">
        DefaultType text/html
        Options +ExecCGI FollowSymLinks Indexes
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

Links and Resources

http://rmagick.rubyforge.org/install-osx.html

http://wiki.github.com/tenderlove/nokogiri/what-to-do-if-libxml2-is-being-a-jerk

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

http://guide.macports.org/#installing

http://distfiles.macports.org/MacPorts/MacPorts-1.9.1-10.6-SnowLeopard.dmg

http://rob.by/2009/installing-mysql-via-macports-on-snow-leopard-for-ruby-development/

http://macromates.com/

http://github.com/joost/sphinxsearchlogic

http://www.sphinxsearch.com/

 Posted by at 3:36 pm

  16 Responses to “Install a Rails enviroment on Snow Leopard with Macports, Mysql and ModRails Passenger (updated)”

  1. I did everything here according to your instructions on Snow Leopard OSX 10.6.

    It worked great and I have my rails apps up and running locally.

    However, I seem to have several problems:

    1. I had a phpinfo.php in my htdocs root:

    http://localhost/phpinfo.php

    Routing Error

    No route matches “/phpinfo.php” with {:method=>:get}

    So, course, now it apears that the document route is the rails app rather than the apache2 htdocs.

    I have the same problem accessing phpmyadmin

    http://localhost/phpmyadmin/index.php?

    Routing Error

    No route matches “/phpmyadmin/index.php” with {:method=>:get}

    Same issue.

    Not a big deal but will need to add more information for clarifying these types of routing issues.

    Thanks looking forward to seeing more of your posts.

    Sanjaya Yogi

  2. Hi, thank you for you nice comment.

    Your error is happening probably because one of your rails apps is default host of your apache and you dont have any other default vhost.
    The default is actually the first rails vhost on the passenger file.

    To be able to enable php and others you will have to enable another default vhost with localhost as servername.

    This is what did to fix your problem:
    Edit the file /etc/apache2/httpd.conf to enable vhosts

    1
    2
    
    # Virtual hosts
    Include /private/etc/apache2/extra/httpd-vhosts.conf

    then open /private/etc/apache2/extra/httpd-vhosts.conf file,
    delete or comment everything from there and add this block ONLY:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    #
    # Use name-based virtual hosting.
    #
    NameVirtualHost *:80
     
    # Change /Users/fred/ to your appropriate login name
    <VirtualHost *:80>
        ServerName localhost
        DocumentRoot /Users/fred/Sites/
        RewriteEngine On
        DefaultType text/html
        <Directory "/Users/fred/Sites">
            DefaultType text/html
            Options +ExecCGI FollowSymLinks Indexes
            AllowOverride All
            Order allow,deny
            Allow from all
        </Directory>
    </VirtualHost>

    there you go, worked nice for me.

  3. [...] I’ve had it now for a little over a week, and yesterday finished installing my rails environment and downloading the databases I have been working with. (For setting up the Rails environment on Snow Leopard, I recommend. this guide.) [...]

  4. Everything went beautifully up to the point of going to Safari and entering
    http://jclis1.local

    It can’t find the server jclis1.local. I checked all logs and found no errors and the httpd deamon is running. I’ve had rails running on windows XP and Linux servers for the past two years.

    Any help would be greatly appreciated.

    Thanks

    John Lewis

  5. Please scratch my recent cry for help. My error_log is now registering errors I can pursue.

    John Lewis

  6. Perfect. Smooth installation on OSX 10.6.6!! Thanks a ton!

  7. After following your instructions for Snow Leopard 10.6, I can access localhost site but not either of two virtual host sites. When I attempt to connect to http://mysite.local I get “[client 127.0.0.1]Directory index forbidden by Options directive: /Users/myname/rails/mysite/public”

    I have rails and apache running on Windows XP and Linux (both development and production).

    Any help will be greatly appreciated.

    Johnl

  8. One added piece of info for my recent comments…..The Safari Screen advises “You do not have permission to access / on this server”

    johnl

  9. try the apache error log and what does it say? I think it’s /private/var/log/apache2/error_log

    Also, could be that your folder permissions are restrictive. i.e. they are read-only by you and apache might not be able to read it.
    try adding others readable?
    chmod 755 /Users/myname/ /Users/myname/rails /Users/myname/rails/mysite

  10. The apache error log errs on startup as follows:

    [Mon Jan 17 11:02:31 2011] [warn] Init: Session Cache is not configured [hint: SSLSessionCache]
    [Mon Jan 17 11:02:31 2011] [warn] NameVirtualHost *:80 has no VirtualHosts
    [Mon Jan 17 11:02:31 2011] [notice] Digest: generating secret for digest authentication …
    [Mon Jan 17 11:02:31 2011] [notice] Digest: done
    [Mon Jan 17 11:02:31 2011] [notice] Apache/2.2.15 (Unix) mod_ssl/2.2.15 OpenSSL/0.9.8l DAV/2 Phusion_Passenger/3.0.2 configured — resuming normal operations

    My rails error log gives the following when I attempt to access http://jclis1.local:
    [Mon Jan 17 11:16:33 2011] [error] [client 127.0.0.1] Directory index forbidden by Options directive: /Users/myname/rails/jclis1/public/

    Both logs repeated the same error after I did chmod 75 on files you recommended.

  11. Good Morning!

    I tried again to access http://myname.local and the apache2/error_log added a new line which would support your thesis that permissions could be a problem. The added line reads:

    ttpd: Could not reliably determine the server’s fully qualified domain name, using My-name-iMac.local for ServerName

    I can’t fine where it’s reading myname as My-name-iMac. It’s obvious it’s not reading the proper configuration file…..and I don’t know why.

    johnl

  12. What a maroon! I finally noticed the Admin account which was set up at the Apple store is set at ‘My app’ while my login and ‘who am i’ is ‘myapp’. The only account in /Users is the ‘myapp’. Where/why is the system reading the ‘My app’ account (wherever it is).

    johnl

  13. Since the only errors seemed to be registering on apache startup, I went after the one in my local error_log proclaiming “Directory index forbidden by Options directives” and changed the public directory directive in
    httpd-passenger.conf by adding “Indexes”.

    Now I have no errors in either the apache2/error_log or my local error_log. Unfortunately, when I now request http://myname.local, Safari comes back with:
    Safari can’t connect to the server.
    Safari can’t open the page “http://jclis1.local/” because Safari can’t connect to the server “myname.local”.

    Since I made the change regarding Indexes in the same file that contains the virtual host names, I’m at a loss to understand why it doesn’t recognize the virtual host name(s).

    johnl

  14. As Dr. Frankenstein said……IT’S ALIVE!

    After a shutdown and startup….it worked! Apparently, the addition of “Indexes” to the Options Directive was the fix needed.

    Sorry to have choked you site with so much babble. Your setup instructions for rails are truely awesome.

    Thanks again

    Johnl

  15. Thank you! This was the only way to get the Mysql Rails gem to install correctly on my new 2011 MacBook Air. I did not use the steps for Apache or Passenger because my dev environment doesn’t use that but everything worked like a charm. I spent at least a whole day wrestling with the gem install configuration and in the end I guess it was MacPorts that saved my ass – that and your super helpful guide.

    thanks!

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Switch to our mobile site