How to install PHP on Windows

Note

Some of you have asked that the older version of the tutorial with PHP 4.3.x be put back up. I am putting it back up, but will no longer update it. You can still find it at PHP 4 page. However, I strongly recommend using the PHP 5 tutorial below.

PHP 5

  1. Download & Unpack

    Download and install PHP from http://windows.php.net/download/, you should grab the newest VC6 x86 Thread Safe zip package (VC6 is on the bottom half of the page).

    My file was named: php-5.2.17-Win32-VC6-x86.zip
    Warning against PHP Installer
    I would NOT use the "PHP Installer" version. The Installer version (even though it's larger) is missing some of the library/extension files you need for MySQL and it seems to have other random problems every time I try it. After multiple tries over several versions, my conclusion it is NOT worth the tiny bit of time it saves. So I highly recommend you use the PHP Zip Package version.
    Warning VC6 -vs- VC9 (PHP 5.2 -vs- PHP 5.3)
    PHP 5.3 is no longer available in a VC6 binary version (VC6 is needed for older versions of windows).
    * My Windows 7 worked correctly with the newer VC9 (PHP 5.3).
    * My Windows XP would not run VC9 (PHP 5.3).

    If you are using Windows 7 or manually installed the C++ Runtimes (there's links on the left side of the PHP download page) then you can probably get away with using VC9/PHP5.3 versions. Otherwise I highly recommend sticking with the older VC6 (PHP 5.2) version.
  2. Unzip php. In my case, I unzipped to C:\php\
  3. Rename C:\php\php.ini-recommended to php.ini
    VC9 Note
    VC9 will use the file php.ini-development instead of php.ini-recommended
  4. Edit your php.ini

    Open php.ini in a text editor and scroll down about halfway through the file and look for doc_root then change it to point to whatever your Apache DocumentRoot is set to. In my case: doc_root = "C:\public_html"

    Scroll down about 7 more lines and change the extension_dir from extension_dir = "./" to the location of the ext directory after you unzipped PHP. in my case: extension_dir = "C:\php\ext"
    VC9 Note
    VC9 will have ;extension_dir = "ext" so make sure you remove the ; from in front of it also.
  5. Editing Apache Conf File

    Using Notepad open httpd.conf (should be start-menu shortcut "Apache HTTP Server 2.2 > Configure Apache Server > Edit the Apache httpd.conf Configuration File"). Either at the very beginning or end of the file add the following lines: (NOTE: be sure to change BOTH C:/php parts to the directory you installed your php to)
    	LoadModule php5_module "C:/php/php5apache2_2.dll"
    	AddType application/x-httpd-php .php
    	PHPIniDir "C:/php"
    

    Note: If you installed the older Apache 2.0, instead of the above lines, you will need to use the lines listed on the bottom step of the Apache 2.0 tutorial.
  6. [OPTIONAL] Editing Apache Conf File (part 2)

    To get Apache to automatically look for an index.php, search httpd.conf for DirectoryIndex (about line 212) and add the files you want apache to look for when a directory is loaded (if it doesn't find any of these files, it displays folder contents). Mine looks like:
    <IfModule dir_module>
         DirectoryIndex index.php index.html default.html
    </IfModule>
    
  7. Testing

    Restart Apache if it is already running (if it doesn't start or you get errors, use your Apache "Test Configuration" shortcut in the Start Menu to see why).

    To test your PHP simply create a test.php file in your Apache "DocumentRoot" folder (C:\public_html\ in my case). In your test.php file, type these 3 lines and then load the file in your browser like http://localhost/test.php (you should get a whole long list of php variables, settings, etc):
    <?php
    phpinfo();
    ?>
    
  8. Documentation Suggestion

    One weird thing I have noticed about PHP is that it does not come with documentation of any kind. If you are a php developer/programmer, I suggest you download the documentation. Downloads can be found on http://www.php.net/download-docs.php and I personally recommend the "English - Windows HTML Help" (chm) version as the search is so handy, although they all contain the same information.

Comments

Great articles!!! I'm having an issue with Step 5 (Editing Apache Conf File). I believe I've followed every step exactly, but when I restart Apache, I get an error: "The requested operation failed!"

[edit]
I had installed the wrong Apache version (httpd-2.0.64-win32-x86-no_ssl). I installed the one you used and it works great now. Thanks!

In the start menu, use the "Test Configuration" shortcut to find the error in the httpd.conf (if the text window pops up then closes before you can read it, your config file is fine).

Also the last couple errors in the Apache error log (there should be a link in the start menu to that to) should tell you the reason it could not be started.

Make sure everything is typed exactly as stated. It's very easy to miss a single letter when re-reading or your work. For me it was the 3lines of code added to the apache config. On the second line, AddType it should read "httpd" I instead had "http" I left out the "d". I'm silly

I've had multiple people complaining that when they try to load http://localhost/test.php they receive a 404 error so I'm consolidating my info. Here is what to try (and feel free to reply to this post so we can figure it out):

1) What folder is your Apache configuration's DocumentRoot set to? please reopen the config file and doublecheck (in my example I used "C:/public_html")

2) Did you put the PHP file in the DocumentRoot folder from question #1?

3) If you put a test.html in the DocumentRoot folder, can you load the http://localhost/test.html file in your browser?

4) What happens if you put the PHP and HTML file into Apache's default DocumentRoot folder? (which is C:/Program Files/Apache Group/Apache2/htdocs/ folder) Do either of them load then?

5) What operating system are you on? I'm starting to suspect this might be only people on Win7.

6) [question added 2011-06-27] If you go into your Apache error log and look at the last few entries (there should be a link in the start menu) does it list the correct file path that you were trying to load? If it's listing the wrong path, what path is it listing? Try to find any matches to a wrong path in the httpd.conf file (to maybe figure out where it might be getting it from).

Hi, I get the same error 404. In answer to your questions:

1) I also used C:\public_html for DocumentRoot
2) yes I put the php file in C:\public_html
3) nope, test.html does not work
4) yes, the html does work in htdocs. but still not the php
5) windows 7. i think you might be right :(

Me too,

I did step 1, 2, 3, 4 correctly. But my window is shown as Window 7 :D

1) "C:/apache/htdocs"

2) I didn't put it in ("C:/public_html") instead in "C:/apache/htdocs" (and this is my default folder)

3) No

4) No, see my #2 answer

5) WinXP sp3

I don;t know if this can help but this is my PHP and Apache version:
PHP : php-5.2.17-Win32-VC6-x86.zip
Apache : httpd-2.2.19-win32-x86-no_ssl.msi

It took me some time now (i.e.days!) and still can't get it all working.
Please any help would be great

Best regard, Rizky

If you go into your Apache error log (there should be a link in the start menu) what do the last couple entries say? Is there any type of error that lists the file path for the file that it was actually trying to load?

Hi Rico thanks for your quick response.
I didn't do any setting change since yesterday but it works now even though the 404 page Not Found error message still rarely occur. But one thing still confuse me is what the cause of my problem? because the only one different thing i do from yesterday is restart my PC.

But then again thank you for your great instructions. It's easy to understand even for a beginner like me.

I honestly don't know. Problems that are seemingly random are the worst. Hopefully enough people who have this problem will post and we can figure it out.

I too was having same problem of 404 but I found the problem was that FILE I had created was test.php.txt

I made test.php file by opening cmd -> notepad test.php
then write
<?php
 phpinfo
();
?>
Then run the file by putting it in the DocumentRoot folder.

Thanks for the help with the 404 not found problem. I too struggled with it. Using cmd to make the php file really helped, because i was using a text file. Restarting is what finally made it work.

I also thought that to restart Apache all I had to do was close the Monitor. Instead, I realized if you double click the tray icon you open a window with a lot of extra options, including the restart button.

I'm new, so bare with me. If I go to my public_html directory and open an HTML file in the browser it works fine. eg;
file:///C:/public_html/test.html
However, if it is a PHP file, it doesn't work. e.g.:
file:///C:/public_html/test.php

The php is just not executed. However, if I open the file using localhost, it works perfectly well.
http://localhost/test.php

This might be useful info for some people. Maybe someone can explain why this occurs though. Other than that, great instructions, best I found on the web.

Any URL that starts with file:// means that the browser is simply opening the file directly off your computer (essentially the same as if you went through and opened the file through "My Computer")
file:// does not use Apache, PHP, or any of that. It's pretty much just a Windows file viewer that you access through your browser.

I have downloaded 'php-5.3.5-nts-Win32-VC6-x86.zip' file and but can not find the file 'php5apache2_2.dll'. There is an other file with the name 'php5apache.dll'. I thought it won't be matter for apache to start the server. But when I added below lines in httpd.conf file it stopped working and giving the error 'The requested operation has failed'

You need to make sure you download the VC6 x86 Thread Safe zip package off the site. The -nts- part of your file name means you downloaded the incorrect Non Thread Safe version which, among other things, doesn't have the required file.

Apache works correctly but can't get PHP to work. I've looked over the past day or so at every recommended fix and it still does not work. I get error "The requested operation has failed"
#BEGIN PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL
PHPIniDir "C:/webserver/Apache2.2/php/"
LoadModule php5_module "C:/webserver/Apache2.2/php/php5apache2_2.dll"
AddType application/x-httpd-php .php
#END PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL

When I comment out the PHP lines, Apache works perfectly.
#BEGIN PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL
#PHPIniDir "C:/webserver/Apache2.2/php/"
#LoadModule php5_module "C:/webserver/Apache2.2/php/php5apache2_2.dll"
#AddType application/x-httpd-php .php
#END PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL
I've tried everything to fix this. Any suggestions?

Run the "Test Configuration" shortcut in the Start Menu and see if it lists anything specific. Also check the bottom of the Apache error log and see if it lists any info on what the problem is.

I am having the same issues as the person above. My apache won't start once I add the lines from #5.
The test and error log say it cannot open because a module in the php folder is missing, but I've checked the php folder, and the module is there.
I'm not sure what to do!!??

To the people who get to the point of adding the below then apache wont start... ( YOU DOWNLOADED THE WRONG PHP)

LoadModule php5_module "C:/php/php5apache2_2.dll"
AddType application/x-httpd-php .php
PHPIniDir "C:/php"

____________

You downloaded this : VC9 x86 Non Thread Safe

But you need this : VC9 x86 Thread Safe <----------- "Thread Safe"

Nice write up.. works fine!
T0rx

I was also trying to create test.php with a text file. It was not working, i was getting error '404'. I created that file
Run->cmd->notepad test.php
it will prompt with a blank notepad. Type the content and save it.
Once saved put it in document folder and it works like a charm.

Very good information on this site.

Thanks

Hello,
I finally fixed my 404 errors, but now when I do the php test, it only displays the code, it doesn't execute. Html is executed fine. I'm running on Vista.

This has been driving me nuts. I gave up on this months ago and decided to try again, but it seems it's just one problem after another. I've tried this at least five times. I've uninstalled and reinstalled, and restarted. I hope I haven't followed the steps wrong that many times.

I really appreciate any help,
Thanks

Are you loading the PHP file with something like http://localhost/filename.php (make sure the address is not like file:///C:/public_html/filename.php because that means Apache isn't handling it)

Doublecheck your Apache config (step 5 above) and make sure the appropriate lines are in there. Also remember to restarted Apache after making any changes to the config file.

Thanks for your instructions - by far the simplest and best available! But I am getting HTTP 403 Forbidden error when I try to show http://localhost/test.php. The error log gives it as: [error] [client 127.0.0.1] client denied by server configuration: E:/public_html/test.php
Please help (and please note that I am a beginner)
Thanks

Is it only php files that are giving 403 errors? or every file in E:/public_html/?

Generally 403 errors for all files are caused by something like:

Order deny,allow
Deny from all
Where you'd want something closer to
Order allow,deny
Allow from all

I get the same error for an html file also. Where would I go to change the [Order allow,deny / Allow from all] you refer to?
Thanks again!

It should be inside either (from step five):

<Directory "C:/public_html">

</Directory>
or there should be a
<Directory "/">

</Directory>

Hello,

i do Step by Step your Tutorial, i downloaded:

PHP: windows.php.net/downloads/releases/php-5.4.7-Win32-VC9-x86.zip

Apache: mirrors.hostingromania.ro/apache.org//httpd/binaries/win32/httpd-2.2.21-win32-x86-no_ssl.msi

I do install Apache, then i do change DocumentRoot do C:/htdcos and Directory too.

Then i go to install PHP, i unpack php in C:/php and i do rename php.ini-development to php.ini, i search for doc_root and add there doc_root = "C:\htdocs".
Then i search for extension_dir = "./", and i add there extension_dir = "ext" without ";".

I open httpd.conf and add :

LoadModule php5_module "C:/php/php5apache2_2.dll"
AddType application/x-httpd-php .php
PHPIniDir "C:/php"

then i add this one:

DirectoryIndex index.php index.html default.html

Then i try to Restart Apache, but Apache no want to Start, it startet Test Configuration hee says me Apache cant load C:/php/php5apache2_2.dll on line 129 at httpd.conf.

Now i dont understand what to do, please i need your help.

Regards

Are you installing this on Windows 7? The PHP version with VC9 usually does not work with older versions of windows (like XP). Other than that, everything you did looks fine and I don't eally have any suggestions.

i do it on a Virtual Private Server , details:

Microsoft Windows Server 2003 R2
Standard x64 Edition
Service Pack 2

I need ur help sir i do run it as Administrator but no chance, only he says cannot load .dll, i do upload a 100% working dll on other VPS but no chance, same..

If you are using Server 2003, I'm guessing VC9 version of PHP will not work. You looks like you will either need to download the older VC6 x86 Thread Safe for PHP 5.2, or use the links along the left side of the download page to install the necessary Microsoft 2008 C++ Runtimes to use the VC9 version.

I understand this tutorial was written a couple of years ago from the time I'm responding, but I wanted to let you know that after having decided to setup Apache, PHP, and MySQL locally (without using an all-in-one package), your tutorial is the only one I've followed that worked 100% error free for me. I'm down with hitting the nitty-gritty config files for the learning experience, but it was nice that you only targeted the necessary changes required to make PHP and Apache function together. Thanks ! *I know this comment isn't especially constructive, so feel free to delete; just wanted to let you know someone's still using your info :)

This tutorial has been a simple yet detailed one to set-up PHP with Apache on a Windows platform. Few thing I would like to share from my experience is that make sure you are downloading same bit version of Apache and PHP. For me on Windows 7, Apache 2.4.4 x86 along with PHP "php-5.4.16-Win32-VC9-x86.zip" thread safe version only worked with each other. Also make sure you modify your httpd.conf to load "php5apache2_4.dll" if you are using Apache 2.4.x. If you miss this analogy, you will run into issues where Apache cannot load this dll and will report incompatibility. Good Luck!

Thank you sooo much for the solution. I had to search for hours until I found your comment on this SIMPLE resolution! Thanks!!

What do you mean you "can't edit httpd.conf"? Do you get a permission error? If so, is it set to read-only or are you trying to edit it from a non-admin user?

Or is your text editor resaving it as httpd.conf.txt name instead? Or something else?