P1: KTX WY027-Mercer WY027-v2.cls June 5, 2004 0:44 1 RI AL Getting Up and Running MA TE PHP, which stands for HyperText Preprocessor, is widely used for creating programmed features for Web sites because it is easy to learn and also because PHP syntax is drawn from other widely used languages, making it familiar to many programmers. In this chapter we present a very brief history of PHP, and then discuss the nature of PHP as it relates to the Web.
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Chapter 1 code that creates a pleasing (hopefully) display of text, graphics, and links within a browser. Although there are a few helpful features of HTML (such as the capability to cause a form submission), for the most part HTML does nothing programmatically. For example, there are no HTML commands that enable you to add two numbers together, or access a database.
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Getting Up and Running know or can easily pick up how to make basic Web pages). You’ll also need to download, install, and configure PHP, so we provide complete instructions about how to do these things in the coming sections.
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Chapter 1 that you may need to rewrite your code a little bit to work properly with the recommended file’s configuration settings. We’ll discuss this more as we go along. Setting Up a Test Machine In this chapter, we’ll walk through setting up PHP5 on a Red Hat Linux machine running the Apache Web server, as well as on a Windows 2000 machine running Internet Information Server (IIS).
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Getting Up and Running PHP5 can be installed on a great variety of Web server/operating system combinations, including under Apache on Windows. The two systems we’re using are the easiest to get working. If neither of them suits you, of course you can install whatever other configuration you want—you should still be able to run all of the examples in the book. Refer to the PHP5 manual for more general installation instructions.
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Chapter 1 Installing PHP5 with Linux and Apache At the time of this writing, the very first release candidate of PHP5 was available, and that’s the one we’re using. But you may want to check the PHP site for more recent versions, and any notes about changes. The combination of Linux, Apache, MySQL, and PHP is probably the most common production environment for running PHP Web servers.
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Getting Up and Running have an RPM for PHP5, so we’ll provide the instructions for getting and installing the RPM for PHP4 here, and then show how to download and compile PHP5 for Fedora later. By the time you read this, in all likelihood there will be an RPM available for PHP5 for your Linux distribution, so the RPM installation presented here should provide good guidance for installing PHP5 via the RPM method.
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Chapter 1 system can be controlled using the rpm command-line tool, and you’ll see how to install the required components using this interface.
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Getting Up and Running If you install the packages in the order listed previously, you should find that all the prerequisite files are installed in the necessary order. Installing PHP5 by Compiling from Source Files The installation method we’ll use for installing PHP5 on Red Hat Fedora running Apache is downloading the source files and compiling them.
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Chapter 1 Figure 1-2 You can use the Konquerer file-management tool to view the contents of the compressed file. Figure 1-4 shows some of the contents in the tar file. You also could use Konquerer to copy all of the compressed file’s contents directly to another folder, but doing so will make your compilation fail cryptically (meaning you’ll get strange error messages that won’t help you figure out what’s wrong).
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Getting Up and Running Figure 1-3 Now that you’ve cd’d to the php-5.0.0RC1 folder, you’ll see quite a few folders and files there. Open the INSTALL text file (see Figure 1-6) to find many of the instructions related to your installation. Folder and directory are equivalent terms and can be used interchangeably.
P1: KTX WY027-01 WY027-Mercer Chapter 1 Figure 1-4 Figure 1-5 12 WY027-v2.
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Getting Up and Running Figure 1-6 Checking Apache for DSO installation You must have Apache installed and set up for dynamic modules before compiling PHP5 as a DSO. Use the following command from the terminal to make sure Apache is ready: httpd -l You see a terminal window like the one shown in Figure 1-7. As long as mod_so.c is present, you’re OK to proceed.
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Chapter 1 Figure 1-7 Running the configure Script Within your PHP5 directory (probably named something like php-5.0.0RC1.), you’ll find a shell script named configure. This script accepts arguments to control the features that PHP will support. You’ll need to run this script to configure compilation, not PHP5 itself (PHP configuration with php.ini will come later).
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Getting Up and Running For your quick install use only –with-mysql and –with-apxs2. If you get any error messages telling you something couldn’t be found, provide the full path to the folder in which the appropriate files can be found. For example, our configure command found the path to mysql. If it hadn’t we would have provided the full path to mysql as part of the command to run the configure script.
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Chapter 1 Figure 1-8 attacks such as accessing system files and Web documents can be launched against your server. When you compile PHP5 with the –with-apache option, you will have a Server Application Programming Interface (SAPI), which provides a higher level of performance and security over the CGI method. Setting up Apache for PHP To install Apache, use RPMs or download and compile the source code.
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Getting Up and Running Figure 1-9 If Apache is not already installed and running, you can use these commands for installing it from the Terminal: lynx http://httpd.apache.org/download.cgi gzip -d httpd-2_0_NN.tar.gz tar xvf httpd-2_0_NN.tar ./configure --prefix=PREFIX make make install vi PREFIX/conf/httpd.
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Chapter 1 Figure 1-10 If there isn’t such a line, you need to add one; or if the path is wrong, you need to correct it. Find out where the PHP compile put your libphp5.so file. Open Konqueror and choose Tools ➪ Find file. My installation put the libphp5.so file in /usr/lib/httpd/modules You need this information to tell Apache how to run PHP scripts. In the Apache configuration file httpd.conf, add the LoadModule instruction to load PHP5.
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Getting Up and Running ClearModulesList line in the file, you need to add the following line to the file. Although it doesn’t matter where you put it, it makes sense to locate it near other AddModule lines for easy access in the future: AddModule mod_php5.c The AddModule line is not required unless you have a ClearModulesList line. Finally, you tell Apache how to recognize a PHP program file by its extension.
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Chapter 1 Figure 1-11 Now let’s take a look at the Internet Service Manager, a console application for managing IIS, to see how IIS has been configured. Select Start ➪ Programs ➪ Administrative Tools ➪ Internet Services Manager. The IIS snap-in displays in Microsoft Management Console (MMC), as Figure 1-12 shows.
P1: KTX WY027-01 WY027-Mercer WY027-v2.
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Chapter 1 Figure 1-14 Now, the directory you created (such as PHPRC01) contains several subdirectories, and a few text files. It also contains a program file called php.exe, which you won’t actually be using, and a library file called php5ts.dll (in the dev folder). Copy this .dll to your D:\WINNT\System32 directory (using the correct drive letter for your machine). Now copy the rest of the .
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Getting Up and Running Figure 1-15 like switches, turning a variety of PHP behaviors on and off. Extensions provide added or enhanced built-in capabilities to PHP. At the top of your PHP directory should be files called php.ini-dist and php.ini-recommended. Copy the php.ini-dist file to D:\WINNT (using the appropriate drive letter), rename it php.ini, and open it up with Notepad.
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Chapter 1 This gives you access to the functionality of the GD library, which enables you to generate images using PHP programs (you’ll see how in Chapter 16, “Generating Graphics”). Save your modified php.ini file. Again, select Start ➪ Programs ➪ Administrative Tools ➪ Internet Services Manager, and open the hierarchy of services. Right-click Default Web Site, and bring up its Properties (see Figure 1-16).
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Getting Up and Running Figure 1-17 Provided the message in the MMC reports that the World Wide Web Publishing service was started successfully, you now have PHP5 installed. Remember the name of your Web site’s root directory (the book’s is D:\Inetpub\wwwroot). Create a folder in the wwwroot folder. Name it anything you want (but something helpful, like php_files), and place files in it with a .php extension.
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Chapter 1 Figure 1-18 Testing and Troubleshooting Testing your installation of PHP is really as simple as writing a small PHP program and running it. Create a small PHP file to use for tests. Write the following code in it: Save the file as test02.php in any folder within wwwroot (or an appropriate folder if you happen to be running some other OS/Web server combination than Windows 2000/IIS).
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Getting Up and Running Figure 1-19 Did the file you tested work? Or are you reading this section right now because the file didn’t work? Don’t worry; it’s very common for problems to occur anytime you try something new, especially when it comes to computers and programming. In fact, consider yourself lucky if it didn’t work because you’ll learn a lot more about PHP and programming from your mistakes than from your successes.
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Chapter 1 within Services doesn’t mean they are on. Stop and restart them if you need to reassure yourself. You can also check to make sure the Web server is running on the default Web site in Internet Services Manager. For Apache on Linux, check the httpd service (you can also test it by entering http://localhost in your browser). 2. Place a simple HTML Web page in the wwwroot folder, making sure that it has .htm or .
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Getting Up and Running max_execution_time = 30 ; Maximum execution time of each script, in seconds max_input_time = 60 ; Maximum amount of time each script may spend parsing request data memory_limit = 8M ; Maximum amount of memory a script may consume (8MB) ; Whether or not to register the EGPCS variables as global variables. You may ; want to turn this off if you don't want to clutter your scripts' global scope ; with user data.
P1: KTX WY027-01 WY027-Mercer WY027-v2.cls June 5, 2004 0:44 Chapter 1 files go upon installation, and how to test your installation. The basic definition of troubleshooting and debugging was covered, as well as a series of steps to take if your installation of PHP isn’t working. Exercise You’re not really ready to begin programming exercises at this point, so let’s do a different type of exercise that you’ll find handy whenever you have to install with or work with PHP on a new platform.