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.)
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:
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/.
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.
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
sudo mv libphp4.so /usr/libexec/httpd/
3. Enter your administrative password when prompted.
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.)
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
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:
You should now see a table containing details particular to the installed PHP module. Well done!
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:
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.
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 ./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.
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.
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.