Apache, PHP, MySQL on Mac OS X

February 21, 2009

I started using MAMP a couple years ago to manage all my local web development needs. It comes in an easy install package and gives you Apache, MySQL, PHP, and more. It uses a dashboard widget to turn the servers on and off. It’s a great solution, though I have three primary gripes with it:

  • Turning the servers on and off is buggy. Sometimes they fail to shut off when you restart your computer and the widget says they’re on when really they’re not. I’ve gone the rounds on the troubleshooting for this, and I have a decent solution but it takes a couple minutes each time I do it.
  • The port numbers in the URL are lame: http://localhost:8888. Sure, you can use them to create virtual hosts (make Site A show up by typing localhost:5555, for example) but they are just noise. Why does that matter? I demo websites regularly, and I want the URL to look clean and organized. I guess I’m a picky designer.
  • Along with my last point, I hate the idea of installing something extra when my operating system already comes with a solution. Using MAMP is a waste of the Sites folder and the Web Sharing feature on my Mac.

By using the built-in Mac OS X server, you use resources that are already available and are already on by default. It works great.

There are instructions all over the web about how to get the built-in server up and running on Mac OS X Leopard, but each phase of the process is on a separate site, and in some cases the documentation is too wordy and unclear. I’m going to attempt to give the instructions here in a short and simple fashion.

Step 1: Turn on Apache

Turn turn the Apache server on, simply go to System Preferences then go to Sharing.

System Prefs and Sharing

Web SharingCheck the Web Sharing box in the list and shazam. Apache is on. You’ll notice in the same window that you can set what your computer name is. If you pick bob as your name, then in your web browser you would use bob.local to view your document root, which is the Sites folder found just under your home folder. (As you can see, I consider the Sites folder to be pretty important, so I dress it up from the default icon.)Sites This window also tells you the IP address that other computers are use to see the contents of your Sites folder in a web browser. As long as you leave Web Sharing checked, your server is running. That means it starts and stops as you log in and out. It’s fully automatic.

Step 2: Turn on PHP

Mac OS X Leopard also comes with PHP 5.x so you just have to enable it. By default, it’s off (don’t ask me why). You’re going to need to edit the httpd.conf file for your Apache server in order to turn it on.

Go to FolderBy default, you can’t see the httpd.conf file in Finder. It’s a hidden system file. To get to it, open Finder, go to up to Go and select Go to Folder… (or hit Command + Shift + G). Enter /private/etc/apache2 in the field and hit Go.httpd.conf permissions There you’ll find httpd.conf. In order to edit the file, you will likely have to edit its permissions. Just Get Info on the file, click the little lock to unlock it (administrator password required) and give your user Read & Write permission. Then open it with your code editor of choice. On Line 114 you’ll see this:

#LoadModule php5_module libexec/apache2/libphp5.so

This is the function that fires up PHP. It’ll be commented out with a # so just delete that and you’re ready to go:

LoadModule php5_module libexec/apache2/libphp5.so

PHP is up and ready to go. You won’t have to do anything else. It’ll always be available. If you’re experienced with httpd.conf files, this is the one where you do all your magic for your OS X Apache server.

Step 3: Install MySQL

Go to the MySQL download page and find the one that says Mac OS X (package format). Download it. Once you have it, double-click the .dmg file to mount it. You’ll see two installer packages:

MySQL Installer Packages
Double-click the mysql-5.x.x-osx10.x-x86.pkg one first. Go through the installation. If you get a “cannot install this software” error, just go back then click continue and you’ll be okay. It’s just a bug in the installer. Once that installation completes, double-click the MySQLStartupItem.pkg. Complete that installation. This one makes it so MySQL starts automatically when you log in to your computer).

The last thing to do is double-click MySQL.prefPane. This will install a little widget in your System Preferences that will allow you to turn MySQL on and off with a checkbox.

QueriousThe last thing to do is interface with your database. If you don’t want to deal with the command-line, and you feel like puking every time you look at phpMyAdmin, then you can get a thick client that will let you manipulate your database. To do just that, I highly recommend Querious. It’s a great way to manage both local and remote databases, and it’s pretty cheap. Once you get it installed, connect to your local MySQL stuff by using localhost with the user root:
Sequel Pro Connection
You can then create and modify all the databases you want. Your mom will be so proud.

A couple other options for a thick client database manager are Sequel Pro and Navicat for MySQL. In my opinion, the UI on these apps is inferior to that of Querious, but they may possess power that you need.

Extra Credit: Sweet Virtual Hosts

Wow, the nerd factor on this blog post is really getting high, but there’s one more thing I have to tell you about: virtual hosts. Patrick Gibson provides a handy little script and handy little instructions on how to make it so you can give unique names for the folders in your Sites directory. So if you have a family blog in there, you can create a virtual host called “family” and when you go to your browser, you just point it to http://family and it references that folder. It’s great. Check it out.

Lots of thanks to Wade and Rob for helping me learn this stuff.
Happy local server-ing!