Set up a local server on OS X

There are a few ways of setting up a local server on OS X, but I think the best way is to do it manually without any applications like MAMP running. They are fantastic applications, but why run an application when the features are built into the operating system anyway? Follow these simple instructions to get a local server environment running on your Mac!
I’m a web designer and do some front-end development. I understand that Terminal is a scary environment, but it’s much easier than it looks! If you follow this tutorial step-by-step and don’t panic or take shortcuts, you’ll be fine!
Note: Make sure you follow all instructions carefully, making backups along the way. If you don’t make backups and something goes wrong, you may have to reinstall your operating system.
What you’ll need
- An intel-based Mac
- OS X 10.5 or above (I’m running Lion)
- The latest version of Xcode. This should be on the disk that came with your (Snow) Leopard machine. If you’re on Lion it’s available for free on the App Store – make sure this is downloaded and installed before steps 4 & 5.
Step 1: Enable localhost
Open your System Preferences, go to “Sharing” and make sure “Web Sharing” is checked. This allows you to use http://localhost/ on your system. All web files should be placed in /Library/WebServer/Documents/, what I tend to do is the following (nb: this will remove anything from the Sites folder in your home directory):
Open up Terminal (Applications > Utilities) and type:
cd ~ rm -r Sites ln -s /Library/WebServer/Documents/ Sites
The Sites folder in your home directory is now an alias for /Library/WebServer/Documents/ – easy access.
Next, you want to make sure you can easy add/remove files to the Documents folder. In terminal again, replacing {username} with your Mac username (the same name as your home directory), do the following:
cd /Library/WebServer/Documents
sudo chown -R {username}:_www .
sudo chmod -R g+rwx .
For example, I type in “sudo chown -R traxor:_www .”.
Step 2: Enable PHP & mod_rewrite
We’re going to be working with some core files here, so we should always backup the file before editing it. Open up Terminal and type:
cd /etc/apache2/ sudo cp httpd.conf httpd.conf~backup sudo nano httpd.conf
The “nano” text-editor will open up the httpd.conf file. Nano is just a normal text editor without any point-and-click functionality. For reference, ^ means ctrl.
Enable PHP5
Press ctrl-w and search for “#LoadModule php5_module”. Delete the # from the beginning of the line, so the entire line looks like this:
“LoadModule php5_module libexec/apache2/libphp5.so”
Hurrah, you’ve just enabled PHP5!
Enable Mod_Rewrite
Mod_Rewrite is what allows you to have pretty URLs in applications like WordPress. To enable it, press ctrl-w and search for “AllowOverride None”, replacing all instances with “AllowOverride All” – there are about 5. To repeat the same search phrase again in nano, press ctrl-w, then the up key. Repeat until you’re confident that all instances of “AllowOverride None” have been changed.
Now save the file by pressing ctrl-o then the return key. Close it by pressing ctrl-x.
Step 3: Configure PHP
Before configuring PHP we should make a backup of the php.ini file and then open it:
cd /etc sudo mv php.ini.default php.ini sudo cp php.ini php.ini~backup sudo chmod 666 php.ini sudo nano php.ini
The php.ini will open in nano, just some nice and easy searching again. Press ctrl-w and search for “;date.timezone =”, uncomment it by removing the semicolon and add your timezone. Mine looks like this:
date.timezone = Europe/London
In the same file we need to enable error display and short open tags. Press ctrl-w and search for “display_errors = Off”, changing it to “display_errors = On” then search for “short_open_tag = Off” and change to “short_open_tag = On”.
Now save (ctrl-o then return) and close (ctrl-x) nano.
Step 4: Installing Homebrew & MySQL
Homebrew is a package manager for OS X. It provides an easy way to install packages and all their dependencies through terminal – all using Git. Homebrew will allow us to install MySQL and other packages easily, all in terminal.
Install Homebrew
Homebrew is seriously easy to install. You can find installation instructions on the Homebrew GitHub page – it’s just a one-liner! Important note: you will need Xcode installed on your machine for some kegs to work correctly.
Once installed, you should install Git and wget. Installing them is as simple as typing the following into Terminal:
brew install git wget
Homebrew will do it’s work and install the kegs and all dependencies (though those two things don’t need anything else to run).
Install MySQL
Your next step is to install MySQL, following the same syntax as the previous installation, open Terminal and type in:
brew install mysql
MySQL has a few dependencies, so this may take a little while. Once the installation is complete, follow the instructions you see in Terminal to enable MySQL. The instructions look like this:

Don’t worry, as long as you follow them step-by-step it’ll be fine.
To manage your MySQL databases use Sequel Pro – it’s free and really easy to use. If you’re having troubles running MySQL, restart your machine.
Step 5: Restart Apache gracefully
Now you can save (ctrl-o, enter) and close (ctrl-x) the php.ini and restart apache:
sudo apachectl graceful
Fin! All sorted!
You’re sorted. Your local server should work fine. Try it out by adding a few PHP files to your Sites directory – it should work! A massive thanks to Sam Penny, a God when it comes to terminal; despite his constant confusion between BSD and Linux commands when jumping onto my Mac.
Warranty
There is no warranty for the instructions provided on this site, to the extent permitted by applicable law. Except when otherwise stated in writing the copyright holders and/or other parties provide the instructions “as is” without warranty of any kind, either expressed or implied. You assume the cost of all necessary servicing, repair or correction.
Tweet