Installing PHP with Apache on Windows

written by: Tomy Winderfind; article published: year 2008, month 01;


In: Categories » Computers and technology » PHP » Installing PHP with Apache on Windows

We try to install PHP into Apache so it can process PHP pages and static HTML pages. We assume that you have installed and tested Apache.

Downloading PHP

The first step is to download PHP, which is available from the PHP downloads page at http://www.php.net/downloads.php.

Scroll down the downloads page until you find the section labeled Windows Binaries. The current version at the time of this writing is PHP 4.3.0, and there are two different packages available: a .zip file and an installer package. The .zip file is the larger of the two, and it is the one that should be downloaded, since it allows you much more choice when it comes to configuring PHP and adding extensions.

Once you've selected the .zip file, you'll be taken to a list of mirror sites from where you can download the file. It helps to pick a mirror site in a country close to you, as this will be the fastest way to download.

Installing PHP

Before you install PHP, make sure you have stopped Apache. If you have a MySQL server running as well, it's a good idea to stop that, too.

The first step is to unzip the PHP .zip file you downloaded. It's recommended that you extract it to a somewhere fairly logical, such as C:\php. It is best not to use a directory path that includes directory names with spaces in them, such as C:\Program Files\php, since this can cause complications.

You'll find that there is a file called install.txt in the directory you extracted PHP into. Before you start, you should read this file. It may contain special instructions for newer versions of PHP.

There will also be a number of subdirectories relevant to running PHP. Two are especially so: the extensions directory and the sapi directory. The extensions directory, as its name suggests, contains the DLLs required to run extra PHP modules. The sapi directory contains important Server API DLLs, which form the heart of PHP. The PHP module has a different core DLL file, designed for each web server that supports PHP. The one you need for Apache is named php4apache.dll. You need to copy this from the sapi directory into the main php directory—in our case, from C:\php\sapi\php4apache.dll to C:\php\php4apache.dll.

PHP also needs a temporary directory, which it will use to store certain information when it is running. This can be anywhere on your file system, but we advise that you create a directory called temp in the php directory (for example, C:\php\temp).

Inside the newly created temp directory, create a directory called sessions and a directory called uploads. You'll refer to these directories later when you configure PHP.

There are only two more steps left: you need to configure Apache to recognize the PHP modules, and you need to create a php.ini file. We'll look at each step separately.

Configuring Apache for PHP

Configuring Apache to use the PHP module is fairly simple. It involves simply editing the main Apache configuration file, httpd.conf. If you installed Apache, then you will find the httpd.conf file at C:\Program Files\Apache Group\Apache\conf\httpd.conf.

The first section that needs editing is the one that tells Apache which modules to include when it starts up. Each module that you want to add requires an entry in the section under the heading

Dynamic Shared Object (DSO) Support  

DSO support allows you to add extra modules to Apache without changing the main Apache program itself. You'll see a number of entries already in the list, such as the following:

#LoadModule vhost_alias_module modules/mod_vhost_alias.so  
#LoadModule mime_magic_module modules/mod_mime_magic.so  
#LoadModule status_module modules/mod_status.so  

The LoadModule directive tells Apache to load in a module. However, the pound sign (#) at the start of the LoadModule line effectively comments it out and tells Apache to ignore it. To make the module active, you just need to remove the # sign from the front, save the httpd.conf file, and restart Apache.

In this case, you need to add a line to tell Apache to load in the PHP module. This needs entering on a new line underneath the existing ones.

LoadModule php4_module c:/php/php4apache.dll  

If you didn't extract PHP to C:\php, then you'll need to change the path accordingly, of course.

Right below the LoadModule section is a corresponding section for AddModule lines, a few of which are as follows:

#AddModule mod_vhost_alias.c  AddModule mod_env.c  AddModule mod_log_config.c  

Every entry in the LoadModule section needs a corresponding entry in the AddModule section. In the case of PHP, you need to add the following line just beneath the other AddModule directives:

AddModule mod_php4.c  

Note that you don't need to add a path to the AddModule entry.

Finally, this command needs to be added just beneath the last AddModule line:

AddType application/x-httpd-php .php  

This line tells Apache to process all pages with the extension .php using the PHP module. If you want to have other extensions processed by PHP, just add a duplicate copy of this command with the extra page extension for the types of page you want processed by PHP.

These are all the changes that are needed in the Apache httpd.conf file, so you can now simply save the file and close it.

Configuring PHP

Now that you've set up Apache, the next step is to configure PHP. PHP uses a configuration file called php.ini. This file can add to or modify the options built into the PHP module when it was compiled, and it allows you to reconfigure it as you wish without having to recompile it. This file has to be manually created, but this is not as hard as it may sound, because PHP comes with two template configuration files to use as a starting point. These two files are as follows:

  • php.ini-dist – This file contains a default PHP configuration, which is tailored for a development server. This is the file you are going to use as the basis for your own file, as this a development server that you're setting up.

  • php.ini-recommended – This file is tailored for a real web server, and it is locked down, security-wise. All the notes regarding this file are placed as comments at the top of the file, and it's worth reading these to see the differences between the two versions of php.ini.

Two example copies of the PHP initialization file ship with PHP. You can use these as a starting point for your own version.

Creating the php.ini File

Open php.ini-dist in a text editor. If you extracted PHP to C:\php, then the location of this file will be C:\php\php-ini.dist. Before you edit the file, first save it as php.ini in your main Windows directory (such as C:\Windows\php.ini). That way, you will still have a backup copy of the original configuration file.

Although php.ini contains a large number of settings, you need to look at only a few to get the PHP installation up and running. They are for more advanced configurations. Let's look at the settings in the same order as they appear in the file.

Resource Limits

The first two settings we're going to look at are in the section labeled Resource Limits.

Error Reporting and Logging

  • error_reporting

    This setting defines the level of error reporting that PHP uses. The default value is

    error_reporting = E_ALL & ~E_NOTICE  

    This tells PHP to show all PHP errors and warnings, but not to display notices. If you want PHP to show notices as well, then you would change the setting to

    error_reporting = E_ALL  

    Normally, notices cause more problems than they solve, so it's easier to leave them turned off. This setting can be overridden by the PHP error_reporting() command, so it's best to leave notices turned off here and then turn them on as desired in your PHP scripts.

Paths and Directives

Here you can set up the paths that PHP uses.

  • doc_root

    This setting tells PHP which directory on the server's hard disk represents the root directory of your web site. It needs to match the DocumentRoot setting in the Apache httpd.conf file. If you configured Apache according to this guide, then this should be set to

    doc_root = "C:\web server"  
  • extension_dir

    This setting tells PHP where its extension DLLs are located. If you extracted PHP to c:\php, then this should be set to

    extension_dir = "C:\php\extensions\"  

File Uploads

Scroll down to the section labeled File Uploads, which governs settings concerning file uploading from a web page.

  • file_uploads

    This setting can be on or off. It tells PHP whether to allow file uploading through a browser or not. The default setting is

    file_uploads = On
  • upload_tmp_dir

    This setting sets the location where uploaded files are stored temporarily until they are moved to their proper location. This should be changed to the temp directory that was created after the PHP files were extracted:

    upload_tmp_dir = "C:\php\temp\uploads"  
  • upload_max_filesize

    This setting specifies the maximum file size (in MB) that can be uploaded through a PHP web page. The default setting is

    upload_max_filesize = 2M  

Sessions

The final setting in php.ini that we are going to look at is in the Sessions section.

  • session_save_path

    This setting is the main cause of problems with session variables under Windows, as the default setting is

    Session_save_path = /tmp  

    Because this /tmp directory doesn't exist, session data can't be saved, and sessions don't work. To fix this, you're going to change the path to the temp directory you created earlier, so this setting should be changed to

    session.save_path = C:\php\temp\sessions  

This completes configuration of the main PHP settings, so you can save php.ini and then close the text editor.

Restarting Apache

As you've edited both the Apache configuration file, httpd.conf, and the PHP configuration file, php.ini, you need to restart Apache (if it's running) for the new settings to take effect. Refer back to the "Checking the Apache Installation" section for details of how to do this.

Now that the server has been restarted, you can move on to check that the installation was successful and that PHP pages are being processed.

Testing Apache and PHP

When you restarted Apache, you will have received a message similar to the following (if you used the manual install):

Apache/1.3.27 (Win 32) PHP/4.3.0 running...  

You should see that, in addition to the main Apache message, there is a message saying that PHP is running. If you get an error message, it should tell you in which configuration file the error was found and the setting that caused it. You can then correct the error and restart Apache.

To test PHP, open Dreamweaver MX and create a new page with the following code:

<html>  
<head>  
<title>PHP Test Page</title>  
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  
</head>  
<body>  
<?php phpinfo(); ?>  
</body>  
</html>  

Save this file as phptest.php in the directory set as Apache's document root. If you followed this guide, you would save the file as

C:\web server\phptest.php  

Now that the file has been saved in the web server's document root directory, it's now available through Apache. Open a new browser window and enter the following address:

http://localhost/phptest.  php  

If the installation was successful, you should see a screen similar to the screen shot shown here. The call to the phpinfo() function simply produces an automated output of all of the PHP settings.

Note that the Server API section will show Apache for an ISAPI install or CGI/FastCGI for a CGI install.

Troubleshooting a PHP Installation

If you have a problem with the installation, it likely has its roots in the php.ini file. First check that php.ini is located in your Windows directory (such as C:\windows), so it can be found and the settings can be read.

Open the php.ini file in a text editor. Make sure that all the values and paths you entered are correct.

If you get a 404 – Page not found error when you know that the page exists in Apache's document root directory, it may mean that the document root given in the php.ini file is incorrect. This must be changed so that it exactly matches the document root specified in the Apache httpd.conf file and the location of the folder holding your web site.

Open the Apache httpd.conf file and check in the LoadModule section that the correct path to PHP is given, and that you have entries for both LoadModule and AddModule.

If you later discover that you have problems with session variables not working or files not uploading through a browser, check that the path to the PHP temp directory is correct. If you have followed the steps in this guide, then the path for session variables is c:\php\temp\sessions, and the path for file uploads is c:\php\temp\uploads.

If all else fails, try copying all the DLL files from the PHP install directory into your Windows\system32 directory. If you've had a previous version of PHP installed, you'll need to allow Windows to overwrite any existing PHP files.

The online PHP manual at http://www.php.net is a great resource. There are many user comments added to the manual pages that show how others have solved real-life problems. Have a read through the configuration and installation sections of the manual, and you're more than likely to find information that will help you track down the cause of any trouble.

It's also useful to do a search of the Web and Usenet. Because PHP has so many users, it's likely that someone else has experienced a similar problem to your own and has documented how he or she solved it.

PHP Extensions

Like Apache, PHP can also use modules to provide extra functionality. To use these modules, first look in the online manual at http://www.php.net/ for the relevant page for the extension concerned. This is a must, as some PHP extensions require other software or DLL files to be installed that are external to PHP. For example, the XSLT extension requires an application called Sablotron.

If you want to use these extensions, you must first copy all files from the dlls directory, which was created when you extracted PHP. Assuming you followed this guide, the path to this directory is C:\php\dlls. All the DLL files in this directory need to be copied into your Windows\system32 directory. Note that only the files themselves should be copied to C:\Windows\system32, not the directory.

The DLL files for the extensions themselves are located at C:\php\extensions. Again, the DLL files for the extensions you want to use should be copied to your Windows\system32 directory.

Finally, open your php.ini file with a text editor and scroll down to the Windows Extensions section, where you'll see entries similar to the following:

;extension=php_bz2.dll  
;extension=php_ctype.dll  
;extension=php_cpdf.dll  
;extension=php_curl.dll  
;extension=php_cybercash.dll  

All the current extensions are listed here, but they have a semicolon (;) at the start of their entries. This indicates that PHP should ignore the line, so the extension isn't actually loaded. To activate an extension, simply remove the semicolon from the front of the line and resave the php.ini file. If the extension needs any external files or software, you should install them now and restart Apache as described earlier so that the changes will take effect.

legal disclaimer

1) Our website is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringements, please read the Terms of service and contact us to investigate the problem.
2) The E-articles directory team is not responsible for inaccuracies, falsehoods, or any other types of misinformation this tutorial may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here. Please read the Terms of service

Useful tools and features

Translate this article to...    Send this article to you or to a friend

Link to this article from your page   
If you like this article (tutorial), please link to it from your web page using the information above. Linking to this page, this is the only way to help us improve our service, the same time providing your visitors with a way to improve their online experience.

related articles

1. Common PHP Errors
In this tutorial, we are going to look at some of the common PHP errors that occur and how to solve them. Parse Errors A parse error occurs when the format of your PHP code is incorrect. For example, the following code: <?php for($i=1;$i<10;$i++){ $output = "Current Iteration: " . $i . "<br>" echo $output; } ?> will return an error similar to the following: Parse error: parse error, unexpected T_ECHO in c:\webserver\test2.php on lin...

2. PHP mail
In this article, we are going to look at some of the frequently asked questions regarding e-mail and PHP. We will begin by looking at a more fundamental issue: how to actually send an e-mail as HTML. How Do I Send an E-mail As HTML? As the PHP mail function defaults to sending plain text e-mails unless otherwise specified, a frequent question is how to send HTML e-mails using the mail function. The format for the mail function is as follows: mail($to, $subject, $message, $headers); ...

3. PHP Date and Time
In this tutorial, we look at commonly asked questions regarding the date function in PHP. How Do I Read the Date or Time from the Server? To read the date or time from the server, you need to use the PHP date function. You pass the date function a string, with special tokens in place of the date parameters you require. When the code is run, the tokens will be replaced with the date or time section that they represent. For example: <?php echo date("d/m/Y"); ?> outputs 11/02/2003 &...

4. PHP Random Password
In some applications, it can be useful to generate a random password, such as setting the user's initial password and then letting the user change it if he or she wishes. The function shown in the following code generates a password with a number of random characters, and you can set the length of the password when you call the function: <?php function randomPassword($length) { $possibleCharacters = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $characterLength = strlen($possibleCharac...

5. How to Install PHP into Apache
In this section, we look at how to install PHP into Apache. The first step is to download it from the PHP web site. There are other sources for PHP around the Web, but it is much easier to get it from the source. Downloading PHP PHP is available as a free download from the PHP web site, http://www.php.net/ downloads.php. The file that you need to download is at the top of the page, in the section labeled Complete Source Code. The current file at the time of this writing was for PHP 4.3....