Setting up a local web server on OS X Snow Leopard 10.6

Basic Web Server

MAMP is great. It’s an easy way to host your PHP/MySQL site locally for test­ing and devel­op­ment. But, if you want to take advan­tage of Snow Leopard’s built in web server capa­bil­i­ties, OS X makes it easy. Go into Sys­tem Pref­er­ences > Shar­ing, and check the “Web Shar­ing” box.

That’s it. You can now setup an HTML-only web­site locally. Where do you put the files? The default folder is /Library/WebServer/Documents/. While we’re at it, let’s setup PHP and MySQL to work locally, as well.

Test it. http://localhost. No need for a port as it will use apache’s default port 80.

PHP and MySQL will now need to be setup for a full func­tion­ing local host.

Set­ting up PHP

Open up Ter­mi­nal (Applications->Utilities->Terminal) and type:

sudo nano /etc/apache2/httpd.conf

Go to this line

#LoadModule php5_module        libexec/apache2/libphp5.so

and uncom­ment the line by remov­ing the hash character

LoadModule php5_module        libexec/apache2/libphp5.so

After sav­ing the file (Control+O) and exit­ing (Control+X). Restart Apache.

sudo apachectl restart

Con­fig­ur­ing PHP

cd /etc
sudo cp php.ini.default php.ini
sudo chmod 666 php.ini
sudo nano php.ini

Find this line and remove the semi-colon

;date.timezone = 

and add your own time­zone. A list of time­zones can be found here (http://php.net/manual/en/timezones.php)

date.timezone = America/Halifax

You’re halfway there. PHP 5 is now run­ning on your local server.

Set­ting up MySQL

Down­load the MySQL pack­age for Mac OS X.5 (32 or 64 bits depend­ing on your machine)
Install every­thing in the pack­age in this order:

  • mysql
  • the startup item
  • the pref­er­ence pane.

Now, open /etc/php.ini, find these three lines

pdo_mysql.default_socket=/var/mysql/mysql.sock
mysql.default_socket = /var/mysql/mysql.sock
mysqli.default_socket = /var/mysql/mysql.sock

and replace /var/mysql/ with /tmp/

pdo_mysql.default_socket=/tmp/mysql.sock
mysql.default_socket = /tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock

After installed, you can start MySQL in the sys­tem pref­er­ences. To access your data­base from the Terminal

/usr/local/mysql/bin/mysql -u root

Change your Home Directory

Want your local server’s files to be located some­where other than the default /Library/WebServer/Documents/? Open /etc/apache2/httpd.conf, and change the next two lines to reflect your new home directory.

DocumentRoot "/Library/WebServer/Documents"
...
<Directory "/Library/WebServer/Documents">

Dru­pal Problems?

If you’re get­ting errors dur­ing Dru­pal instal­la­tion try link­ing the MySQL sock file like below

cd /
sudo mkdir /var/mysql
sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

Drupal’s Clean Urls

Hav­ing prob­lems enabling Clean Urls? Find all instances of the fol­low­ing line in /etc/apache2/httpd.conf

AllowOverride None

and change None to All

AllowOverride All

18 Responses to Setting up a local web server on OS X Snow Leopard 10.6

  1. Nice and to the point of the matter!

  2. Awe­some tuto­r­ial. I was using MAMP and it just doesn’t work how I’d like it to. Now my iMac feels even more hawt.

  3. Luke Sent me over here via twit­ter. Got my MBP today and this filled a seri­ous gap. Thanks a lot

  4. Apt, use­ful & con­cisely pre­cise.
    Thanks a lot!

  5. Thanks — exactly what I needed to get up and run­ning with some local dev.

    You the man!

  6. I really appre­ci­ate this post but I’m lost after installing mysql! How do I open /etc/php.ini?

    • Open­ing php.ini is in the Con­fig­ur­ing PHP sec­tion, the first and last lines will open it up.

      I ended up get­ting a PDO error, couldn’t con­nect to the file, if this hap­pens, just restart apache by unclick­ing and reclick­ing the ‘Web Shar­ing’ box in the first step.

  7. Great tuto­r­ial — clear and to the point. Thanks!

  8. I just fol­lowed these instruc­tions on OS X Lion and it works, the prob­lem is I can­not seem to find the web­page which is dis­played and which sim­ply says it works! I looked in the /Library/WebServer/Documents direc­tory and there is no file there.

  9. Thanks, man. It is still use­ful for me even I just want prac­tice writ­ing html+css and js

  10. Can any­one sug­gest why I can open http://192.168.1.102/~JS/

    if I click the link in Sys­tem Preferences/Sharing/Web Sharing

    but once I am in the browser, if I try a refresh on that page I get Fire­fox can’t estab­lish a con­nec­tion to the server at 192.168.1.102″

  11. Fix for my above problem:

    Edit httpd.conf

    # OLD —Doc­u­men­t­Root “/Library/WebServer/Documents”

    Doc­u­men­t­Root “/Users/JS/Sites”

  12. This is a nice piece you have going here…thanks..
    Do you think you could add a bit on con­fig­ur­ing for phpmyAdmin?

  13. Thanks so much — top article

Trackbacks/Pingbacks

  1. Sasha – Mac OS X 10.6 Snow Leopard Localhost Environment Setup

Leave a Reply

Web Design Ideas and Resources