Luke Jones

A young & enthusiastic visual designer from Bristol, UK

Set up a local server on OS X

Posted on November 12, 2011

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.

  • http://daneden.me Dan Eden

    Nice run-down. I think my love of MAMP comes from the fact it comes with phpMyAdmin, which I use extensively when working with MySQL. I imagine it would be a pain to install manually, or to fiddle with the MySQL databases any other way.

    • http://www.lukejones.me/ Luke Jones

      Before doing this myself, I would’ve agreed with you. However (and it’s a HUGE however), Sequel Pro does exactly the same job as phpMyAdmin and is a much nicer interface… http://cl.ly/2e2k381Q221v383F1S2x

      • http://daneden.me Dan Eden

        Ah yes, I forgot about Sequel Pro. Might have to make the move – my MAMP install seems pretty buggy these days!

      • Anonymous

        There’s also SQL Buddy, which, like phpMyAdmin, is web based and has a gorgeous UI http://www.sqlbuddy.com/

  • http://iamkeir.com iamkeir

    What would you say the advantages are of this over MAMP?

    • http://www.lukejones.me/ Luke Jones

      The OS X operating system already has the core files required for you to run a WebServer on your Mac. The way I see it, there’s no point in running an application, wasting space on your hard-drive and using up additional memory when you can spend 30 minutes following a tutorial and never have to run an application again.

      • http://iamkeir.com iamkeir

        That’s fair enough – although, I would probably say the biggest advantage would be getting used to administrating a server environment (vhosts, etc.) using terminal rather than a GUI, which is very useful when working with stage/live servers. Otherwise, MAMP’s footprint is pretty small for the ease of use it provides.

        • http://www.lukejones.me/ Luke Jones

          I would say using Terminal rather than a GUI is a huge advantage. You have much more overall control and it helps you to better understand the file structure of your system and learn commands that will make people more efficient.

          For example, running `mkdir img js css ; touch index.php css/style.css css/content.css js/functions.js` takes about 10 seconds to type – it’d take me a while to do that with a mouse.

          Jumping from MAMP to this has made me far more comfortable with Terminal, so I don’t bother with any point-and-click software for Git and am comfortable SSH-ing onto a server and doing what I need to do to fix it… Plus, I’m happy setting up a VPS or cloud server from scratch all command-line. Easy peasy.

          • http://iamkeir.com iamkeir

            Definitely worth pimping this advantage in your article,  I reckon!

          • http://www.lukejones.me/ Luke Jones

            Perhaps I will! Initial priorities are to get the tutorial to version 1.0 :)

  • http://twitter.com/jimbomoss James Moss

    There’s no need to manually download and install the MySQL binaries; since you’re using homebrew you can just do `brew install mysql`

    • http://www.lukejones.me/ Luke Jones

      I’ve updated my blog post with a version number. Instructions on installing MySQL using Homebrew will be done when I’ve done it on my machine and am confident that it works just as well as the Homebrew install.

    • http://www.lukejones.me/ Luke Jones

      Thanks for your suggestion James, the article has been updated! :)

  • http://twitter.com/bit_byte_bit Mike Stephens

    Nice article but you might want to include a section on virtual hosts and updating the /etc/hosts file as I suspect a lot of people will have multiple projects they are working on…

    • http://www.lukejones.me/ Luke Jones

      That’s a good point, I’ve put it in the “Future updates” including a link to where you should do it.

  • http://twitter.com/alexolder Alex Older

    Great tutorial but the reason I still with MAMP is because of the ability to switch between versions of PHP easily.

    Drupal 6 for example has a number of issues running on 5.3 and needs to run on 5.2 as well as some legacy code I work on has a small number of issues with 5.3 too. 

    • http://www.lukejones.me/ Luke Jones

      That does seem like an extremely good reason to use MAMP. However, I think you can have a php.ini in each site directory, controlling what versions of PHP are being used etc – you can also do this in the .htaccess to force certain versions if they’re available. :)

      • http://twitter.com/alexolder Alex Older

        yes, in MAMP however you just click a radio button and click apply and its done.

  • http://twitter.com/ben_argo Ben Argo

    Thanks for this :) Most helpful.

    One thing I might point out, if anyone’s like me who’s using two machines (I’ve got an iMac and a Macbook Pro) and like to use Dropbox to sync between devices, then you can always change the document root in Apache.

    Just nano back into the /etc/apache2/httpd.conf file, search (ctrl+w) for “DocumentRoot” and change the path to something like “/Users/ben/Dropbox/Sites” Easy enough to do :)

    • http://www.lukejones.me/ Luke Jones

      Yeah that is a pretty way to do things. Thanks for the tip! :)

  • http://twitter.com/chrismj83 Chris Johnson

    I personally prefer the MAMP way… I use SquelPro to manage my databases. Plus it allows for quick and easy configuration of DynamicDNS so i am able to share with clients

    • http://www.lukejones.me/ Luke Jones

      Ah yeah some people have suggested that MAMP is great for DynamicDNS/VirtualHosts/multiple PHP version etc, happy that it works for them. It’s just not me at all I suppose.

  • Matt Shaw

    Great tutorial thanks!

    Having problems connecting to mySQL though. Using Sequel pro it appears to connect fine, but wordpress can never establish a connection. Any ideas?

    • http://www.lukejones.me/ Luke Jones

      Make sure you’re using the correct database connection details and that your database. What I usually do is this:

      1. Open Sequel Pro
      2. Connect to your local database server (127.0.0.1 + root + empty password)
      3. Press “Users” top right
      4. Add a user called “admin” with whatever password you’d like.
      5. Check ALL privileges
      6. Make sure you give each Schema the right permissions for that user: http://cl.ly/1j3C0B3o1j042I451P0s

      • Matt Shaw

        Thats great, thanks!

        I was using localhost instead of 127.0.0.1. Also hadn’t set Schema privileges.

  • Anonymous

    Personally, I avoid installing caching on a development environment.  (Usually APC over Memcache though).

    When developing you want to always load the files directly, and not worry if it’s loading from an old cache. 

    Many a time have I been puzzled over why something isn’t working to then find it’s because the cache hasn’t refreshed and it’s loading an old version.

    Blooming good write up though, Luke!

    • http://www.lukejones.me/ Luke Jones

      Yeah that’s an awesome point about caching, although it is good to check that caching is working.

  • Anonymous

    Just another point for those not comfortable using nano as the editor within terminal, to open your php.ini file in textedit on the mac, use this : 

    sudo open -a TextEdit php.ini

    Works a charm….