Installing PHP on a local Windows-based, Apache, server

While our previous article walked through adding Perl to our local server, Perl just isn't going to cut it for creating dynamic content. For that, we're going to have to install a more powerful language. As I said quite a few articles ago, if we would have gone the IIS route, we could have the use of ASP. However, since we're going the Apache route, that means PHP is our best choice. The fact that the cheapest servers out there use Apache with PHP is another reason to go this route.

After we install PHP, we'll also install MySQL, a powerful database utility (which we'll go over in another post).

Downloading PHP

While the newest version of PHP is 5.x, we'll go ahead and download a copy of the most current version of 4.x (from PHP.net, at http://www.php.net/downloads.php). We'll be doing this because it's fairly easy to move from 4 to 5, but you'll be able to do things in 5 that you can't do in 4. As of the time of this writing, the most current version of PHP 4.x is 4.4.2. We'll want to download the (Windows Binaries) zip file, even though it is significantly larger in size (over 8 MB). Remember you've got a folder where you've got the downloads for Apache, Perl, and the like – make sure you put the PHP installation in the same place.

Installing PHP

Now that we've downloaded PHP, it's time to install it. First, unzip the zip file to the C drive. When we're done, we'll want the folder to be called php (so that the path to php.exe is C:\php\php.exe).

The install.txt document included with the PHP zip tells us exactly what we'll need to do to get PHP up and running. We'll want to install it as an Apache module, instead of as a CGI binary (although, since we did install Perl, we could do the latter if we wanted to).

First, open up the httpd.conf file, as usual. Search for LoadModule, and place the following text at the very end of the listing. If you've placed PHP somewhere other than in C:\php\, you'll need to change the below accordingly.

LoadModule php4_module "c:/php/php4apache.dll"

Scroll down a little bit, and put the following at the end of the AddModule section.

AddModule mod_php4.c

Now, do a search for AddType. After the first uncommented line (AddType application/x-tar .tgz), place the following.

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

Save httpd.conf. Now, in the C:\php\ directory, and find the file php.ini-dist. You'll notice that there's also a file called php.ini-recommend. The second one has some important security points contained within it, that should be used on an actual server. However, again, since we aren't building a server that will be available for people to access online, we can use the dist file.

Now, make a copy of php.ini-dist, and rename the copy to simply php.ini. Next, open the file in Notepad. Do a search for doc_root, and change this to:

doc_root = "c:\home"

Next, do a search for "session.save_path". We'll change this value (and uncomment it) from:

;session.save_path = /tmp

to:

session.save_path = "c:/windows/temp"

Make sure this folder exists. You could also create a folder specifically for PHP temporary files, which is what I'm going to do (c:/windows/temp/temp_php"). Again, make sure that any such folder exists.

Finally, search for "extension_dir", without the quotes. Replace the following line:

extension_dir = "./"

with this line:

extension_dir = "C:\php\extensions\"

Save your changes to the php.ini file, and close Notepad. Now, move this file (php.ini) into the c:\windows\ folder. You may also want to create a copy of the file in the folder you're storing your downloads - this way you have an easy way to restore this file.

Finally, first copy the file php4apache.dll from the c:\php\sapi\ folder into the c:\php\ folder. Next, in c:\php\, copy php4ts.dll into the c:\windows\ folder.

Now, restart Apache.

Open Notepad, and enter the following three lines.

<?php phpinfo(); ?>

Save the file as phpinfo.php, into either your c:\home\ directory, or one of your Web site folders. Finally, open this file through localhost (such as http://localhost/phpinfo.php).

When you load this page, you should see a large amount of information on the PHP version that you've installed, as well as information about your 'server'.

Now that we've installed PHP, we should go ahead and configure Apache so that it doesn't solely look for an index.html file when we go to something like http://localhost/. Open httpd.conf once again, and search for DirectoryIndex. The second occurrence is the one that we want. Change the following:

DirectoryIndex index.html

to:

DirectoryIndex index.php index.html index.htm

Save httpd.conf and restart Apache. Now, if you go to http://localhost/, or any other URL with a trailing /, Apache will first look for an index.php. If it finds one, it will serve it. Otherwise, it will look for index.html, and finally index.htm. If you wanted to emulate some sites, you could even put home.html or home.htm, or main.html, or main.htm, instead.

Now that we can run PHP, we can begin experiencing an all new level of enjoyment. Next time, we'll be working on installing MySQL, so that we can begin using databases. Until then, enjoy.

Next steps:

Now that you've installed PHP, you may want to connect to MySQL, or PostgreSQL.

In another tutorial, I discuss how to move PHP from one directory to another, which is important if you'd like to upgrade PHP, yet preserve PHP4 access.

===

View all of the steps to creating a local Web server, for development.