Apache, MySQL, PHP development environment setup on Mac OSX 10.6 Snow Leopard
Sep 07 2009
As some of you may have heard, Snow Leopard was released a week and a half ago. I pre-ordered my copy from Apple, but didn’t get round to upgrading until the following Monday. First impressions were great: faster boot up, sleep and shutdown times, Time Machine backed up quicker, and the extra 7GB back on my HDD was also welcome.
Then I checked my local development environment. Screwed!
As it turns out, the upgrade had reset all my Apache and PHP config files as well as any Virtual Hosts I had set up. These would need to be reapplied. It wasn’t that simple, however, as I did encounter one or two other problems as well.
As SL ships with Apache and PHP already installed the majority of the work will be fairly simple config changes along with a few other small bits-and-bobs.
I use TextMate, so I’ll be using the mate command to open files when in the Terminal. Of course, you can use whatever you like.
vhosts.conf setup
I started by opening up the vhosts.conf file. In the Terminal I entered:
mate /private/etc/apache2/extra/httpd-vhosts.conf
I create separate VirtualHost entries for each site I have in my development environment. To do this I commented out the example and added the following lines:
<VirtualHost *:80>
ServerName metagraphical.dev
DocumentRoot "/Users/Matt/Sites/dev/"
</VirtualHost>
Remember, of course, to substitute “metagraphical” and “Matt” for your own details.
The next chunk of code is also required to prevent the “403 Forbidden” appearing when trying to navigate to your vhosts entries:
<Directory /Users/*/Sites/>
Options Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
I then saved the file and entered the required password.
Next I edited my hosts file with the new reference to the local site. In the Terminal:
mate /private/etc/hosts
Once the file had opened I added the following line:
127.0.0.1 metagraphical.dev
That was all, so I could save the file and close it.
Apache
With that done, I could move on to the Apache configuration.
Snow Leopard comes with Apache 2 already installed, so no faffing about downloading and installing it. Back in the Terminal I opened up the Apache httpd.conf file:
mate /private/etc/apache2/httpd.conf
I uncommented this line to apply all the VirtualHosts settings I’d just made:
# Include /private/etc/apache2/extra/httpd-vhosts.conf
And also this line to get PHP up and running:
# LoadModule php5_module libexec/apache2/libphp5.so
Bam! Done!
To test this I created a new index.php at the location I specified in my vhosts.conf and inside it added the <?php phpinfo(); ?> function and then saved the file. Before I could check it though, Apache needed to be turned on: System Preferences > Sharing, click the Web Sharing checkbox to fire it up. Now when I went to http://metagraphical.dev/ I could see the details of my current PHP setup.
PHP
If you scroll down when viewing your PHP setup page you’ll notice a huge warning chatting on about date and timezone issues. I only found out about this after I installed Wordpress unfortunately, and ended up doing a good bit of Googling to find the answer. The fix is pretty simple though.
I created the php.ini in order to begin making changes to the PHP configuration. In the Terminal:
cd /private/etc cp php.ini.default php.ini sudo chmod 777 php.ini mate php.ini
Once open, I went and found the line ;date.timezone =, uncommented it and added "Europe/London". A full list of supported timezones can be found on php.net.
The next problem I also found when I came to setup Wordpress. The problem was that PHP couldn’t successfully connect to my MySQL database, and it only occurred when I created the new php.ini. Back to Google, once again, where I found that the problem was down to the default port for MySQL not being set. I did a search for the line mysql.default_port = and set the port number to 3306.
That done, I saved the file and went back to the Terminal where I changed it’s permissions back:
sudo chmod 444 php.ini
MySQL
It is recommended that you upgrade to the 64 bit version of MySQL. You can download it from the MySQL downloads page. You might want to make sure you backup any databases and database settings you have created before doing this.
I also installed the MySQL Pref Pane and the Startup Item that are bundled in with the disc image. The Pref Pane is still the 32 bit version, however, so when you click it you will be told that System Preferences must quit and reopen. It will then reopen in 32 bit mode. Guess we’ll just have to wait for an update.
Finally, I downloaded and installed GUI Tools for managing my databases, which I prefer over phpMyAdmin.
That was it. All that was left to do now was to install WordPress, which is always a piece of cake.
Hope this helped.
September 9th, 2009 at 9:22 am
Great article! If I ever get a mac, I’ll be sure to use it
September 14th, 2009 at 7:39 pm
Fantastic documentation, worked first time, every step of these instructions was spot on!
September 20th, 2009 at 7:41 am
Great article.
I updated osx and had the same problem.
This article really saved the day!
September 20th, 2009 at 10:19 am
@Jasper Thanks man, glad it helped.
March 12th, 2010 at 7:50 am
Thank you, thank you, thank you.
Googled around and couldn’t seem to find the *right* article… finally found this and BINGO!
May 5th, 2010 at 10:35 am
Thanks so much!
Best info I found, after countless lost hours and recurring disappointment.
My only issue after reading this was that I had previously set-up a virtual host name on my Sites folder e.g. http://macbook/subsite.com.
I just had to define ‘macbook’ as a virtual host first inside httpd-vhosts.conf (as you’ve shown above) before defining other ’subsites’ separately.
My big win for the week!
December 7th, 2010 at 5:50 pm
Well.. got a mac and now going through this
Found this helpful to view hidden files in the finder.
Terminal command:
defaults write com.apple.Finder AppleShowAllFiles YES
Then restart finder:
control-option click finder icon in dock > relaunch