loading comments

Creating a localhost in Windows (Part 2: Installing PHP 5)

PHP logo

In part one we setup a Apache server and configured the localhost ready for you to use, we also set up the directory where you are going to save your local files. But so far in part one we only essentially setup the localhost, so all of the goodness of being able to view server side documents/scripts etc won’t work yet as you will need to install them, one of the most popular if not the popular server side code is PHP. It is used mostly to create dynamic webpages. Lets install PHP!

Before we begin if you missed part one of creating a localhost in windows go ahead and click here to view it in part one I showed you how to set up Apache and get the localhost running, once you’ve set that up come back here to install PHP!

Step 1: Download the PHP Binary

Remember when I said not everything will be in a installer? Well my statement really shows here, to install php properly on Apache we will be doing it via the binary which means downloading PHP 5 from PHP.net and setting it up manually. First off head obtain the php 5 binary which can be found by clicking here. You will want to find the Windows Binaries section and download the latest zip version of PHP. Select a download mirror and the download should start. All of the binary files are in a zip archive so you will need some unzipping software to get into the file. I’d recommend downloading a free program like WinRaR but Windows does have it’s own unzipping tool built in.

Step 2: Placing the PHP 5 Binaries in your document root

Once you’ve downloaded the php 5 binaries and unzipped the archive, you will now need to place all of the contents of the zip archive into your document root. Well I say document root but, you don’t have to. If we go back to Part one of the tutorial you should remember the part where we defined the document root of where all of the webpages, files etc will be. To keep it simple I used my computers C:/ drive as the document root and within the C:/ created a folder called localhost and the within it created a folder called public_html. All of my localhost files, webpages etc will be within the public_html folder, and apache knows to look there as I defined it as the document root. If you need a bit of refresh about the document root click here to go back to part one and read step 4

What I’ve decided to do is create a folder called php in my localhost folder, but not within the public_html. This is simply to keep all my binaries and files organised and away from my webpage files. So the location is C:/localhost/php and then extract all of the contents of the zip archive into the php folder located on the C:/ so now within the php folder there are all the binary files.


extract

It should be extracted so it looks like this:


php-structure

Step 3: Your PHP.ini file

When you look at your PHP binary files look for a file called php.ini-dist by default this file has had it’s ini status removed to avoid problems during install, but what you need to do if go ahead and removed the -dist part of the filename so you are left with php.ini much like the Apache httpd Conf, the php.ini file is the configuration of php and we’ll need to do a bit of editting to get it to work correctly on our localhost. Edit the php.ini file in any editor and scroll down about halfway to find this part of the config:


doc_root

Like I was starting to reference in Step 2 the document root will be relative to whatever you set the document root to in the Apache httpd conf, so for me it would be:


doc_root = "C:\localhost\public_html"

About 10 more lines down you will also see this line:


extension_dir = "./"

You will need to also change this to where your PHP binary files are located for me it would be:


extension_dir = "C:\localhost\php\ext"

Step 4: Error Reporting (Optional)

As you know a localhost has many uses, but one major use is the ability to test out scripts and dynamic content, so if your going to be doing a bit of testing then I suggest you turn on something called error reporting. This is a feature in PHP that gives off errors when there are mistakes in PHP code, by default this feature is not enabled so if a PHP script had errors no message would be displayed to help you fix it. This step is optional but I’d recommend doing it. To enable error reporting you will need to find this line in the php.in file:


error_reporting

By default this is what will be set:


error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT

Change it to:


error_reporting = E_ALL | E_NOTICE | E_STRICT

Now error reporting is enabled!

Step 5: Adding the PHP Module to Apache httpd conf

Apache doesn’t know that PHP is installed yet, as the module is not being loaded in the apache conf. You will need to add the module into the configuration file. Remember to edit the Apache configuration file you go to Start > Programs > Apache HTTP Server 2.2 > Configure Apache Server > Edit Configuration file

You will then need to add this to either the very beginnning or the very end of your Apache Conf, I’d recommend placing it at the very beginning above all the other modules (Just look for LoadModule in config to see the placement area):

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

However if you not use the same document root etc you will need to edit the two lines:


LoadModule php5_module "C:/localhost/php5apache2_2.dll"

And


PHPIniDir "C:/localhost/php"

To your document root. Make sure you change both!

Step 6: Testing PHP

Before anything else you must restart your Apache service has you have added in a new module and a restart is required for Apache to load the new module and to realise it’s there. Remember to start, stop or restart the Apache service you can either right click on the tray icon and select open apache monitor:

tray icon

Or go to Start > Programs > Apache HTTP Server 2.2 > Control Apache Server and select the restart command. If you edited the httpd while the service was stopped then you will simply need to start the service and the new module will be loaded in (Starting the service serves a similar purpose to a restart)

To test if PHP is working go ahead and create a document called php_test.php and place this code within it:

<?php
phpinfo();
?>

Save it within your document root, mines C:/localhost/public_html and then view it. To access the file I would go to: http://localhost/php_test.php in any of my internet browsers. If you see a table with php services listed then PHP is working on your localhost! You can now view php scripts and dynamic pages locally!

In Part 3 of creating a localhost in Windows we will be installing MySQL, which is a relational database management system which will allow us to easily create databases. Giving you the ability to create databases on your localhost

I'm James, I'm 18 years old and I'm a freelance website developer from Nottingham, England. I have a passion for website development and have designed websites for a range of clients in my freelance years. James' Blog is my personal blog where I post articles and general posts whenever I can. I typically like talking website development, but you'll find a range of posts and articles here on my blog.

  • http://www.php-blog.com/2009/02/07/creating-a-localhost-in-windows-part-2-installing-php-5/ Creating a localhost in Windows (Part 2: Installing PHP 5) | PHP-Blog.com

    [...] original post here: Creating a localhost in Windows (Part 2: Installing PHP 5) Related ArticlesBookmarksTags PHP PHP is a computer scripting language. Originally [...]

  • http://www.james-blogs.com/2009/02/15/creating-a-localhost-in-windows-part-3-installing-mysql-5/ James’ Blog » Creating a localhost in Windows (Part 3: Installing MySQL 5)

    [...] we dive in, make sure you have read part one and two of this tutorial series, you will need to have done everything in both of these parts for you to be [...]

  • http://www.james-blogs.com/2009/02/18/creating-a-localhost-in-windows-part-4-installing-phpmyadmin/ James’ Blog » Creating a localhost in Windows (Part 4: Installing phpMyAdmin)

    [...] 5) I…James’ Blog » Creating a localhost in Windows (Part 4: Installing phpMyAdmin) [...] we dive in, make sure you have read part…James’ Blog » Creating a localhost in W… I have a copy of Win 95 right through to…Andrew [...] we dive in, make sure you have read [...]

  • Mel

    Nicely done! This was a very easy to follow and exactly what I was looking for, a local server for development. Thanks!

  • LivinDeadGurl

    Hey! Awesome tutorial.. working on #4 now ;)

  • http://infoeduindia.com/2009/06/06/creating-a-localhost-in-windows-part-2-installing-php-5/ Creating a localhost in Windows (Part 2: Installing PHP 5) – Tutorial Collection

    [...] View Tutorial No Comment var addthis_pub=”izwan00″; BOOKMARK This entry was posted on Saturday, June 6th, 2009 at 7:32 am and is filed under Php Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. [...]

  • Tshepo

    After adding this in the config file, I could not restart Apache

    LoadModule php5_module “C:/localhost/php/php5apache2_2.dll”
    AddType application/x-httpd-php .php
    PHPIniDir “C:/localhost/php”

    Does anyone know what would the problem be?

  • http://www.james-blogs.com James

    Have you tried fully stopping the service and then restarting. If not you might have a error in your conf.

  • Tshepo

    I did stop and restart Apache then got an error that apache failed. I then googled the problem around but could not get a convincing answer. Would you know where the problem may be? What information can I give you to help me locate the problem? Do you know if WAMPSERVER will help?

  • http://www.james-blogs.com James

    The only other issue I can think of is how you’ve placed the php module in the apache conf. If you haven’t already try adding the php module code after this part of your Apache httpd:

    # Dynamic Shared Object (DSO) Support
    #
    # To be able to use the functionality of a module which was built as a DSO you
    # have to place corresponding `LoadModule’ lines at this location so the
    # directives contained in it are actually available _before_ they are used.
    # Statically compiled modules (those listed by `httpd -l’) do not need
    # to be loaded here.
    #
    # Example:
    # LoadModule foo_module modules/mod_foo.so
    #

    LoadModule php5_module “C:/localhost/php/php5apache2_2.dll”
    AddType application/x-httpd-php .php
    PHPIniDir “C:/localhost/php”

    Remeber if your using a different directory structure to the tutorial example you will have to update it to your directory path accordingly

  • Tshepo

    Manged to solve it! The problem was that I did not change the Directory in the httpd file to point to “C:/localhost/public_html”. Maybe if you can make the tutorial a bit clear on that part.

    thanks for your promp responses

  • http://www.james-blogs.com James

    Glad you have solved it! Im sorry that my tutorial was unclear for you.

  • http://www.oklahomaaquariumclub.org Joe

    James,
    great tutorial(s) btw! Making it a lot easier for me to pick up joomla..
    I am having difficulty installing the php on my local host however.
    I created the doc “php_test.php” and added the code above:

    as well as edited the apache httpd document accordingly (from your answer above)
    yet, I get a 404 Not Found error when I try to load it in a browser.
    The respondent above mentioned something about using the C:/localhost/public_html – i followed each of your instructions to the letter (copy/pasting) and don’t believe I’ve missed adding that line anywhere, but may have.
    Any suggestions?
    TIA
    Joe

  • http://www.james-blogs.com James

    Sorry Joe, I have been inactive on my blog for a while. A very strange issue you have there. If you are getting 404 Not Found errors and your sure the you are requesting a page that exists, if so there could be something wrong with your Apache configuration. Perhaps your document root is set incorrectly?

    Also what operating system are you running? This tutorial was written for Window XP and below as Windows Vista was not the standard and Windows 7 was just an idea at the time when I wrote the localhost series. I’ve noticed that people using this localhost guide on newer operating systems will have problems. I plan to update this localhost series with a newer version soon!

  • Sachin

    Hey brother, I m getting error 403 Forbidden. Please help me !

  • http://www.james-blogs.com James

    What throws the 403 error?

    Did you update your directory index (explained in part 1)

  • http://smellofearthafterrain.blogspot.com/ rainboy

    http://i56.tinypic.com/24n0cn7.jpg

    this is the error i get :( help here …

    I used this library php-5.3.3-nts-Win32-VC9-x86 :(

    HELP!!!!
    mail me at eatit007@gmail.com

  • http://Military Military Medals

    Nice Post…

    [...]I saw this really great post today. I linked back to it from my site. Thanks![...]…

blog comments powered by Disqus