Working with PHP and MySQL

From Mac Guides

Jump to: navigation, search

Mac OS X has a built in Apache web server, and so it is possible to install PHP and MySQL and have them function locally. These actions require that you have your webserver configured.



Check to see if it's already configured

PHP is likely installed, just not configured. Create a document using an appropriate editor (ZDE, TextMate or even TextEdit,) with this content:

 <? phpinfo(); ?>

Save this file into your Sites directory:


Using a web browser, navigate to this URL (replacing your machine name and username with your own). Personal Web Sharing needs to be enabled, if it is (The Sharing preference pane will reveal your local address, near the bottom. Just attach the filename to it)

You could either use your machine's name (here 'mark.local' is your machine's name - you could also enter the ip address instead)


or use the easier synonym 'localhost' to refer to your machine like this


If you see the source code you entered above, you need to enable PHP. If you see loads of PHP info, then PHP is already configured.

Configuring Apache to use PHP

(This is written specifically for PHP v4.x. PHP 5 is likely similar)

  • Open the Terminal
  • To open Apache's configuration file in the pico text editor…
  sudo pico /etc/httpd/httpd.conf
  • Find occurrences of "php4". Two lines need to be uncommented, to get Apache itself to know how to execute PHP scripts (which means removing the # from the start of the line). Find and uncomment the following lines (both of which are about 1/4th the way down):
 LoadModule php4_module libexec/httpd/ 
 AddModule mod_php4.c
  • Now you have to instruct Apache that *.php files are PHP files. Find the AddType section, about 3/4th the way down, and make it look like this:
   # AddType allows you to tweak mime.types without actually editing it, or to
   # make certain files to be certain types.
   AddType application/x-tar .tgz
   AddType application/x-httpd-php .php 
   AddType application/x-httpd-php-source .phps
  • Save the file.
    • You would have to assign write permissions to the /etc/httpd/httpd.conf file since it's read-only by default, so temporarily grant them. Type this in the terminal:
  sudo chmod 666 /etc/httpd/httpd.conf

Once you've saved the file, change the permissions back to the default, for security reasons, using

  sudo chmod 644 /etc/httpd/httpd.conf
  • Restart Apache to take effect. Open the Sharing preference pane.
    • Under the Services tab, with Personal Web Sharing selected, click the Stop button.
    • After a few moments, click the Start button.
  • Now, revisit the URL, and it should look much less like source code (you should actually see a big web page that shows "hello world" followed by a lot of configuration information about PHP/Apache).


1. Go to the MySQL downloads page in the Mac OS X section. You will want to download the appropriate package for your platform.

2. Once the DMG is downloaded, install both packages as well as the Preference Pane.

3. Add the MySQL binary path to your $PATH in (as of 5.0.27 this is /usr/local/mysql/bin).

touch ~/.profile
echo "export PATH=$PATH:/usr/local/mysql/bin" >> ~/.profile
source ~/.profile

4. Now that the MySQL tools are in your $PATH, set your MySQL root password.

 sudo mysqladmin password 'INSERTPASSHERE'

At this point, MySQL is installed and ready to be used.