Blog / Setting up Mac for PHP and Ruby websites
Update: This guide should work on Mountain Lion with one exception: the Web Sharing box no longer appears in System Preferences > Sharing.
Want to get started with PHP, MySQL, and Ruby development using Apache? You can use a tool like XAMPP, but it’s possible to run these services natively on your Mac, just like a real web server.
This guide for OS X Lion assumes some familiarity with the command line. I recommend using iTerm2.
Apache, MySQL and PHP
Apache
Enable Apache by checking the Web Sharing box in System Preferences > Sharing
PHP
Edit the Apache configuration file. You’ll have to type your password to edit this file.
$ sudo nano /etc/apache2/httpd.conf
Use Ctrl-W and type libphp5 to find this line (below). Uncomment it by removing the pound sign (#) in front.
#LoadModule php5_module libexec/apache2/libphp5.so
Enable virtual hosts by using Ctrl-W to search for httpd-vhosts.conf and uncomment the line below.
#Include /private/etc/apache2/extra/httpd-vhosts.conf
Set the Apache process to run as your username so it has access to your files. This is kind of unsafe, but as long as you’re the only one working on your computer it should be fine.
Use Ctrl-W to search for _www and replace User with your username (AKA the one word name of your home folder) and Group with “staff”.
User JordanRoher
Group staff
Press Ctrl-X to exit, type Y to save the buffer, and press enter to keep the name of the file.
Finally, restart Apache. If you don’t see an error, it worked.
$ sudo apachectl restart
Virtual hosts
Virtual hosts let you use custom domains like www.mysite.local.
sudo nano /etc/apache2/extra/httpd-vhosts.conf
Delete the sample virtual hosts at the bottom of the file (tip: use Ctrl-K to erase an entire line). Here’s a sample virtual host I used for testing a Ruby on Rails site. If you’re not running a Ruby site, you don’t need the three Passenger/RailsEnv lines in the middle.
Replace the DocumentRoot and Directory folder with the path to your site.
<VirtualHost *:80>
DocumentRoot "/Users/JordanRoher/Dropbox/Projects/Not Clickable/Web/www/public"
ServerName notclickable.local
ServerAlias www.notclickable.local
PassengerUser JordanRoher
PassengerGroup staff
RailsEnv development
<Directory "/Users/JordanRoher/Dropbox/Projects/Not Clickable/Web/www/public">
AllowOverride All
Options -MultiViews
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Restart Apache again. You’ll need to do this every time you make a change to your virtual hosts file.
$ sudo apachectl restart
Hosts file
Edit the hosts file and add lines as necessary pointing to 127.0.0.1. Every time you make changes to the hosts file you need to close and reopen your browser.
$ sudo nano /etc/hosts
127.0.0.1 notclickable.local
127.0.0.1 www.notclickable.local
MySQL
Go to MySQL and download the 64-bit community server DMG installer. Yes, it’s supposed to be 100-something megabytes. Run the installer. You’ll want the startup item and preference pane too.
Note
At this point you’re done with hosting regular MySQL and PHP sites. Continue for Ruby on Rails directions.
Ruby on Rails
XCode
Install XCode from the Mac App Store and run it. Go to Preferences > Downloads and install the Command Line Tools. This will install Git, among other things.
Ruby
Although OS X comes with a version of Ruby, it’s somewhat out of date. We can work around the issue by installing RVM.
$ bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
Update your bash file to load RVM at startup.
$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" ### Load RVM function' >> ~/.bash_profile
Quit and re-launch your terminal app.
Install Ruby 1.9.3 and use the latest version of Ruby by default.
$ rvm install 1.9.3 --with-gcc=clang
$ rvm --default 1.9.3
Passenger
Passenger lets you run Ruby on Rails sites through Apache.
$ sudo gem install passenger
$ rvmsudo passenger-install-apache2-module
Follow its instructions to put lines at the end of your Apache config file. Your numbers will look different than mine.
$ sudo nano /etc/apache2/httpd.conf
LoadModule passenger_module /Library/Ruby/Gems/1.8/gems/passenger-3.0.11/ext/apache2/mod_passenger.so
PassengerRoot /Library/Ruby/Gems/1.8/gems/passenger-3.0.11
PassengerRuby /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
Restart Apache again.
$ sudo apachectl restart
Ruby on Rails
Finally you’re ready for Ruby on Rails itself.
$ sudo gem install rails
Jekyll
Even more optional: I use Jekyll to create this blog and my other sites.
$ rvmsudo gem install jekyll
$ rvmsudo gem install RedCloth
To ensure you’re always using UTF-8 character encoding (I’m looking at you, Coda 2), add these lines to your .bash_profile:
$ sudo nano ~/.bash_profile
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8