One of the biggest hurdles for new programmers is starting. Before you can write your first line of PHP, you must first download Apache and PHP, and usually MySQL, and then fight through installation instructions that are full of technical jargon you might not understand yet. This experience can leave many developers feeling unsure of themselves, doubting whether they've installed the required software correctly.
In my own case, this hurdle kept me from learning programming for months, even though I desperately wanted to move beyond plain ol' HTML. I unsuccessfully attempted to install PHP on my local machine not once, but three different times before I was able to run my first PHP command successfully.
Fortunately, the development community has responded to the frustration of beginning developers with several options that take all the pain out of setting up your development environment, whether you create applications for Windows, Mac, or Linux machines. These options include all-in-one solutions for setting up Apache, MySQL, and PHP installations.
The most common all-in-one solution is a program called "XAMPP" (www.apachefriends.org/en/xampp.html), which rolls Apache, MySQL, PHP, and a few other useful tools together into one easy installer.
XAMPP is free and available for Windows, Mac, and Linux, so this book assumes you will use it as your development environment.
NOTE
Most Linux distributions ship with one flavor or another of the LAMP stack (Linux-specific software that functions similarly to XAMPP) bundled in by default. Certain versions of Mac OS X also have PHP and Apache installed by default.
Enough background: You're now ready to install XAMPP on your development machine. This process should take about five minutes and is completely painless.
NOTE
A good habit to get into is to create separate development and production environments. A development environment is for testing projects for bugs and is generally sheltered from the world at large. A production environment is reserved for fully functional, publicly available projects.
Your first task is to obtain a copy of the XAMPP software. Head over to the XAMPP site (www.apachefriends.org/en/xampp.html) and download the latest version (0.7.4 for Mac, 1.7.1 for Windows, and 1.7 for Linux at the time I write this).
After downloading XAMPP, find the newly downloaded installer and run it. You should be greeted with a screen similar to the one shown in Figure 1-1.
NOTE
All screenshots used in this book were taken on a computer running Mac OS X 10.4.11. Your installation might differ slightly if you use a different operating system. XAMPP for Windows offers additional options, such as the ability to install Apache, MySQL, and Filezilla (an FTP server) as services. This is unnecessary and will consume computer resources even when they are not being used, so it's probably best to leave these services off. Additionally, Windows users should keep the c:xampp install directory for the sake of following this book's examples more easily.
Click the Continue button to move to the next screen (see Figure 1-2), where you can choose the destination drive you want to install XAMPP on.
The installation wizard's next screen (see Figure 1-3) asks what type of installation you prefer. This is your first time installing XAMPP, so the only available option is a basic installation of XAMPP.
Clicking "Upgrade" brings up a screen that shows the progress of XAMPP as it installs on the selected drive (see Figure 1-4).
Installation requires a minute or two to complete, whereupon the installer displays the final screen (see Figure 1-5), which confirms that the installation was successful.
So far you've used the XAMPP wizard to install Apache, PHP, and MySQL. The next step is to activate the trio of applications.
You can activate the just-installed applications by navigating to the newly installed xampp folder and opening the XAMPP Control Panel (see Figure 1-6).
NOTE
When opening the XAMPP Control Panel you may be prompted for your password. This has no effect on the services themselves and should not affect the projects covered in this book.
Activating Apache, PHP, and MySQL on your development machine is as simple as clicking the "Start" button next to both Apache and MySQL on the XAMPP Control Panel. You might be prompted to confirm that the server is allowed to run on your computer, and you might be required to enter your system password. After you do this, the "Output" panel should start displaying a series of messages (see Figure 1-7); the final message displayed should say, "XAMPP for (operating system here) started."
NOTE
There is also an FTP (file transfer protocol) option available in XAMPP. FTP provides a method for moving files between networks. The examples in this book don't require this option, so there is no need to activate it in the XAMPP control panel.
It's a simple matter to check whether all the services are running properly on your development machine. Simply open a browser and go to this address: http://localhost. If everything has gone correctly, you'll be redirected to http://localhost/xampp/index.php (see Figure 1-8).
If this screen loads, you've installed Apache and PHP on your development machine successfully!
If you do not see this screen, the XAMPP online community is extremely helpful and most installation issues have been addressed in the Apache Friends forum at http://www.apachefriends.org/f/viewforum.php?f=34.
The address, http://localhost, is an alias for the current computer you're working on. When using XAMPP, navigating to http://localhost in a browser tells the server to open the root web directory; this is the htdocs folder contained in the XAMPP install directory.
Another way to use your server to access the root web directory on your local machine is to navigate to the IP address—a numerical identifier assigned to any device connected to a computer network—that serves as the "home" address for all HTTP servers: http://127.0.0.1.
You can verify that MySQL is also running by going to the Tools menu and choosing "phpMyAdmin." This should bring up the screen shown in Figure 1-9.
Now that have MySQL running on your development machine, you're ready to start running PHP scripts. Note that if you're a Windows user, you might need to navigate to C:xamppphpphp.ini and locate the following lines to verify that magic_quotes_gpc is set to Off:
; Magic quotes for incoming GET/POST/Cookie data magic_quotes_gpc = Off
3.149.240.196