- What You Will Learn
- Approximate Time
- Lesson Files
- Dynamic Web Site Basics
- Choosing a Server Model
- Redefining the Newland Tours Site for Dynamic Development
- Developing with a Local Server
- Setting Up a Local Environment for IIS/ASP
- Setting Up a Local Environment for ColdFusion
- Setting Up a Local Environment for Apache/PHP
- Developing with a Remote Server
- Defining a Dynamic Site in Dreamweaver (All Users)
- Building a Simple, Dynamic Application
- What You Have Learned
Setting Up a Local Environment for Apache/PHP
Setting up a local environment using the open-source Apache and PHP setup can be difficult and frustrating for Windows and Macintosh users who are largely unfamiliar with Unix systems. There are often multiple versions of the same software available, and there are usually multiple ways of installing and configuring it.
But the biggest reason why Windows and Macintosh users are often intimidated by Apache/PHP setup is that the way users interface with the software is fundamentally different from what they’re used to. Windows and Macintosh users are accustomed to interfacing with the computers through dialogs, windows, and wizards. To set up IIS, for example, you access a dialog through the Windows Control Panel. To configure ColdFusion, you access a special Web page, where you fill out nicely designed and well-documented Web forms. To configure PHP, Apache, and (later in the book) MySQL, you will need to open and manually edit text files buried in nested application directories. A single typo can cause the system to malfunction or cease functioning altogether (until you fix it), and you’ll be given little guidance on what to do while you are in the text document. Such is the price of open-source technologies.
This section walks both Windows and Macintosh users through the process, albeit at a slightly generalized level. The reason for this is that the exact steps are likely to change (in minor ways) between the time I write this and the time you read it.
You begin by downloading the latest versions of the Apache Web server and the PHP module software, both available for free on the Web. You should install Apache before attempting to install PHP, because PHP is dependent on a Web server.
When you download Apache, PHP, or MySQL, in addition to different versions for different operating systems, you’ll also usually see options to download the Source or the Binary files. The source code is the non-compiled code that actually runs these products. Before it can be installed and used, it must be compiled, that is converted to 0s and 1s, also known as binary code. The binary code has already been compiled for you, and it is ready to install on your system. With commercial products, such as Microsoft or Macromedia products, you never have the option of seeing the source code—what comes on the CD is the binary file. One benefit of being able to access the source code is that you can change it. However, I assume that if you are reading this book, you are probably not at a point where you want to rewrite portions of the programming code behind Apache or PHP! Also, if you get the source code, you have to go through the additional trouble of converting it to a binary file. Thus, for the purposes of this book, I strongly recommend that you take the easy way out and download the binary file.
Installing Apache for Windows
To download and install the Apache Web server for Windows, follow these instructions.
- Go to the following URL, read the instructions, and click the Download link: http://httpd.apache.org/
In addition to downloading the Web server itself, this site contains numerous useful resources for Web developers.
- Find the Windows Binary file for the most current production release, and click its link.
At the time of this writing, the most current production release was 2.0.54.
- Save the file to disk, and wait for it to download.
At the time of this writing, the Windows binary for Apache was under 5 MB.
The application downloads in a single installer file.
- Double-click the installer file, which is called something like apache_2.0.54-win32-x86-no_ssl.msi to launch the installation
The installer file is on your hard drive, in the directory you saved in the preceding step.
- Complete the installation wizard. In the Server Information screen, enter localhost in both the Network Domain and Server Name fields. Leave the default For All Users option selected. Finish the remaining
screens using the default setting.
The Apache installer is entirely self-explanatory, with one exception: the Server Information screen. This screen tells Apache how developers and users alike should access the server. If you were installing Apache on a network server, you’d have to specify the computer name and domain, but since you are only installing it for use on a single machine, you can just enter localhost.
- If a Windows Security Alert appears, select Unblock.
To enhance security Windows XP users who have installed Service Pack 2 (SP2) will probably see a security alert screen. Windows by default is trying to disable the server you just installed, in case it is a program run by a virus. You know the Apache Web server is legitimate, though, so you can tell Windows to let it run.
- To verify the successful installation of your server, open a browser and go to the following location: http://localhost/
You should see a placeholder page, as follows.
Installing PHP in Windows
Installing PHP in Windows is slightly more complex than Apache, because the files don’t come with an installer. Instead, the files needed for the program come zipped. After you unzip them, you must manually move a few files to different locations.
- Go to www.php.net/downloads.php and download the latest stable (not beta) version of PHP, choosing the PHP 5.x installer version, found in the Windows Binaries section.
At the time of this writing, the latest stable version is PHP 5.0.4.
- Double-click the PHP installer icon.
The icon can be found in the directory into which you downloaded it. My version of the file is called php-5.0.4-installer.exe.
The first screen of the installer warns that you should stop your Web server, in this case, Apache.
- Click the Apache icon in the taskbar (lower-right corner of your screen), and from the menu that pops up, choose Apache2 >
The Apache icon is a smaller representation of the Apache feather logo you saw when you opened localhost in your browser and saw Apache’s default start page. Stopping the server temporarily disables it, which makes it possible for you to make changes to it (in this case, installing PHP). When you are done installing PHP, you’ll reopen Apache and make sure Apache with PHP works.
- Proceed through the screens accepting all the defaults, until you come to the Mail Configuration screen. In that, enter your
SMTP server as well as your email address. Click Next.
Your SMTP server is the server through which you send your email. The information is provided by your ISP. If you don’t remember it, but you are using a mail client such as Microsoft Outlook Express or Entourage, or Mac OS X Mail, you can look it up in the mail settings. For example, in Microsoft Entourage, choose Tools > Accounts, select your email account, and click Edit. In the dialog that appears, you will see your SMTP (outgoing mail server) settings.
- In the Server Type screen, make sure Apache is selected in the list.
As noted earlier, PHP can run on many types of servers, but in production environments, it is usually paired with Apache.
- Click through the remaining screens and initialize installation.
When you are done, you may see the following dialog, which indicates that not all the installer has been written and there are a few tasks you’ll have to complete yourself.
Directions can be found in install.txt, though it is not clear from this message whether they mean install.txt in the PHP folder (C:\PHP) or in the Apache folder (C:\Program Files\Apache Group\Apache2\). Worry not—I’ll walk you through it here.
As mentioned earlier, open-source applications such as Apache and PHP are often configured through plain text files, rather than elaborate dialogs and wizards. Both PHP and Apache have configuration text documents, which you can use to specify important local settings and desired behaviors.
- For PHP, the document is called php.ini, and can be found by default at C:\PHP\BACKUP.
- For Apache, it can by default be found at C:\Program Files\Apache Group\Apache2\conf\http.conf.
When you installed PHP, the choices you made in the dialogs configured PHP for you, so you won’t have to deal with changing php.ini. Unfortunately, a couple changes need to be made in Apache’s configuration file, httpd.conf, and the PHP installer did not make those changes (hence the warning dialog).
Specifically, even though you have installed PHP and it is running successfully, Apache doesn’t know it is there. As a result, if you try to browse to a PHP file on your Apache server, it won’t know to let PHP process the PHP scripts, and since it doesn’t know how to process them either, it will just send them back to your browser. But your browser also doesn’t know how to deal with the script, so it will not open the file. The operating system at that point, knowing that a file needs to be opened and that the browser, which called it, can’t do it, will look for another application to open it. It will find Dreamweaver, which registered itself as an editor for PHP files. So the file will open in Dreamweaver, and you will see the script itself.
To solve this problem, you will go into Apache’s configuration file, httpd.conf, and make a couple of edits so it knows that whenever a PHP file is requested, it needs to let the PHP application process it.
- In Notepad, open httpd.conf.
Again, the default location of this file is as follows: C:\Program Files\Apache Group\Apache2\conf\http.conf.
- Use the Find feature to locate the first instance of the word AddType. Scroll down 15 lines or so, and locate the space between
the last AddType directive (by default it probably is AddType application/x-gzip .gz .tgz) and the next block of code, which begins # AddHandler allows you to map certain file extensions.... Press Enter or Return a few times to create some space.
The first instance is about 75 percent of the way down the file, and it appears in a line that begins, #AddType allows you to add to or override the MIME configuration....
- In that space, type the following code:
ScriptAlias /php/ "c:/php/" AddType application/x-httpd-php .php .php5 Action application/x-httpd-php "/php/php-cgi.exe"
These lines inform Apache that if it encounters any files ending in .php or .php5 that it should pass them off to PHP, whose executable can be found at the address provided.
- Save and close httpd.conf.
PHP and Apache are now configured to talk to each other, and you are almost set to go.
- Return to the Apache icon in the system tray and restart the server.
Apache restarts using the new httpd.conf settings.
Setting Up Apache in Mac OS-X
Apache comes preinstalled as the default on Mac OS X and higher, so there is literally nothing to install. You do, however, have to turn it on, because by default, the Apache Web service is stopped.
- Open System Preferences and click the Sharing folder.
The Sharing folder is used to control file sharing, Web services, FTP access, printer sharing, and firewalls, among other features.
- In the Services tab of the Sharing folder, check the Personal Web Sharing option.
Checking this box starts Apache. That’s it!
Setting Up PHP in Mac OS X
Like Apache itself, PHP comes preinstalled in Mac OS X, so you don’t need to install it. Also like Apache, the version of PHP that ships with Mac OS X (PHP 4.3.11) is not current. You can download and install the latest and greatest version of PHP (to do so, see the Apple developer site, mentioned in the preceding note). That requires some extra work, and it is sufficient for the purposes of this book to use the shipping version. In the remaining chapters, only one line of code won’t work in this version, and it has an easy workaround that I’ll point out when the time comes.
To use PHP on Mac OS X, you need to turn it on. In contrast to turning on the Apache Web server, you can’t just click a check box, though; you need to type some code in the Apache Web server’s configuration file, httpd.conf.
- In the Terminal window, log in as the root user (using su) and open the file httpd.conf in a text editor.
Typically, this file can be found in the following location: /etc/httpd/httpd.conf.
- Search or scroll until you see a number of lines that begin with LoadModule or #LoadModule.
As the name suggests, LoadModule causes extensions, including PHP, to be activated on the Apache server.
The hash mark (#) that precedes some lines signals a comment. That is, the interpreter ignores any lines preceded by a hash mark.
- Look through the LoadModule section for the following line of code. If it exists and is preceded with a # sign, remove that
sign. If no such line exists, type it in at the end of the LoadModule block, exactly as you see below.
LoadModule php4_module libexec/httpd/libphp4.so
This is one of two lines in httpd.conf that activates PHP in Apache.
- Shortly after the block of LoadModule lines, you’ll see a block of lines that all begin AddModule or #AddModule. The following
line of code needs to appear in that block. It may already be there, commented; if so, remove the comment (#) preceding it.
If the line of code is not there at all, then type it at the end of the AddModule block.
Between this and the LoadModule line entered in the previous step, we have given Apache enough information to activate PHP.
- Search or scroll much later in the file, until you see a group of AddType statements.
The AddType group is a little harder to spot, but you can find it by searching for the comment that precedes it, as shown in the following screen shot.
- Verify that the following two lines of code appear in this AddType group, and that they are not commented. If they are not
there, type them in yourself.
AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps
These two lines tell Apache to treat all files ending with the .php extension as PHP files, and likewise to treat all .phps files as PHP source files. PHP source files actually display the PHP code in the HTML document, complete with code coloring, rather than interpreting and outputting them. This option is useful for debugging.
- Save and close httpd.conf.
You have now configured Apache to work with PHP. Before you can use it, though, you must restart Apache.
- To restart Apache, return to the Services tab of the System Preferences, select Personal Web Sharing from the list, click
the Stop button, and then click it again once it becomes the Start button.
Your system is now configured to process and serve PHP files.