Mac OS X: Setting Up Apache, MySQL, PHP and Mod_Rewrite

Please note that I did not write this. This is reprinted from TextDrive written by SuperJared. The only thing that has been changed is formating, all else is exactly as the author written. This is here for my personal reference.

Step 1: Enable Apache.

Go to System Preferences > Sharing and enable Personal Web Sharing. Verify that Apache has started by opening a browser and pointing it to http://localhost/.

Step 2: Install MySQL.

This is almost as easy as enabling Apache since MySQL has excellent support for Mac OS X.

Go to http://dev.mysql.com/downloads/ and pick out the latest OS X flavored package. Make sure to get the right one for your architecture. Mount the dmg and you should see two installer packages: one for the MySQL database, and the other installs the MySQL startup item.

The easiest thing to do is just run the standard installer for the database. It should be called something like mysql-standard-VERSION-apple-ETC.pkg. After that is installed, install the file MySQL.prefPane by double-clicking it. This is a panel in the System Preferences that allows you to start, stop and configure the database for automatic start.

Your database is installed. Look into the MySQL documentation if you want to secure it, because right now, the user name is root and there is no password.

Step 3: Install PHP.

Go to http://www.php.net/downloads.php and download the latest source version. You’ll want to put this in your directory (that’s my preference, at least), so open up a terminal:

cd /usr/local/src/ sudo fetch http://path/to/the/latest/download/from/php.net/ sudo tar zxvf php-VERSION-ETC.tar.gz cd php-VERSION

At this point you’re in the PHP source directory. We’ll now configure our PHP. NOTE: This example has the bare minimum of options to get this all working properly. See the PHP documentation for more configure options.

sudo make sudo make install sudo cp php.ini-dist /usr/local/lib/php.ini

Make sure to edit to your desired settings. Now we need to edit httpd.conf:

sudo open -a TextEdit /etc/httpd/httpd.conf

And make sure these lines are in their proper locations:

LoadModule php5_module libexec/libphp5.so # Should have been added already AddModule mod_php5.c # Should have been added already AddType application/x-httpd-php .php

Now restart Apache:

sudo apachectl graceful

And verify that the install worked by creating a file called test.php under your Sites directory (/Users/myusername/Sites/) with the following text:

<?php phpinfo(); ?>

Go to http://localhost/~myusername/test.php to verify that it worked.

Step 4: Enable mod_rewrite. (Optional)

Enabling mod_rewrite requires additional modification of the httpd.conf (and related) files so we can use .htaccess files.

sudo open -a TextEdit /etc/httpd/httpd.conf

Under this line:

<Directory "/Library/WebServer/Documents">

You’ll see:

AllowOverride None

Change this to:

AllowOverride All

You’ll also want to modify your user’s configuration file:

sudo open -a TextEdit /etc/httpd/users/myusername.conf

The Options directive must include FollowSymLinks, and the AllowOverride should be AuthConfig All. Example:

<Directory "/Users/myusername/Sites/"> Options Indexes MultiViews FollowSymLinks AllowOverride AuthConfig All Order allow,deny Allow from all </Directory>

Restart Apache and you’ll be able to use .htaccess files.