This is a tutorial on how to setup Apache 2.2, PHP 5.3, and MySQL 5.5 on Mac OS X 10.6 Snow Leopard. For Leopard instructions read How to set up Apache, PHP, & MySQL on Mac OS X 10.5 Leopard.
Tomorrow, I’ll be starting my first day at BetterWorks and figured this tutorial would be a good reference for when I have to set up my development environment.
While I’m a big fan of nginx (pronounced: “engine x”) this tutorial uses Apache 2.2, which comes preinstalled on Snow Leopard.
Preparation
Please be warned that I’m assuming you have a clean and updated install of Snow Leopard, I doubt anything you do here can cause any serious damage but just be sure you have backups of everything important. If you screw up my directions there are copies of the Apache configuration files you’ll be editing in /etc/apache2/original so don’t despair.
Download and install Xcode 3.2.x (version 4 or later will not work on Snow Leopard). If you don’t want to wait for the latest version to download a copy of Xcode comes with every Mac OS X DVD in the Optional Installs folder.
Open up the Terminal application in /Applications/Utilities or if you prefer use iTerm.
I will be using the GNU nano text editor in my examples since it already comes with the Mac, but if you’d like to use TextMate or BBEdit just replace nano with mate or bbedit respectively and remove any nano specific options (e.g. +n or -w).
Whenever I ask you to type something into Terminal you have to hit return for the command to go through. Alternatively, for you lazy ones out there, you can just copy and paste these commands.
Setting up PHP
Enter the following into the command line/shell (Terminal):
sudo nano -w +115 /etc/apache2/httpd.conf
Whenever you put sudo in front of a command you will need to enter your root/administrator password when prompted to do so.
Your cursor will now be at the beginning of line 115, which should look like this:
#LoadModule php5_module libexec/apache2/libphp5.so
Uncomment that line by removing the pound/hash sign (#).
Before you close the httpd.conf file you should hit control w and search for:
DirectoryIndex index.html
Add index.php to the end of the line like so:
DirectoryIndex index.html index.php
When you are finished hit control x to exit, then type y, and hit return to save your changes.
To create php.ini type the following into Terminal:
sudo cp /etc/php.ini.default /etc/php.ini
Fix php.ini’s permissions:
sudo chmod 644 /etc/php.ini
You should configure php.ini to your liking, however I’d strongly recommend you turn error reporting on (I’m assuming you’re using this setup for development) and define a timezone.
Type this into shell:
sudo nano -w +514 /etc/php.ini
Change line 514:
error_reporting = E_ALL & ~E_DEPRECATED
To:
error_reporting = E_ALL | E_STRICT
Search (control w) for:
display_errors = Off
And replace with:
display_errors = On
Then search for:
html_errors = Off
And replace with:
html_errors = On
Then search for:
;date.timezone =
Uncomment (remove the leading semicolon) and then add your timezone (see: http://php.net/manual/en/timezones.php):
date.timezone = America/Los_Angeles
Close and save (unless you have any other changes to make of course).
In addition to these simple changes to php.ini I’d also suggest setting up Xdebug with webgrind for PHP performance profiling.
Setting up Apache
If you’re like me and use rewrite rules in an .htaccess file for those nice clean search engine-friendly URLs you’ll need to make sure mod_rewrite is working properly. For this you will need to know your computer’s short name, on this one it is ariadoss, so replace ariadoss with whatever yours happens to be, you can easily tell what it is from the Terminal cause it’s the name right before the dollar sign (e.g. Danilo-Stern-Sapads-MacBook-Pro:~ ariadoss$). If you still can’t figure it out it doesn’t matter since there is only one file in /etc/apache2/users, which should be shortname.conf.
Go to the Terminal and type in everything except the bold part unless you know what goes there, hit tab to autocomplete the missing bold part with your info before hitting return:
sudo nano -w /etc/apache2/users/ariadoss.conf
You should now see something like this on line 1:
<directory "/Users/ariadoss/Sites/"></directory>
Directly underneath line 1, you should change lines 2 and 3 to look like this:
Options All
AllowOverride All
Close and save.
We’re not exactly done yet but let’s go ahead and test that Apache and PHP are running.
Go to System Preferences, click on the Sharing icon, then select Web Sharing and make sure it’s checked, the light will turn green and it will say Web Sharing: On. Now open up your favorite web browser and go to:
http://localhost/
If you want to see the contents of your Sites directory go to: http://localhost/~ariadoss
Again, make sure you replace ariadoss with your short name.
Personally, I hate typing in ~ariadoss every time I need to test a site so I made some further changes to my httpd.conf file, which you probably only want to implement if there are no other users on your computer. We could setup virtual hosts to make short memorable URLs like http://ariadoss.loc/ instead of just pointing /Users/ariadoss/Sites to http://localhost but that’s outside the scope of this tutorial. However, if this interests you read my tutorial on How to setup virtual hosts on Mac OS X 10.5 (Leopard). Please note that this tutorial is written for Leopard and not Snow Leopard. To make it work on Snow Leopard change all occurrences of 461 to 465 when typing in the commands.
So here’s what you do, back to Terminal:
sudo nano -w /etc/apache2/httpd.conf
Once open, replace the two occurrences of /Library/WebServer/Documents (on lines 167 and 194) with the full path to your own Sites directory (e.g. /Users/ariadoss/Sites). To easily find the path to your particular directory open another Terminal window (command n) and enter:
cd ~/Sites
pwd
Once you finish finding and replacing all that jazz, restart Apache using the command below:
sudo apachectl restart
You’ll want to restart the Apache service either from System Preferences or the command line whenever you make a change to a configuration file.
To make sure everything is working go to your Sites directory and create a new PHP file called info.php that contains the following code:
<?php phpinfo(); ?>
Open the following URL in your preferred web browser:
http://localhost/info.php
Make sure everything is in order.
Installing MySQL
Download the latest version of MySQL DMG Archive for Mac OS X:
http://dev.mysql.com/downloads/mysql/
Mount the MySQL disk image (i.e. the .dmg archive).
Install mysql-5.5x-osx10.6-x86_64.pkg, MySQLStartupItem.pkg, and MySQL.prefPane, which are contained inside the disk image.
Eject the disk image.
Open up Terminal and enter:
sudo nano -w /etc/php.ini
Search (control w) for:
pdo_mysql.default_socket=/var/mysql/mysql.sock
And replace with:
pdo_mysql.default_socket=/tmp/mysql.sock
Then search for:
mysql.default_socket = /var/mysql/mysql.sock
And replace with:
mysql.default_socket = /tmp/mysql.sock
Then search for:
mysqli.default_socket = /var/mysql/mysql.sock
And replace with:
mysqli.default_socket = /tmp/mysql.sock
Save and close.
In Terminal type:
nano -w ~/.bash_login
Put this text at the top of the document to set the path to MySQL:
export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"
Save and close.
Then type:
. ~/.bash_login
Still in Terminal restart Apache:
sudo apachectl restart
Now from the MySQL preference pane in System Preferences click Start MySQL Server.
This last step isn’t necessary for a dev environment but if you want to easily secure MySQL execute this command in Terminal:
mysql_secure_installation
Congrats, you’ve successfully set up a MAMP stack!













About
