Last night I started getting back into coding but have since switched how I develop. All my code is now (finally) in a local Git repository which is fantastic, but the next step was to get a local server running so I could actually see the results of my work.
In the past, I’ve used Dropbox to sync the code between my workstations and to a Linux virtual machine which ran a web server. The problem with this setup is that when it failed, it failed miserably. The issue here was with Dropbox. The Linux client is quite crappy and is prone to failure very easily – at least the command line version. The version which integrates into Nautilus seemed a lot more stable.
So, I decided to go back to my old buddy, Mac OS X and use the server capabilities built-in. I’d check-in code and test using a local server. However, in Mountain Lion, Apple removed “Web Sharing” from the Sharing pane of Preferences. After a bit of searching though, it was obvious that although the GUI was removed, the services weren’t. Follow the directions below to enable an Apache + MySQL + PHP Stack on Mountain Lion that allows .htaccess file directives and has a few tweaks to php.ini.
1. Configure Apache
sudo vi /etc/apache2/httpd.conf
Find the php5_module and uncomment the line.
#LoadModule perl_module libexec/apache2/mod_perl.so
LoadModule php5_module libexec/apache2/libphp5.so
#LoadModule hfs_apple_module libexec/apache2/mod_hfs_apple.so
Next, I changed the default virtual domain to point to my local document root and set the User/Group as myself. I also enabled directives to allow .htaccess to work.
Options Indexes FollowSymLinks MultiViews
Allow from all
2. Configure PHP
Next up, we need to configure PHP to have a proper date timezone set. This reduces errors/warnings which could show up. We will also enable short_open_tags. Not necessary for most setups, but can be useful.
sudo cp /etc/php.ini.default /etc/php.ini
sudo vi /etc/php.ini
; Defines the default timezone used by the date functions
date.timezone = "America/New_York"
;short_open_tag = Off
short_open_tag = On
3. Install & Configure MySQL
The MySQL libraries are actually built-in to the PHP that ships with Mountain Lion. If you do not need any of the command line tools and will only connect to a remote server, you can skip to the next step.
4. Set Apache to run on boot
Run the following command to have Apache start at boot
sudo defaults write /System/Library/LaunchDaemons/org.apache.httpd Disabled -bool false
Congratulations! You’ve now configured Apache2, MySQL and PHP to play nicely together on OS X Mountain Lion in a more standardized way than the default Apple configs would normally allow.
Leave a comment if you have any questions or suggestions!