Article
Build Your Own Database Driven Web Site Using PHP & MySQL, Part 1: Installation
Mac OS X Installation
Mac OS X distinguishes itself by being the only consumer OS to install both Apache and PHP as components of every standard installation. That said, these take a few tweaks to switch on, and you’ll need to install the MySQL database as well.
In this section, I’ll show you how to start running a PHP-and-MySQL-equipped web server on a Mac computer running Mac OS X version 10.5 (Leopard). If you’re using an alternative to a Mac, you can safely skip this section.
All-in-one Installation
I normally recommend that you install and set up your web server, PHP, and MySQL individually, using the official installation packages for each. This process is especially useful for beginners, because it gives you a strong sense of how these pieces all fit together. If you’re in a rush, however, or if you need to set up a temporary development environment to use just for a day or two, a quick-and-dirty solution may be preferable.
You can skip ahead to the section called “Installing Individual Packages” if you want to take the time to install each piece of the puzzle separately.
MAMP (which stands for Mac, Apache, MySQL, and PHP) is a free, all-in-one program that includes built-in copies of recent versions of the Apache web server, PHP, and MySQL. Let me take you through the process of installing it:
- Download the latest version from the MAMP web site. After downloading the file (as of this writing, MAMP 1.7.2 is about 130MB in size), double-click it to unzip the disk image (
MAMP_1.7.2.dmg), then double-click the disk image to mount it, as shown below.
- As instructed in the disk image window, drag the MAMP folder icon over to the Applications folder icon to install MAMP on your system. After the copy operation has completed, you can drag the MAMP icon on your desktop to the Trash icon on your dock to eject it (it will turn into an Eject icon), then delete the disk image, as well as the original
.zipfile you downloaded.
Browse to your Applications folder and find the new MAMP folder there. Open it, and double-click the MAMP icon inside to launch MAMP. As MAMP starts up, the following will happen. First, the MAMP window shown below.

The two status indicators will switch from red to green as the built-in Apache and MySQL servers start up. Next, MAMP will open your default web browser and load the MAMP welcome page, shown below.

When you’re done working with MAMP, you can shut it down (along with its built-in servers) by clicking the Quit button in the MAMP window. When you’re next ready to do some work on a database driven web site, just fire it up again!
Later in this book, you’ll need to use some of the programs that come with the MySQL server built into MAMP. To work properly, these programs must be added to your Mac OS X system path.
To add the MySQL command prompt programs that come with MAMP to your Mac OS X system path, follow these instructions:
- Open a Terminal window. (To open a Terminal window, launch the Terminal application, which you can find in the Utilities folder in the Applications folder.)
If you’re running Mac OS X 10.5 (Leopard) or later, type these commands:
Machine:~ user$ sudo su
Password: (type your password)
sh-3.2# echo '/Applications/MAMP/Library/bin' >> /etc/paths.d/MAMP
sh-3.2# exitWhat to Type
The
Machine:~ user$portion (whereMachineis your computer’s name) represents the prompt that’s already displayed. You only need to type the command.If you’re running Mac OS X 10.4 (Tiger) or earlier, type these commands:
Machine:~ user$ touch .profile
Machine:~ user$ open .profileThis should open the hidden .profile file in TextEdit. This file contains a list of Terminal commands that are executed automatically whenever you open a new Terminal window. If you’ve never installed command prompt programs on your system before, this file will be completely empty. In any case, add this line to the end of the file:
export PATH=$PATH:/Applications/MAMP/Library/binSave your changes, and quit TextEdit.
- Close the Terminal window to allow this change to take effect.
Installing Individual Packages
Installing each individual package separately is really the way to go if you can afford to take the time. You gain the opportunity to learn how all the pieces fit together, and you have the freedom to update each of the packages independently of the others. Besides, it’s always worthwhile being familiar with the inner workings of any software with which you’ll be spending a lot of time.
The following instructions assume you’re running Mac OS X 10.5 (Leopard) or later. If you’re running an earlier version of Mac OS OX, you should stick with the all-in-one option.
Installing MySQL
Apple maintains a fairly comprehensive guide to installing MySQL on Mac OS X on its Mac OS X Internet Developer site if you want to compile MySQL yourself. It’s much easier, however, to obtain the precompiled binary version directly from the MySQL web site.
Start by visiting the The MySQL Downloads page
. Click the Download link for the free MySQL Community Server. This will take you to a page with a long list of download links for the current recommended version of MySQL (as of this writing, it’s MySQL 5.1).
Click the Mac OS X (package format) link. You will be presented with the list of downloads shown in the figure below. Which one you need to choose depends on your operating system version and platform architecture. If your system is running Mac OS X version 10.5 (Leopard), you can ignore the Mac OS X 10.4 links. If you know your Mac has a 64-bit processor, you can safely pick the Mac OS X 10.5 (x86_64) version. If you’re at all unsure, your best bet is the Mac OS X 10.5 (x86) version—all it requires is that you have an Intel-based Mac (to be sure, check the processor information in the About This Mac window, which you can access from the Apple menu). If you have an older, PowerPC-based Mac, you’ll need one of the PowerPC versions. The 32-bit version is the safe bet, since it will run on 64-bit systems too.
![]()
Once you’ve downloaded the mysql-version-osxversion-platform.dmg file, double-click it to mount the disk image. As shown below, it contains the installer in .pkg format, as well as a MySQLStartupItem.pkg file. Double-click the installer, which will guide you through the installation of MySQL.

Once MySQL is installed, you can launch the MySQL server. Open a Terminal window (To open a Terminal window, launch the Terminal application, which you can find in the Utilities folder in the Applications folder) and type this command:
Machine:~ user$ sudo /usr/local/mysql/bin/mysqld_safe
What to Type
The Machine:~ user$ portion (where Machine is your computer’s name) represents the prompt that’s already displayed. You only need to type the command, which is shown in bold.
Once you have typed the command, hit Enter.
This command runs the mysqld_safe script with administrator privileges. You’ll be prompted to input your password to do this, then a status message will confirm that MySQL is running.
Once MySQL is running, you can switch it to background execution by typing Ctrl+Z to stop the process, and then typing this command to let it continue running in the background:
Machine:~ user$ bg
You can then quit the Terminal application and MySQL will continue to run as a server on your system. When you want to shut down the MySQL server, open a new Terminal window and type this command:
Machine:~ user$ sudo /usr/local/mysql/bin/mysqladmin shutdown
Though you’ll gain plenty of geek cred for memorizing these commands, there’s a much less tedious way to control your MySQL server. Back in the installation disk image shown previously, you’ll notice a file named MySQL.prefPane. Double-click this to install a new pane in Mac OS X’s System Preferences, and the window shown below will open.
![]()
This window will tell you if your MySQL server is running or not, and lets you start it up and shut it down with the click of a button!
Presumably, you’ll want your system to launch the MySQL server at startup automatically so that you can avoid having to repeat the above process whenever you restart your system. The system preferences pane has a checkbox that does this, but for this checkbox to do anything you must first install the MySQLStartupItem.pkg from the installation disk image.
When you have everything set up the way you want it, you can safely drag the MySQL installation disk icon on your desktop to the trash, then delete the .dmg file you downloaded.
One last task you’ll want to do is add the /usr/local/mysql/bin directory to your system path. Doing this enables you to run programs like mysqladmin and mysql (for which we’ll have plenty of use later in this book) in the Terminal without typing out their full paths. Pop open a new Terminal window and type these commands:
Machine:~ user$ sudo su
Password: (type your password)
sh-3.2# echo '/usr/local/mysql/bin' >> /etc/paths.d/mysql
sh-3.2# exit
Close the Terminal window and open a new one to allow this change to take effect. Then, with your MySQL server running, try running the mysqladmin program from your home directory:
Machine:~ user$ mysqladmin status
If everything worked the way it’s supposed to, you should see a brief list of statistics about your MySQL server.
Installing PHP
Mac OS X 10.5 (Leopard) comes with Apache 2.2 and PHP 5 built right in! All you need to do to use them for development is switch them on:
- Open System Preferences (System Preferences… on the Apple menu).
- In the main System Preferences menu, click Sharing under Internet & Network.
- Make sure that Web Sharing is checked, as shown below.

- Quit System Preferences.
- Open your browser, type
http://localhostinto the address bar, and hit Enter. Your browser should display the standard Apache welcome message shown below.
With this procedure complete, Apache will be run at startup automatically on your system. You’re now ready to enhance this server by enabling PHP support:
- In the Finder menu bar, choose Go > Go to folder (?+?+G), and type
/private/etc/apache2/before clicking Go. - In the Finder window that opens, there should be a file named
httpd.conf. This is the Apache configuration file. By default, it’s read-only. Right-click the file and choose Get Info (?+I) to open the file’s properties. Scroll down to the bottom of thehttpd.confInfo window to find the Sharing & Permissions setting.By default, the settings in this section are disabled. Click the little lock icon shown in the figure below to enable them. Enter your password when prompted.

To make this file editable, change the value in the Privilege column for everyone to Read & Write, as shown in below.

- Back in the Finder window for the apache2 folder, right-click in the background of the folder window and choose Get Info to open the folder’s properties. As in the previous step, set the Sharing & Permissions settings from everyone to Read & Write.
- Finally, double-click the
httpd.conffile to open it in TextEdit. - In the
httpd.conffile, search for this line:#LoadModule php5_module libexec/apache2/libphp5.soEnable this command by deleting the hash (#) character at the start of the line.
- Save your changes, and quit TextEdit.
- If you like to tidy up after yourself, you can go back and reset the privileges on the
httpd.conffile and theapache2folder. This will keep other users of your computer from making changes to the Apache configuration. - Open a Terminal window and type this command to restart Apache:
Machine:~ user$ sudo /usr/sbin/apachectl restartType your password when prompted.
- Load
http://localhostin your browser again to make sure that Apache is still running.
Your computer is now equipped with an Apache web server with PHP support. If you need to make changes to Apache’s configuration, you know how to edit its httpd.conf file using the instructions above. The PHP plugin, however, has its own configuration file, named php.ini, and you need to edit that file to tell PHP how to connect to your MySQL server.
With the version of PHP built into Mac OS X, there is no php.ini file by default—PHP just runs with the default settings. In order to modify those settings, you’ll need to open Terminal and copy the /private/etc/php.ini.default file to /private/etc/php.ini:
Machine:~ user$ cd /private/etc
Machine:etc user$ sudo cp php.ini.default php.ini
Password: (type your password)
To make this new php.ini file editable by users like yourself, use the same procedure described above for editing httpd.conf: in Finder use Go > Go to folder to open /private/etc, modify the permissions of both the php.ini file and the folder that contains it, then open the file with TextEdit.
Scroll down through the file or use Edit > Find > Find… (?+F) to locate the mysql.default_socket option. Edit this line of the php.ini file so that it looks like this:
mysql.default_socket = /tmp/mysql.sock
Scroll down further to locate the mysqli.default_socket option (mysqli, not mysql), and make the same change:
mysqli.default_socket = /tmp/mysql.sock
Save your changes, quit TextEdit, and restore the file and directory permissions if you want to. Finally, open a Terminal window and type this command to restart Apache once more:
Machine:~ user$ sudo /usr/sbin/apachectl restart
Type your password when prompted. Once Apache is up and running again, load http://localhost in your browser once more to make sure that all is well.
That’s it! With MySQL, Apache, and PHP installed, you’re ready to proceed to the section called “Post-Installation Set-up Tasks”.