How to Install PHP on Windows

Contributing Editor

Install PHPHopefully, you now have a working local installation of Apache on your Windows PC. In this article we will install PHP 5 as an Apache 2.2 module.

Why PHP?

PHP remains the most widespread and popular server-side programming language on the web. It is installed by most web hosts, has a simple learning curve, close ties with the MySQL database, and an excellent collection of libraries to cut your development time. PHP may not be perfect, but it should certainly be considered for your next web application. Both Yahoo and Facebook use it with great success.

Why Install PHP Locally?

Installing PHP on your development PC allows you to safely create and test a web application without affecting the data or systems on your live website. This article describes PHP installation as a module within the Windows version of Apache 2.2. Mac and Linux users will probably have it installed already.

All-in-One packages

There are some excellent all-in-one Windows distributions that contain Apache, PHP, MySQL and other applications in a single installation file, e.g. XAMPP (including a Mac version), WampServer and Web.Developer. There is nothing wrong with using these packages, although manually installing Apache and PHP will help you learn more about the system and its configuration options.

The PHP Installer

Although an installer is available from php.net, I would recommend the manual installation if you already have a web server configured and running.

Manual Installation

Manual installation offers several benefits:

  • backing up, reinstalling, or moving the web server can be achieved in seconds (see 8 Tips for Surviving PC Failure) and
  • you have more control over PHP and Apache configuration.

Step 1: download the files

Download the latest PHP 5 ZIP package from www.php.net/downloads.php

As always, virus scan the file and check its MD5 checksum using a tool such as fsum.

Step 2: extract the files

We will install the PHP files to C:php, so create that folder and extract the contents of the ZIP file into it.

PHP can be installed anywhere on your system, but you will need to change the paths referenced in the following steps.

Step 3: configure php.ini

Copy C:phpphp.ini-recommended to C:phpphp.ini. There are several lines you will need to change in a text editor (use search to find the current setting).

Define the extension directory:

extension_dir = "C:phpext"

Enable extensions. This will depend on the libraries you want to use, but the following extensions should be suitable for the majority of applications (remove the semi-colon comment):


extension=php_curl.dll
extension=php_gd2.dll
extension=php_mbstring.dll
extension=php_mysql.dll
extension=php_mysqli.dll
extension=php_pdo.dll
extension=php_pdo_mysql.dll
extension=php_xmlrpc.dll

If you want to send emails using the PHP mail() function, enter the details of an SMTP server (your ISP’s server should be suitable):


[mail function]
; For Win32 only.
SMTP = mail.myisp.com
smtp_port = 25

; For Win32 only.
sendmail_from = my@emailaddress.com

Step 4: add C:php to the path environment variable

To ensure Windows can find PHP, you need to change the path environment variable. From the Control Panel, choose System, (then “Advanced system settings” in Vista), select the “Advanced” tab, and click the “Environment Variables” button.

Scroll down the System variables list and click on “Path” followed by the “Edit” button. Enter “;C:php” to the end of the Variable value line (remember the semi-colon).

PHP path environment variable

Now OK your way out. You might need to reboot at this stage.

Step 5: configure PHP as an Apache module

Ensure Apache is not running (use “net stop Apache2.2″ from the command line) and open its confhttpd.conf configuration file in an editor. The following lines should be changed:

Line 239, add index.php as a default file name:


DirectoryIndex index.php index.html

At the bottom of the file, add the following lines (change the PHP file locations if necessary):


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

Save the configuration file and test it from the command line (Start > Run > cmd):


cd Apache2bin 
httpd -t

Step 6: test a PHP file

Create a file named index.php in Apache’s web page root (either htdocs or D:WebPages) and add this code:


<?php phpinfo(); ?>

Ensure Apache has started successfully, open a web browser and enter the address http://localhost/. If all goes well, a “PHP version” page should appear showing all the configuration settings.

See also:

Best of luck!

If you enjoyed reading this post, you’ll love Learnable; the place to learn fresh skills and techniques from the masters. Members get instant access to all of SitePoint’s ebooks and interactive online courses, like PHP & MySQL Web Development for Beginners.

Comments on this article are closed. Have a question about PHP? Why not ask it on our forums?

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • alex.w747

    From my working and installation experience, installing PHP as CGI is better then as module – it does not require modifying environment variables.
    Of course the performance in better when you install PHP as apache module, but for the development time the difference is not significant.

    This very good instruction I’ve created explains how to install PHP as CLI, IIS/FastCGI, and Apache module with different PHP.ini for each of them:

    http://www.witsuite.com/support/knowledge-base/manual-installation/install-php.php

  • http://www.optimalworks.net/ Craig Buckler

    Thanks for the tip, alex.w747. I’ve never noticed any significant performance differences between installing as a CGI or module – especially within a development system. Either should be fine.

    I actually use both: PHP5 as a module and PHP4 as CGI. You can then configure your environment to test the same code in both PHP versions (the subject of another article…)

  • alex.w747

    BTW, with PHP CGI you can use different PHP version in different folders or sites and do not change extension.
    For example you can create PHP4, PHP51, PHP52, PHP53 folders or virtual sites and publish quickly test your application by executing unit tests with different base URLs.

  • Keith

    I love that the article is about installing PHP on windows and the image is of an Apple Macbook. I know you can use bootcamp or any number of other methods for putting Windows on Apple hardware, but it just struck me as funny.

  • http://www.tibobeijen.nl Tybe-O

    In several small companies I’ve deployed similar setups on Windows office servers. Usually those machines are also running IIS, for instance for Exchange web access. If the purpose of the Apache server is to host a development environment one might consider changing the default port to, say, 8080. Look for the ‘Listen‘ directive in the top section of httpd.conf

  • http://www.mikeborozdin.com/ Mike Borozdin

    I prefer installing it a separate package, I mean I can download any version of PHP I want, any version of Apache and any version of MySQL, although I can use any other database I want.

    By the way, PHP works nicely with IIS 7 built-in Vista and Windows Server 2008.

  • Gaitors

    I’ve never had the courage to install php/apache. I’ve read that it must be secured, both php, and apache, if not secured, anyone may have access to your computer. I’ve purchased books to learn how to program php, and mysql. The instructions don’t/have never made sense on how to password secure the information. No one seems able to clarify that information. Does anyone here have the ability to explain how to secure this information to someone who has never done this, and is a rank newbie at it? (And incredibly paranoid about security.. my computer has been hacked in the past)

  • http://www.mikeborozdin.com/ Mike Borozdin

    Just forbid any incoming connections to Apache and MySQL (80 and 3306 ports). May only localhost be able to connect to itself.

  • http://www.optimalworks.net/ Craig Buckler

    @Gaitors

    I’ve read that it must be secured, both php, and apache, if not secured, anyone may have access to your computer.

    No – that’s not true. Apache listens for requests on HTTP port 80 and sends back a response. The only way someone could access it is if you configured your modem/router to forward outside requests to port 80 to your PC.

    Therefore, you’d have to explicitly configure your system to allow outside access. Even if you did, Apache is a highly secure web server and hackers would have extremely limited access.

  • http://www.vantagegaming.net/ ben332211

    One option for getting Apache, PHP & MySQL up and running very quickly is EasyPHP: http://www.easyphp.org/

    I often recommend this to newbies’s looking at learning PHP or just wanting to easily test code locally.

    It’s obviously not a good option for power users or for running a production server, but is a useful tool that does exactly what it claims, :)

    Best Wishes,
    -Ben

  • Robert K

    Thanks for this, Xampp is especially easy to get running and comes with phpmyadmin for mysql too. Very useful tool! I remember the old days of php <4 when I had to manually configure it – a good guide is needed and this fits the bill :)

  • Dragos Rusu

    Quick info: on Windows XP you might need to copy “php5ts.dll” into the windows folder for all extensions to work; no idea why this hack is needed

  • predat0r

    Hi,
    I set env.var, restarted computer, but I can’t run php from my dev. folder. Any idea why?
    I use Win7, wamp, so the path is: C:wampbinphpphp5.3.5

  • srini

    Great Article

  • Ransky

    Your step by step instructions worked great; very clear and accurate. Thank you!