So I've finally dropped MAMP and decided to run Apache locally on my Mac. This was definitely a learning experience for me as it was only the second time that I've set up a web server. So here are my findings compiled into a recipe for you to use.
Hardware and Software
- Mac OS X 10.6.8
- Apache 2.2.21
- MySQL 5.5.15
- Rails 3.2.1
- Ruby 1.9.3-p0
- Ruby Version Manager 1.10.2
- Homebrew 0.9
We don't have to worry about installing Apache, Mac OS X comes with it installed, so we can skip this step. The next order of business is to install homebrew for ease of installing packages for mac. So go to homebrew and install brew using this command
$ /usr/bin/ruby -e "$(/usr/bin/curl -fksSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"
Once brew is installed we need to install wget so we can install other packages
Now we can install MySQL via Homebrew
Once MySQL has finished installing you will presented with a bunch of important information to finish the installation of MySQL. If you forget something out of this information you can run this command to retrieve it.
The current MySQL Homebrew recipe installs verison 5.5.20, but installs to a directory at 5.5.15 instead. So brew --prefix mysql shows us a directory pointing to 5.5.20, knowing this will save you a small headache.
We know can set up our mysql database with the previously mentioned hiccup applied here
$ unset TMPDIR
$ mysql_install_db --verbose --user=`whoami` --basedir="$(/usr/local/var/mysql/5.5.15)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
MySQL is now installed! Lets start it up!
And to shut it down.
So far so good, lets get Ruby Version Manager installed now. (Assuming you are using bash, zshell is not really that different)
$ bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
$ source ~/.bash_profile
Now we have RVM, lets install ruby, rails, and passenger
$ rvm install 1.9.3-p0
$ rvm use 1.9.3 --default
$ rvm gemset create rails321
$ rvm gemset use rails321 --default
$ gem install rails 3.2.1 --no-rdoc --no-ri
$ gem install passenger --no-rdoc --no-ri
Now we'll install the passenger module
$ passenger-install-apache2-module
You'll get a lot of prompts, but just follow the instructions and we'll continue the passenger installation. Passenger will ask you to modify your httpd.conf file, in order to do so you have to have root access. So navigating to the apache install and edit the file like so. You'll probably have to enter your root password.
$ cd /private/etc/apache2
$ sudo cp httpd.conf httpd.conf.bak
$ sudo vi httpd.conf (or whatever your editor of chocie is)
Then insert these lines into the httpd.conf file
LoadModule passenger_module /Users/username/.rvm/gems/ruby-1.9.3-p0@rails321/gems/passenger-3.0.11/ext/apache2/mod_passenger.so
PassengerRoot /Users/username/.rvm/gems/ruby-1.9.3-p0@rails321/gems/passenger-3.0.11
PassengerRuby /Users/username/.rvm/wrappers/ruby-1.9.3-p0@rails321/ruby
username is your root username
Personally I like to create a folder in the apache2 directory called mods-enabled and a file in the mods-enabled directory passenger.conf, then include that file via the httpd.conf, but the current method will suffice.
Ok lets create a new rails app in your webservers directory, mine is located at /Users/andredublin/Sites, but apache2 may have you pointing to /Library/WebServer/Documents, depending on the httpd.conf file.
$ rails new test.app.com -d mysql
Now we have to create the databases for our rails app.
$ mysql.server start
$ mysql
$ mysql> CREATE DATABASE test.app.com_development;
$ mysql> CREATE DATABASE test.app.com_test;
$ mysql> CREATE DATABASE test.app.com_production;
$ mysql> exit
Now go back your the apache2 directory at /private/etc/apache2 and we'll create a new folder called sites-enabled and we create a file called test.app.com
$ sudo mkdir sites-enabled
$ cd sites-enabled
$ sudo touch test.app.com
Ok now lets open and edit the test.app.com file with the following
Make sure to point the DocumentRoot to the public folder in the rails app.
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin you@example.com
ServerName test.app.com
DocumentRoot /Users/username/Sites/test.app.com/public
RailsEnv development
<Directory /Users/username/Sites/test.app.com/public>
AllowOverride all
Options -MultiViews
</Directory>
ErrorLog /Users/username/Sites/test.app.com/log/error.log
CustomLog /Users/username/Sites/test.app.com/log/access.log combined
</VirtualHost>
Now we can start our apache server with sudo apachectl start and navigate to test.app.com and viola! You have a rails app running on apache with passenger, give yourself a pat on the back. You can create multiple virtual hosts this way and have multiple instances of rails in tandem. If you come across any problems and improvements please leave a comment below.