Skip to main content.
June 16th, 2003

Mac Serving With Apache, PHP, and MySQL

There are various software applications that can turn your Mac into a web log, news portal, or even an e-commerce storefront. A large proportion of these server software applications have three components in common: a web server, a scripting language, and a database. Follow the steps below to get some of the most popular versions of these components — Apache, PHP, and MySQL — up and running.

While this may not be the most exciting feature article you’ll see on MacServers, these components will be needed for many of the future articles to come. (Note: You must have administrator privileges in order to perform some of these steps.)

Apache

The world’s most popular web server is by far the easiest of today’s three components to set up: it’s included with Mac OS X, it can be started with a click of a button, and it requires no configuration for most basic server applications. Select System Preferences from the Apple Menu, and under the Internet & Network heading, select the Sharing icon. The window shown below will appear.

Under the Services tab, click on Personal Web Sharing and then the Start button at right. Finally, enter the following line in the address bar of your browser (or simply click on the link), in order to test your web server:

http://localhost/

Assuming all has gone well, you should now see a welcome page confirming that Apache is indeed up and running. The default location of that welcome page is /Library/Webserver/Documents/.

PHP

Although Apple has thoughtfully included a PHP scripting language module for Apache in OS X version 10.1 and above, Marc Liyanage has put together a more recent PHP module with additional features not included in Apple’s bare bones version. If you would like to use Marc’s version (recommended), please follow the steps below to install it on Mac OS X 10.2 and above. These steps are based on Marc’s excellent instructions, which contain more detailed information about using the PHP module included in OS X, updating a previously installed version of Marc’s module, and installing older versions of the PHP module on prior versions of OS X. Finally, for those who want to be able to interact with PHP via a command line interface (CLI), forgo the PHP installation instructions below in favor of a CLI version of PHP that Jason Perkins has graciously contributed to the community.

Installing PHP

1. Open the Terminal (located in Applications > Utilities)

2. Copy and paste the following three lines one at a time, hitting return after each line (after entering the first line, wait for the download to complete before entering the second line):

curl -O http://www2.entropy.ch/download/libphp4.so.gz
gunzip libphp4.so.gz
sudo mv libphp4.so /usr/libexec/httpd/

3. Enter your administrative password when prompted.

Activating PHP

4. In the Terminal, copy and paste the following four lines one at a time, hitting return after each line and entering your administrative password if prompted. (The third line below is long and may wrap around; be sure to select the whole thing.)

cd /etc/httpd
sudo apxs -e -a -n php4 libexec/httpd/libphp4.so
echo ‘echo “AddType application/x-httpd-php .php” >> /etc/httpd/httpd.conf’ | sudo sh -s
sudo apachectl graceful

Testing PHP

Open TextEdit and paste the following line into a blank document window:

<?php phpinfo() ?>

From TextEdit’s Format menu, choose Make Plain Text and hit OK to confirm the change. Select Save from the File menu, changing the default “untitled.text” file name to “test.php” and then saving it in your Home > Sites folder. Then enter the following line in the location bar of your web browser, replacing the “your_username” portion with your OS X login name:

http://localhost/~your_username/test.php

You should now see a table containing details particular to the installed PHP module. Well done!

MySQL

Perhaps the most popular server database in common use, MySQL is the data store behind a surprisingly large number of web applications.

1. First download the “Standard” package under the “Mac OS X Package Installer downloads” heading — not the “Mac OS X downloads” section just beneath it — from the following page:

http://www.mysql.com/downloads/mysql-4.0.html

The downloaded file should be a disk image named something like mysql-standard-4.0xx.dmg. If it isn’t, you may have downloaded the wrong package (see above). Also, the Mac version of IE has been known to mangle downloaded file names, so you might consider using Camino or Mozilla.

2. Double-click the downloaded .dmg file and wait for DiskCopy to mount the image on your desktop. Open the mounted image, double-click on the .pkg file contained within, and follow the instructions to complete the installation.

3. In the Terminal, copy and paste the following two lines one at a time, typing return after each line. Type your administrative password when prompted.

cd /usr/local/mysql
sudo chown -R mysql data/

4. To launch the MySQL database server, copy and paste the following two lines one at time in the Terminal, hitting return after each line:

sudo echo
sudo ./bin/mysqld_safe &

Assuming all went well, the Terminal should say “Starting mysqld daemon with databases from /usr/local/mysql/data.” You can hit return at this point to bring back the command prompt.

MySQL will stay launched in the background until it is either quit manually via Terminal command or until the host Mac is rebooted or shut down. To avoid the hassle of having to manually launch MySQL via the above Terminal commands after each restart, download and install the following package to put a StartupItem into /Library/StartupItems.

http://www2.entropy.ch/download/mysql-startupitem.pkg.tar.gz

While you should be able to double-click the above file to decompress it with StuffIt Expander, some people have had more reliable results with OpenUp. If using the latter, simply launch OpenUp and then drag the .pkg file on top of the OpenUp icon in the Dock. Whether you use Expander or OpenUp, a window should pop up containing a decompressed file called “startupitem.pkg” (the icon for this file will be an open brown cardboard box with a translucent orange cube inside). Double-click this file and follow the instructions to install the StartupItem.

5. It’s important to set a password for the master root account by copying and pasting the following Terminal command, making sure to replace the “new_password_here” text with your desired password.

/usr/local/mysql/bin/mysqladmin -u root password new_password_here

Following is what the Terminal output looks like when entering the commands above:

You can now type “exit” followed by pressing the return key to exit your Terminal session. Feel free to quit the Terminal application at this point, as your work—for now—is done.

Wrap-Up

At this point, you’re probably thinking: “Okay, now what?” Ahh, patience, little grasshopper. Now that all the tools are in place for deploying PHP/MySQL-based web applications, future how-to tutorials will cover the installation and configuration of a wide range of server software. But you’ll still have to wait a little longer before we get to the sexy stuff. Our next feature article will focus on database management tools, which allow users to create and edit databases without having to memorize Terminal commands.

Posted by Justin at 12:41 AM PDT — Tags: , , ,

This entry was posted on Monday, June 16th, 2003 at 12:41 am and is filed under Tutorials. You can follow any responses to this entry through the comments RSS 2.0 feed. Both comments and pings are currently closed.

7 Responses to “Mac Serving With Apache, PHP, and MySQL”

  1. vaporboy says:

    If you are wondering, Apache 2 was shipped with Mac OS X Server 10.2 and Apple has a good article explaining how to set it up. Just make sure you don’t have Apache 1.3.X running on the same port before you start up Apache 2.X. Anyway, here is the link to the article… enjoy.

    http://docs.info.apple.com/article.html?artnum=107014

  2. greyshadow says:

    It looks like you missed a line of explanatory text in your article following this text :)

    Testing PHP

    Open TextEdit and paste the following line into a blank document window:

  3. Justin says:

    Thanks, Greyshadow. The line must have gotten munged when a recent edit was made. It’s since been re-added. Thanks for pointing that out!

  4. mikebw says:

    I was just about to post the fix myself, but decided I should try it first.
    Problem is I can’t seem to get the file to load correctly. When I type in the /localhost/test.php into safari, it just loads the test.php file as text, no table is generated. any ideas why?

  5. mikebw says:

    ok i figured it out- i went through the steps to activate PHP again, and this time I actually selected the text and copy/pasted it into the terminal. Should have followed advice to do so originally instead of typing by hand.

  6. biggles says:

    Could you please follow this excellent article up with how to use all this with dyndns.org “virtual host” set-up and configuration of Apache httpd.config, virtualhost file setup and configuration of the NetInfo Database to get this all to work. Nowhere have I found definitive instructions on this. Dyndns.org have some instructions for Apache but not OS X (which requires entries in NetInfo), Patrick Gibson has some pointers on this subject but really only for testing servers and I have “Mac OS X Unleashed” which also gives some info. I’m using DNS Update as well but I still can’t get it to work OK.

  7. stefanl says:

    If you want to start with Apache2, PHP, MySQL and Tomcat, I think you have to start here:
    http://www.serverlogistics.com/apache2.php
    It’s a great site with .pkg installers and prefpanes for these 4 packages. Just double-click, install, and you are up and running in no time. These installers are “smart” enough to even know when to upgrade.