Turn Your Old Mac into a Web Server with Linux, Apache, MySQL, and PHP
- 2008.07.09 - Tip Jar
Last time I talked about pressing your aging Mac into service as a web server. To show that it can be done, and quite easily, I decided to take the plunge.
I have three Macs that qualify as "left behind": an eMac, an iBook, and a Power Mac 6500/225. I decided to take the middle ground and selected the iBook.
Why would you want to make a notebook into a server? Well, it's small and takes up little space in the server room. It has a built-in screen and keyboard for when you can't access it remotely. And it has a built-in battery backup in case of power failure! What's not to love?
Besides, I didn't want to cop out and use a machine that actually runs Leopard relatively well, nor did I want to make my demonstration more difficult than necessary (the 6500 has plenty of disk space, but only 72 MB of RAM - a little tight for a server, where 128 MB is a realistic minimum and at least 256 MB is recommended).
I've admitted to my preference for Debian PPC when working with Macs, and that is what I chose for this demonstration as well. I downloaded the 'netinst' version of the installation CD rather than the whole DVD. I think it makes better use of online resources to do so - not only is the install image relatively small, weighing in at under 250 MB (there have been updates to OS X that were larger!), it also forces you to download the latest and greatest packages from the Internet during the installation, virtually guaranteeing you an up-to-date system at first boot, rather than after you've run Software Update umpteen times.
Pop this installer CD into your system and boot (while holding down the C key), and you'll see your Mac do something you've probably never seen before, nor wanted - boot into a text interface. From this initial boot loader, simply press Enter to accept the defaults. Unless your system is very old indeed or full of very odd hardware, it should serve you just fine.
Watch all the text notices about hardware detection and driver status scroll by and wait for the system to drop you at a blue and gray installation screen.
Put your mouse aside, as you won't be using it for the rest of the installation - nor for the life of the server, if you keep running Linux this way.
The installer uses your keyboard arrow keys and Tab key to navigate, and usually the Enter key to accept whatever settings you have chosen.
The first things you'll be asked about are your location, language, and keyboard layout. After you have answered and some additional components have been installed, you will be asked which network adapter you wish to use. Impressively, Debian 4.0 detected my AirPort card and loaded the necessary drivers for me to proceed with my installation wirelessly.
Next you will have to decide on a name for your server. Make it unique on your network - I choose "iBook", not terribly original - and also put in your domain name. If you don't have a domain or don't know how you'll be using the server, just fill in example.org. Both the server name and domain can be changed after the fact if you change your mind.
Partitioning Your Hard Drive
The most daunting part of installing Linux for most folks is partitioning the hard drive. Let me stop here and say that if you have data on this disk and have not made a backup of it, stop now and do so. What you do next could - either deliberately or accidentally - erase that data.
The easiest option is to let the software do the partitioning for you by choosing "Guided - use entire disk." This does exactly what it says, taking over the entire hard drive and creating a new Linux partition on it, erasing everything that's on it. Note that if you have more than one drive, you can choose this option and in the next step tell it which drive to do use.
Your other option is to choose "Manual" and partition the drive yourself. As I had no data on the drive of this iBook, I chose to let the system do it itself.
You should also probably let the system create one big partition for all of your Linux data. It's the easiest system to deal with, and honestly there are few drawbacks to doing it this way, especially for a single-use server like we're setting up.
After your partitions have been approved and set up, you will be asked about your time zone and then to enter a password for the root account. Root is the superuser, and you'll need that access when you're setting up software, but make the password something strong. You'll also be prompted to create a non-privileged user, one that you can use every day without fear of causing accidental damage to the system.
The system will then install the base packages that are located on the CD-ROM and are required for every system. You could stop there once it's done and install nothing else, but your system would be about as useful as a DOS box onto which you've loaded DOS . . . and nothing else.
Before you can install any additional software, you have to tell it where to look on the Internet. By choosing a mirror that is close to you geographically, you are hopefully picking one that will be fast as well. If, after the install, you decide you wish to use a different mirror, you can edit the mirror location in a configuration file, so this decision is not set in stone.
Once that decision is made, you'll be presented with a list of prepackaged installation options. The only one you need is the standard system. If you don't deselect Desktop Environment, you will wind up installing a graphical desktop and desktop applications like OpenOffice and Firefox that you probably don't want or need on a server. And while you could theoretically select Web Server and SQL Server from this list, you don't know what'll be installed in those packages. In the interest of keeping your system lean, I would recommend not selecting them and instead waiting to install them manually after the system is fully installed.
As soon as you say go, the installation will begin in earnest. It won't take long (how long is more dependent upon your Internet connection than the speed of your hardware) before the CD is ejected and you're told that it's time to reboot.
Setting Up Your Linux Server
Let the boot process run automatically, though you will see an option to boot from the CD if you need to, and an option to enter additional boot parameters - you won't likely need to use them. You'll know it's done when you're prompted to log in for the first time.
Log in as root - we need that superuser access to install the rest of the software and tweak the configuration just so. To install the software, you only need one command:
apt-get install apache2 php5 mysql-server phpmyadmin
Note that this will install the web server, PHP, the MySQL server, and a web-based MySQL administration tool. Also note that when you hit Enter, it tells you that in addition to these packages, it will be installing a bunch of necessary supporting packages - called dependencies - to ensure that everything works. Go ahead and say Yes to this process and wait while everything installs. It won't take long.
Once that is completed, there are two final tweaks to ensure that everything is working as expected. First, you must assign a password to the root account in the MySQL server. By default the password is blank, and that's a major security hole. Type the following command:
mysqladmin -u root password PASSWORD
only replace PASSWORD with an actual password. It can be the same as the root password to log into the system, though for security's sake, I wouldn't recommend it.
Finally, to enable the interaction of MySQL, PHP and Apache, you need to change one line in one configuration file. Type this command:
to open up a text editor and the appropriate config file. Press Ctrl-W to begin a search within the document and type in 'mysql' and hit enter. This will take you to a line that looks like this:
The semicolon indicates that this line has been 'commented out' and does not get loaded at boot time. Use your arrow keys to move up to it, and delete that semicolon. Then type Ctrl-O to save (hit enter to keep the same file name) and then Ctrl-X to exit the editor.
The last step is to restart the web server with one simple command:
And you're in business. Go to a different machine on the same network, open up a browser and type in the IP address of your new server. Don't know what IP address it was assigned? From the command prompt on the server, type:
This will bring up information about each of your network interfaces, including the current IP address.
When you click Go or hit Enter, you will see a screen that says, in big letters "It Works!" - this tells you you've done it, your web server is serving up pages. To test that PHP and MySQL are working, replace the "apache2-default" part of the URL that you were redirected to with "phpmyadmin" to see a login screen for the administration tool. Enter root as the user and the password you assigned above using the mysqladmin command, and you'll be able to manipulate your MySQL database using a PHP program.
And that is that! Easy as pie, honestly, and nothing quite like the rat's nest of commands you had to use to configure Linux once upon a time. Now you're ready to start loading up content on the system and serving up web pages, dynamic as well as static, on hardware that used to be ready to be retired.
Looking to do more? Investigate installing an SSH server so you can remotely connect to your server and manage it from another Mac. Look at the webmin software package for remote server maintenance via the Web. Or, if your hardware isn't quite so obsolete, install a desktop environment and see just how much peppier things are under Linux. The sky's the limit!
- Mac of the Day: Classic II (Performa 200), introduced 1991.10.21. The last b&w compact Mac put a 16 MHz 32-bit CPU on a 16-bit bus.
- Support Low End Mac
Cult of Mac
Shrine of Apple
The Mac Observer
Accelerate Your Mac
The Vintage Mac Museum
Mac Driver Museum
System 6 Heaven
System 7 Today
the pickle's Low-End Mac FAQ