This post is super old! I would now recommend Homestead for all of your server needs, as it will always do a more reliable job than the below.
This article has been designed for a fresh install of Lion or Mountain Lion. If you’re not on either of those, you may find this doesn’t work.
There are a couple of easy ways to get a web server up and running on OS X, including the popular MAMP. However, they can cost money or are restrictive.
You may or may not know, but OS X has Apache and PHP already installed – it’s just a case of turning them on, installing MySQL and you’re there!
The process can be a little painful as there are can be one too many restarts required. Because this sort of thing is so hard to debug if you get something wrong, I’m going ask that you test certain things as you go along. Ignore these tests at your peril.
So, if you’re sitting comfortably, I shall begin.
First, you need a good egg
You’ll need some sort of Mac with OS X Mountain Lion. If you’ve tinkered with some of the files I reference before, do your best to put them back to how you found them.
Before we start fiddling, we’ll start downloading MySQL. It weighs in at over 100mb, so for the benefit of those not on super-fast broadband, I’d recommend downloading it now.
Visit mysql.com/downloads/mysql and choose Mac OS X ver. 10.6 (x86, 64-bit), DMG Archive. (If you’re using an older 32-bit Mac, swap 64-bit for 32-bit. If you’re not sure what type of processor you have, use How to tell if your Intel-based Mac has a 32-bit or 64-bit processor.)
If the scroll past the sign up box, you’ll find a link marked “» No thanks, just start my download!” and off it goes!
Say hello to Terminal
We’re going to be editing some system files. The easiest way to find them and edit them is through Terminal. If you’ve got an application setup such as TextMate or Sublime to use the command line, then of course use that, otherwise just do as I’ve written.
Open Terminal. You’ll find it in your Utilities directory, in Applications.
Great! Yours will look a little different to mine as you’ll have a different name. The first thing we’re going to do is fiddle with Apache. In my code examples, lines that start with a
$ indicate a line you type in. Other lines will be produced by the system.
Let’s navigate to the Apache directory.
$ cd /etc/apache2
That’ll take you to the Apache directory on your system. If you use
ls to list the files, you’ll see something similar to this.
$ ls extra magic httpd.conf mime.types httpd.conf.bak original httpd.conf.default other httpd_ACSServer.conf users httpd_devicemanagement.conf webapps httpd_podcastlibrary.conf
If you don’t, type in
pwd and press Enter. That’ll tell you which directory you’re in. Double check and try again.
First up, we’re going to edit the Apache configuration to enable PHP and virtual hosts.
$ sudo nano httpd.conf
I’ll break that down for you.
sudogives you authorisation to edit system files. When you press enter, it’ll ask you for your administrator password (the letters won’t appear as you type them).
nanois a simple text editor built into OS X and Terminal.
httpd.confis the name of the file we want to edit.
Hit Enter, and you’ll be presented with a text editor that’ll say “GNU nano” in the top left.
This file is quite long, so it may take a few minutes to find where we want to be. To do a page scroll down, use Control+V. If you do this about three times, you’ll find a section that starts like this:
# # Dynamic Shared Object (DSO) Support # # To be able to use the functionality of a module which was built as a DSO you # have to place corresponding `LoadModule' lines at this location so the # directives contained in it are actually available _before_ they are used. # Statically compiled modules (those listed by `httpd -l') do not need # to be loaded here. # # Example: # LoadModule foo_module modules/mod_foo.so #
There’ll be a whole load of
LoadModules after it, but skip through them until you see these three at the bottom:
#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
# comment at the beginning of
LoadModule php5_module – this enables PHP in Apache.
If you’re using nano to edit the file as above, press Control + O to save, then Control + X to exit nano.
We’re going to start Apache to see if our changes have been successful. Type
sudo apachectl start
Now let’s see if it’s worked. Visit http://localhost in your browser.
You should see a “It works!” message in your browser. That tells you that Apache is working. Let’s try PHP.
Back in Terminal, create and edit a new file called
$ sudo nano /Library/WebServer/Documents/info.php
You will have created a blank file. Insert this line
Then save (Control + O) and exit (Control + X). Visit http://localhost/info.php and you should see a phpinfo() page.