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
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
|
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.
|
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.
|
- Unzip php. In my case, I unzipped to C:\php\
- Rename C:\php\php.ini-recommended to php.ini
|
VC9 will use the file php.ini-development instead of php.ini-recommended
|
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 will have ;extension_dir = "ext" so make sure you remove the ; from in front of it also.
|
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.
[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>
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();
?>
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
Installation of PHP after Apache's installation
I' ve made the changes in c:\php\php.ini file (ex php.ini-developer) and to the c:\programfiles\Apache Software Foundation\Apache2.2\conf\httpd.conf file, but after all Apache Service is refusing to start, prompting "The requested operation has failed!"
Pls forgive my ignorance and thank you for your time!
by the way test.php is located in "C:\public_html" directory
[I use VC9 with Win7]
Answer
Run the "Test Configuration" shortcut in the Start Menu to find the error. If the text window pops up then closes before you can read it, your config file is fine and try looking in the Apache error log (there should be a link to it in your start menu).
Edited the files so php and apache would work
And this is the error message I got when I tried to do the 'test.php' thing:
Not Found
The requested URL /test.php was not found on this server.
Any information is appreciated, Thanks!!
Answer
Also already addressed down in the comments Error: 404 Not Found when loading test.php
Apache conf file not opening.
In my case, I cannot edit my Apache conf file. It gives me message that access to change the file is denied. Is there another way to work it out? Thanks!
Access denied
I fixed this
Answer
Try the very first (bottom) post from the Apache page.
Question: Zip file does not contain php5apache2_2.dll
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'
Answer
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.
Question about file:// vs http://
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.
Answer: file:// vs http://
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.
Error: 404 Not Found when loading test.php
I seem to be having a problem with the last step though. When I try to open http://localhost/test.php, I get a 404 Not Found page, although I have followed your guide to a letter.
Answer: What to try (feedback wanted)
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).
What worked for me
I made test.php file by opening cmd -> notepad test.php
then write
<?phpphpinfo();
?>
Thanks a lot to ricocheting
Thanks a lot to ricocheting and the guys 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. Also restarting is what finally made it work.
Taylor
Restarting Apache
In my case, I was really stupid. I 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.
Thanks ricocheting, great articles :)
XP User
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
Answer
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?
It works
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.
Answer
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.
feedback
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 :(
feedback
Me too,
I did step 1, 2, 3, 4 correctly. But my window is shown as Window 7 :D
PHP file tries to download instead of displaying
When I try to access http://localhost/test.php it seems like the server still doesn't recognize the php file. It tries to download the file instead of displaying it in the browser. I believe I followed all steps correctly, but any advice as to what could be the possible cause of this.
Php downloads
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
Answer: PHP file trying to download or displaying PHP code
The behavior you are describing usually occurs when Apache hasn't been restarted after you add the LoadModule and AddType info from step #5. Without the restart, Apache won't know how to process a PHP file so it handles PHP like a generic unknown file.
Error: The requested operation failed!
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!
Answer: Apache's Test Configuration utility
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.