Developing your construct on your local computer is the recommended way to learn since you can interact with all of the components on your own machine and not risk causing problems on a production server. That way, if there are problems in the local environment you can fix the snafu’s immediately without having them exposed to your site’s visitors. Advantages to working with your files locally means that you don’t have to FTP them to a server, you don’t have to be connected to the Internet, and you know exactly what’s installed since you did it yourself.
There are three components of installation:
The bullets indicate the order in which you will install the programs. All our examples will be from a PC install perspective with liner notes on Macintosh installation.
First Apache needs to be installed and operational before PHP and MySQL can be installed or else they won’t work correctly. Plus, there wouldn’t be any use for the coding application ad database without the Apache web server. A web server delivers web pages, has an IP address and possibly a domain name. For example, if you enter http://www.oreilly.com/ in your browser, this sends a request to the server whose domain name is oreilly.com. The server fetches the page named index.html and sends it to your browser.
Any computer can be turned into a Web server by installing server software and connecting the machine to the Internet, which is why we need to install Apache. All our installation examples are done on a Windows-based PC.
1. Download the Apache 2.0.5 Win32 Binary. The file is downloadable from http://httpd.apache.or/download.cgi and the file that you save to your desktop is apache2_0.55-win32-x86-no_ssl.msi.
If you are on Mac OS X, you already have Apache installed. Open up System Preferences, select the Sharing panel and click to activate Personal Web Sharing (which is actually Apache). Mac OS X 10.2, 10.3, and 10.4 all come with different versions of Apache, but each works perfectly fine.
Figure 2-1. The Installation Wizard prompts you for basic configuration
2. Install Apache using the Installation Wizard. Double click the MSI installer file on your desktop.
The Installation Wizard walks you through the installation process.
Figure 2-2. Apache License Terms and Conditions for Use
3. Accept the License Terms by clicking the radio button. Click Next>.
Figure 2-3. Apache HTTP Server information
4. Click Next >. Additionally, this window offers a number of excellent resources related to the web server.
Figure 2-4. Black Server Information dialog box
5. In the blank dialog box enter all pertinent network information, a sample is provided below. Then click Next >.
Figure 2-5. Sample Server Information
Port 80 is the default HTTP port. In other words, when you request http://www.oreilly.com, you’re implicitly requesting port 80. By accepting this port, your web requests can be made without specifying a non-default port.
Figure 2-6. Selecting a Setup Type
6. Select the Setup Type. Click Next >. The typical install will work for our purposes.
Figure 2-7. Destination Folder dialog box for the Apache installation files
7. Accept the default installation directory. Click Next >.
The default installation directory, C:\Program Files\Apache Group\, is both standard and easy to find especially when you need to make changes to your configuration.
Figure 2-8. Ready to Install dialog box
8. Click Install. The installer installs a variety of modules, you will see some DOS windows appear and disappear. Click Finish when the installer is done.
9. Test your installation by entering http://localhost/ in your browser’s location field. The local host is IP address 127.0.0.1.
Figure 2-9. Apache’s default index page after installation
10. After entering the URL in your browser, the default Apache page displays, which is similar to Figure 2-9. Notice the circled part of the window where it indicates that the installation was successful.
Now that you can serve up web pages, you’re ready to add PHP.
Go to http://www.php.net/downloads.php to download the latest version of PHP, both binaries and source code can be found on this website. You need to download the zip file versus the Windows installer because the Windows installer only configures the IIS web server.
Create a directory for PHP on your desktop. You don’t need all the files provided in the archive, so unzip the PHP archive into another temporary location.
A temporary location could be on your desktop, which will be easier to find when you go looking for the files.
1. Copy the following files into C:\php: php.exe, php4ts.dll and php2apache2.dll. The php2apache2.dll file is located in the sapi directory.
Use php2apache.dll instead of php2apache2.dll if you are using Apache 1.3.x.
2. Load PHP extensions. If you plan to load PHP extensions, you will need to copy the files in the extensions directory, or the ext directory for PHP 5.x, to C:\php. Or a sub-folder in C:\php, any subfolder as long as the PHP.ini file is updated to reflect the appropriate directory.
MySQL support is now integrated into PHP and doesn’t need an extension dll. However, PHP 5 supports MySQL as a separate module for download. Follow the instructions below to make sure that your installation of PHP works before you start experimenting with installing extensions.
3. Configuring PHP
Copy php.ini-dist from the extracted files into your Windows directory, typically C:\winnt or C:\Windows and rename it to be php.ini.
4. Setup PHP to load Apache as a module. To configure Apache to load PHP as a module to parse your PHP scripts, use a text editor to open the Apache configuration file, httpd.conf, typically found in C:\Program Files\Apache Group\Apache2\conf\.
Figure 2-10. Viewing httpd.conf in Notepad
5. Loading PHP as an Apache module. Open the httpd.conf file in Notepad or another text editor and search for the section that has a series of commented out LoadModule statements. You can find this around line 134. In Notepad >Edit > Go To…> 134.
Statements prefixed by the hash “#” sign in HTML and PHP are considered commented out and can only be seen by you, never your end user in their browser window.
Figure 2-11. Notepad httpd.conf file with the LoadModule section highlighted
6. Restart the Apache server by selecting Start Menu | Apache HTTP Server 2.0.x | Control Apache Server | Restart, so that it can read the new configuration directives you placed into httpd.conf. Alternatively, in the system tray, double click on the Apache icon and click the Restart button.
Figure 2-12. Notepad httpd.conf file with the AddType section highlighted
7. Search for AddType in the file, around line 754, and add the following line after the last AddType statement: AddType application/x-httpd-php .php
Testing Your PHP Installation
1. Create a PHP file, in any text editor, with the following line:
2. Save the file as phpinfo.php and save it under the Apache htdocs directory, usually C:\Program Files\Apache Group\Apache2\htdocs.
3. Open your browser of choice.
Access the file you just created by typing localhost/phpinfo.php into your browser’s location bar. You should see a page of information about your PHP setup like Figure 2-13.
Figure 2-13. PHP Introduction screen
If for some reason it doesn’t work, check to see whether your PHP or Apache setup is causing the problem by the command line prompt: C:\php, then type in php “C:\Program Files\Apache Group\Apache2\htdocs\phpinfo.php”, since the file isn’t in the c:\php directory.
If invoking PHP from the command line causes a large HTML file with all the PHP configuration information displayed, then your PHP set up is fine. The problem is probably your Apache configuration, go ahead and re-read the procedure on how to install and configure Apache.
Enabling PHP on Mac OS X
If you are on Mac OS X you have PHP pre-installed on your computer. You need to edit the Apache configuration file to enable PHP in much the same way you edited the PC text file.
Installing MySQL 4.1
Rounding out the last component that allows you to develop and test pages on your local computer is MySQL. Now download the MySQL Installer.
1. Download the MySQL binaries.
Both the binaries and the source code can be found at http://dev.mysql.com/downloads/. The link takes you to a page where you can enter personal info, or just click No Thanks to download the file. A number of download locations (or mirrors) are available, select one. Download the recommended version for the Windows Essentials (x86) MySQL 4.1 installer. Save the installer file to your desktop.
2. Installing MySQL using the Installation Wizard
Figure 2-14. The Installation Wizard prompts you for configuration settings
Double click the MSI installer file on your desktop. A setup wizard walks you through the installation process. If, perchance you’re installing MySQL 5.0 the install process is the same as it is for MySQL 4.1.
3. The Installation Wizard displays. Click Next >.
Figure 2-15. Select a setup type
4. Select Typical Installation by clicking the Typical radio button, then click Next >.
Figure 2-16. Confirm Installation Folder
5. Click Install on the Ready to Install the Program dialog box.
Figure 2-17. The MySQL.com account provides newsletters and bug reporting resources
6. Select Skip Sign-Up and click Next >. You can optionally sign up for an account, it provides access to a monthly newsletter as well as the ability to post bugs and comments on the online forums.
Figure 2-18. Configure the MySQL server
7. Select the Configure the MySQL Server now check box, then click Finish >.
Figure 2-19. The Configuration Wizard customizes the database settings
8. On the MySQL Server Instance Configuration Wizard click Click Next >.
Figure 2-20. Chose the level of detail dialog box
9. Select the Standard Configuration radio button from Figure 2-20. Click Next >.
Figure 2-21. How to Launch MySQL window
10. Check both Install As Window Service and Launch the MySQL Server automatically check boxes selected. Click Next >.
Figure 2-22. Security settings for the database window
11. Enter a password for the root user in the password and confirm fields. Click Next >. You do not need the Anonymous Account since you can do everything with named accounts
Figure 2-23. Commit the configuration settings window
12. Click Execute as shown in Figure 2-23.
Figure 2-24. Figure Caption Text Goes Here
13. Click Finish so that MySQL is now configured and running on your computer.
All critical components: Apache, PHP 5.0 and MySQL 4.1 are installed. The wizard will inform you of basic problem during installation like running out of free disk space or not having proper permissions on your system to install MySQL.
Although we recommend you start out working locally you can use an ISP account as long as it supports PHP and MySQL.
You’ll need login information to the remote server and you may need to use your ISP’s web-based tool to create your database.
In order to transfer your files and directories you’ll need to activate a File Transfer Protocol (FTP) account at your ISP, usually through your account control panel. Once you have an FTP login you upload your HTML and PHP files using a FTP client.
While your computer likely has the command line version of the FTP client available from the command prompt it can be cryptic to use. Graphical FTP clients make using FTP much easier. FTP Voyager, available from http://sourceforge.net/projects/filezilla/ is one FTP client you can use to upload files to your ISP. Your initial login screen will look similar to Figure 2-25. Fetch is a good FTP program for the Macintosh.
Figure 2-25. FTP Voyager initial screen
After connecting you’ll see a dialog similar to Figure 2-26, but you will not see the identical screen as this FTP Voyager screen. You can drag and drop the .php file you created. Remember, in order for your PHP file to run you need to save it with an extension of .php instead of .html, otherwise it won’t run, because the web server needs to know it’s a PHP file in order to run the PHP interpreter.
Figure 2-26. FTP Voyager directory listing
PHP files must be accessed through a web server since your web browser doesn’t have the ability to interpret the PHP code. A PHP interpreter is used to process the PHP files.
You’re ready to rock and roll and start learning all about basic facts, integration and how to get your dynamic web page up and running as quickly and smoothly as possible. In the next chapter we’ll give you basic information about PHP and simple coding principles that apply to using PHP.