Ward Mundy's Technobabblelog

We've been exploring the possibilities of the Mac mini for the past couple weeks with a series of articles covering first what it would take to turn the Mac mini into a Home Automation Server. And then we built a Computer Telephony Server. This week we're using the Mac mini to build a full-featured Internet Hosting Server a.k.a an ISP-In-A-Box. This is the fourth installment in our five-part ISP-In-A-Box series. If you're just joining us, you might want to start at the beginning and read the articles from the bottom up using this link. On the first day, we covered hardware requirements and installation and operation of the Apache Web Server. Day Two added mail services including SMTP, POP3, and IMAP servers. And yesterday we installed MySQL to handle all our data base chores.

Today we'll cover installation of PHP, a pedal-to-the-metal scripting and programming language that you can use to build or run web applications for just about anything. As we mentioned yesterday, adding PHP to the server with MySQL gives you access to literally thousands of free, commercial-quality applications that already have been written by talented programmers all over the world. Here's a partial list from just one of dozens of web sites that catalog these things. After we get PHP up and running, we'll install phpMyAdmin, your first PHP application. We'll use it to manage and create MySQL databases, and you can kiss the MySQL command prompt goodbye ... forever.

For those of you who are programmers or wannabe's, one of the beauties of PHP is its extensibility. In addition to the functions provided in the basic language, others have added function libraries for PHP covering almost every programming discipline you can imagine including imaging, printing, emailing, data base integration with virtually every DBMS on the planet, XML, SOAP, PDF, compression technologies, date, time, calendaring, TCP, URL and socket functions ... and we've barely scratched the surface. The documentation alone (in over two dozen languages) runs well over 1,000 pages. Assuming you decide to develop your own applications, if you've learned any other programming or scripting language including ASP or Visual Basic on the Microsoft platform or C on any platform, then you'll be right at home with PHP. If you're just getting started with programming, find a PHP course at your local community college or university. They all love PHP! But, as we noted earlier, one of the real beauties of PHP is not having to write any applications unless you want to. If there's some web-based application you needed yesterday for a project, chances are someone already has written it for you using PHP.

To simplify the installation process, we're going to use a PHP installation image (.dmg) file rather than a tarball just as we did with MySQL. We're going to install PHP 4.3.10 for OS X v10.3. Make sure you have applied the latest Apple patches to Panther before we begin. As already mentioned, PHP 5 is also available, but it is not yet in widespread use by ISP's, and many of the best PHP applications still require version 4. Click on this link for documentation about this PHP build and for other installation images. When you're ready to begin the actual download we'll be using, click here. The download is 26MB so a modem is probably not the optimal approach. When the download completes, the image file should automatically mount on your Mac desktop. If not, double click on the .dmg file to open the folder. Now double-click on php-4.3.10.pkg. Let it check the compatibility of your Mac. Then select your default drive, provide your admin password, and PHP will be installed. When the installation completes, you can eject the image file from your desktop.

[Update: On some machines, a minor adjustment in the Apache configuration file may be necessary to support PHP. Open a terminal window (Go to Applications/Utilities and double-click on Terminal icon). Type sudo su and press enter to switch to the root user. Type cd /etc/httpd and enter to move to the Apache config directory. Type pico httpd.conf and enter to load the Pico editor and edit the Apache config file. Be careful here! Press Ctrl-W, type php4, and press enter to search for the word "php4." You should be positioned on a line which begins #LoadModule php4_module .... If there is a pound sign (#) at the beginning of the line, we need to remove it. Don't hit delete! If you did, just backspace over the weird characters. Position the cursor under the # sign and press Ctrl-D. Now press Ctrl-W and enter to find the next occurrence of php4. Do this a couple times until you get to a line that begins #AddModule mod_php4.c. If there is a pound sign (#) at the beginning of the line, delete it by positioning the cursor under the # sign and pressing Ctrl-D. That's it for the changes. Now save the file by pressing Ctrl-X, then Y, then enter. Exit gracefully from the terminal window by typing exit and pressing enter, repeat it, and then Command-Q. Now we need to stop and restart the Apache Web Server for the changes to take effect. Click on the Apple icon in the upper left corner of the screen and choose System Preferences. Double-click on the Sharing folder. Deselect Personal Web Sharing by clicking on the check mark beside this option. Wait for the web server to stop. Then select Personal Web Sharing by clicking on the check mark box beside it again. Wait for the web server to start up again, and then close System Preferences by pressing Command-Q. ]

Now let's be sure PHP installed properly. Make sure your Apache web server is running (consult the previous column on Apache if you need further assistance or a refresher course). Now open TextEdit in your Applications folder. Change the editor to plain text format by clicking Format, Make Plain Text. Now type the three lines shown in the left inset ending each line with a carriage return. Then save the file to your desktop as test4u.php in Unicode (UTF-8) format. When prompted whether to append .txt to the file name, choose "Don't append." Now use Finder and click on your local hard disk. Then navigate to /Library/WebServer/Documents. Now drag and drop test4u.php in this folder. Using your web browser, type in this address: http://localhost/test4u.php. You should see a PHP-generated page displaying everything you ever wanted to know about your computer. If not, recheck to make sure your web server is running and that you don't have a typo or missing semicolon in your test4u.php file. If all else fails, try reinstalling PHP or call your favorite Mac guru (not me!) for a little assistance. [Update: Once you've tested your system, move the test4u.php file out of your web site folder to prevent misuse.] Congratulations. You've just earned your first programming merit badge!

Now it's time to install our first PHP application, phpMyAdmin. This is a complete web front-end for MySQL that let's you create, delete, modify, and do just about anything else to any and all MySQL databases. Here are a few warnings to get you started. As with any powerful tool, be careful. Don't muck around in the database called mysql or MySQL itself may come to a screeching halt. If you want to experiment, then create your own database and play there. Leave databases connected to applications you need alone. And, finally, here's a word about security. We plugged a hole in MySQL to keep outsiders out yesterday. When you install phpMyAdmin, you're letting them right back in unless you complete all the steps outlined here. You also will want to RTFM (read the manual) and decide for yourself whether additional security measures are warranted because of where your machine is placed. If you are sitting in the middle of a fraternity house and half of your brothers belong to the 'Nerd of the Month' Club, all bets are off. If you're in a private residence with fairly trustworthy relatives around, the security outlined here is probably enough to keep someone from hacking into your new Mac. End of Sermon.

By now you should know the drill fairly well. First, make sure your Mac is running Mac OS X v10.3 and is up to date with Apple patches. Second, we need to download the phpMyAdmin software. Go to the Sofotex web site and click Download Now. Be patient for a few seconds, and your download should begin automatically. If not, choose a mirror site from the list. This will deposit a phpMyAdmin tarball (.tar) on your desktop which should decompress into a folder named phpMyAdmin-2.6.1. If not, double-click on the tarball to decompress it. Now let's open the folder by double-clicking on it. Find the config.inc.php file and Control-Click on it and choose to open it with TextEdit. When the file opens, choose File/Print and print just pages 1 through 4 of the file. While your file is printing, click on the Apple, choose System Preferences, click on the Sharing folder, and then click on the words "Personal Web Sharing." Be careful not to deselect the option or you'll shut down your web server. Now look at the bottom of the display and write down the IP address that's shown after the words "View this computer's website at http://." Press Command-Q to close your System Preferences window. Click in your TextEdit window and then press Command-Q to close it as well. Now close the phpMyAdmin-2.6.1 folder. Click once on the folder's file name and rename it to php and press enter. Now we're going to drag the folder to its permanent home. Open a Finder window, click on your local hard disk icon, and move to /Library/WebServer/Documents. Now drag and drop the php folder from your desktop into the Documents folder, which is as you'll recall the root folder for your computer's web server. Open a web browser, and go to http://localhost/php/. Assuming your Apache web server and your MySQL server are running, you should see a snazzy phpMyAdmin web page with at least the name of your mysql database in the left column and a bunch of nasty, red security messages at the bottom of the screen. If not, check to be sure Apache and MySQL are both running. As the red messages say, YOUR SYSTEM IS NOT SECURE at this point so don't be quitting just yet. Close the web browser, and let's plug the security hole. You're gonna need those four pages you printed and the IP address you wrote down.

We're going to secure your computer now so PAY ATTENTION! We need to edit a few lines in the phpMyAdmin config file to tell it to only let you run the program. Otherwise, anyone with a web browser can type your IP address and then /php to do exactly what you just did. Not good! Move back to your Finder window which should be positioned in the /Library/WebServer/Documents folder. If not, move there again. Then double-click on the php folder to open it. Find the config.inc.php file and Control-Click on it and choose to open it with TextEdit. Now let's look at your printout and mark what needs to be changed. Then you can make the changes CAREFULLY with TextEdit and save the file (Alt -S). First go to page 2. At the top of page 2 should be a line that says $cfg['PmaAbsoluteUri_DisableWarning'] = FALSE;. We want to change only the word FALSE to TRUE. Don't mess up the existing punctuation and quotes! Now go to page 4. About two-thirds of the way down the page are several lines that look like the following. Be very careful in searching for these because there are two more sets of identical lines further down in the configuration file. We don't want to change those.
$cfg['Servers'][$i]['AllowRoot'] = TRUE; // whether to allow root login
$cfg['Servers'][$i]['AllowDeny']['order'] // Host authentication order, leave blank to not use= '';
$cfg['Servers'][$i]['AllowDeny']['rules'] // Host authentication rules, leave blank for defaults= array();

Now, here's what it should look like after you fix it except you need to replace the IP number 192.168.0.112 with the number you wrote down on your piece of paper.
$cfg['Servers'][$i]['AllowRoot'] = TRUE; // whether to allow root login
$cfg['Servers'][$i]['AllowDeny']['order'] // Host authentication order, leave blank to not use= 'deny,allow';
$cfg['Servers'][$i]['AllowDeny']['rules'] // Host authentication rules, leave blank for defaults= array('deny root from all','allow root from localhost','allow root from 192.168.0.112',);

I'm not even going to tell you how important the correct punctuation is, but if you screw it up, you'll know shortly. Once you save the changes, open a web browser, and go to http://localhost/php/. If this didn't work, try http://192.168.0.112/php/ using your own IP address, of course. Do you finally see the same lovely phpMyAdmin? If so, at least you can get in. If not and you see an Access Denied message, check for typos and make sure you put in the correct IP address. Once you can access phpMyAdmin successfully, then we need to make sure no one else can (from anywhere). If you have another machine on your network, use a web browser to go to http://192.168.0.112/php/ substituting your IP address from your trusty sheet of paper again. You should see the phpMyAdmin heading with an Access Denied message. If you have no other machines on your network, visit a friend and try to access your phpMyAdmin site in the same way using your Internet IP address (if you have a firewall/router, you first need to configure it to pass port 80 traffic to your private IP address). You still will see a big red message saying you're using a root account for MySQL with no password. While this is true, it is only accessible while sitting in front of your Mac and NOT from elsewhere assuming you carefully followed the MySQL tutorial yesterday. phpMyAdmin unfortunately isn't smart enough to know the difference. If you want to make this message go away (and that's all we're doing!), open main.php in the /Library/WebServer/Documents/php folder using TextEdit, search (Command-F) for the first occurrence of the word root, and replace it with toot (or any other word but root), and save the file (Command-S). Now close your web browser, reopen it, and go to http://localhost/php/ or http://192.168.0.112/php/ using your own IP address. Whew! Once again you've passed with flying colors.

That's it for today. In our next session, we'll tie up all the loose ends, do a little experimenting with phpMyAdmin, and I'll provide some sample code and point you to some great books to get you started with your new PHP/MySQL programming career. And we'll share some great news about the future of our ISP-In-A-Box series. Thanks for visiting. Have a great weekend!