Installing PHP on Windows 7 is quite a simple affair. However, as experienced developers know, you need to have a
web server running first. I suggest using the world's most popular web server - get instructions on how to
install Apache on Windows 7 operating system. And if you are a budding PHP programmer, you would very soon like to use it with MySQL database. So once you have finished with PHP installation, read the steps to
install MySQL on Windows 7.
Assuming you have successfully been able to install Apache on Windows 7 and have seen the "
It works" page, download the PHP file from
www.php.net. Though I am on a 64 bit Windows 7 system, I downloaded php-5.2.11-Win32.zip because PHP, at the time of writing, doesn't come in 64 bit flavour.
Important note: Download the Thread Safe PHP zip file.
Create a folder to install PHP
This tutorial will describe how to install PHP from the zip archive and not from the installer file - [Slide 1]. Somehow, I've found this is a little simpler.
The first thing is to create a folder where you would like to unzip the PHP files. Since I like to keep things segregated and yet accessible, I'll be installing PHP in its own directory under the C: drive and recommend you do the same. So go ahead and create a folder called PHPon the C drive. Now unzip the contents of the downloaded PHP zip file into this folder; refer [Slide 2].
In this C:/PHP folder, you'll see a file called php.ini-recommended. Right-click on this file and choose Copy - [Slide 3]. Now paste the file in the same directory - [Slide 4]. Rename this file to php.ini. Windows will tell you that changing the extension will make it unusable; ignore the message and continue - [Slide 5].
Can't find the php.ini-recommended file?
The newer PHP versions come with two files: php.ini-development and php.ini-production. The former is more suited for a developer machine such as yours, while the latter is for web servers that host 'live web sites' in which security is of paramount importance. Thus, use php.ini-development instead of php.ini-recommended.
Configuration Apache 2.2 web server for PHP installation
Though you have been able to put PHP on your computer, it's still not "installed" in the true sense of the word. We have to help the Apache web server communicate with PHP. This is done by editing the Apache configuration file, httpd.conf.
Go to All Programs -> Apache HTTP Server 2.2 -> Configure Apache Server -> Edit the Apache httpd.conf Configuration File. This opens httpd.conf file in Notepad - [Slide 6].
Gear up to make some changes to this file - check the screenshots from the slideshow as you go along.
- Locate the "LoadModule section" (Tip: use the Find feature or the Ctrl-F key combination). Add the following line - [Slide 7]
LoadModule php5_module "c:/PHP/php5apache2_2.dll"
Note: If you have chosen to install PHP is a different folder and not C:/PHP, you have to change the path in the above line.
- Find the "AddType" section and put in the line below - [Slide 8].
AddType application/x-httpd-php .php
Note: You can also use other file extensions for PHP like .php3 or .php4 - simply add these at the end of the above line separated by a blank space.
- Move to the end of the httpd.conf file and add the line below - [Slide 9]
PHPIniDir "C:/PHP"
Note: This tells the Apache web server the location of the PHP configuration file -php.ini.
- If you've read my instructions on installing Apache on Windows7 know that all web site files are to be kept in the htdocs folder located under the Apache install directory. This means when you load http://localhost, Apache checks the htdocsfolder - the "It works" file that's displayed the first time you test Apache installation is actually the index.html file in this folder.
In accordance to my habit of having things organized, I like to keep web site files in their own folder away from Apache program and configuration files... something likeC:/websites. The good news is that Apache allows you to make such a change pretty easily and to do this, you need to specify this new directory path as thedocument root.
First create a folder for your web site files - C:/websites is what I use and is a nice choice. Find the DocumentRoot "C:/Apache/htdocs" in the Apache configuration file and change it to DocumentRoot "C:/websites" - [Slide 10].
- Lastly, change <Directory "C:/Apache/htdocs"> to <Directory "C:/websites"> - [Slide 11].
Testing the PHP installation
Save the httpd.conf file - "File" -> "Save" [Slide 12] and restart your computer. You actually don't need to restart the machine; stopping and starting the Apache service would have probably worked but I needed a cup of tea.
It's now time to test whether PHP was properly installed on Windows 7. Open a Notepad window and type in the line below exactly as it appears - you can copy-paste if you want.
<?php phpinfo(); ?>
Save this file as phpinfo.php in the document root folder which should beC:/websites if you have followed by instructions above.
IMPORTANT: By default, Notepad will save the file as .txt. So you need to select "All Files" from the "Save as type" drop down and enter the full file name in the "File name" field. Confused? Check the screenshot - [Slide 13].
Open a browser window and load http://localhost/phpinfo.php. A long page with details of the installed PHP version should display - please refer Slide 14. If you see the PHP code instead, you've probably not saved the file with a .php extension.