Himesha's Blog

Tuesday, August 01, 2006

Configuring Apache-PHP-MySQL on Windows XP

Introduction

When you install PHP in "C:\PHP" as default you will get a 403 forbidden error when trying to run the PHP scripts with Apache web server in Windows XP.
This article presents a solution for that problem.

Configuring PHP 5+ in Windows as CGI with Apache
  • Install Apache web server.
  • Check whether it's working. If works outputs, "It Works!" in the browser when you access "http://localhost" or if you set a port other than 80. "http://localhost:YourPortNumber".
  • Extract PHP zip file into "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\PHP" not to "C:\PHP". If you run a self installer package PHP will be installed as "C:\PHP". Then copy the PHP folder into the "htdocs" folder in Apache.
  • Copy "php.ini-recommended" file in the "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\PHP" folder into "C:\Windows" folder. (If you've installed PHP using an installer package check whether there is a file named "php.ini" in the "C:\Windows" directory. If so not necessary to add again.)
  • And rename it as "php.ini".
  • Then you must insert the following lines to the "httpd.conf" file in folder "C:\Program Files\Apache Software Foundation\Apache2.2\conf" to run PHP as CGI.
    ScriptAlias /php/ "c:/Program Files/Apache Software Foundation/Apache2.2/htdocs/php/"
    AddType application/x-httpd-php .php .php3
    Action application/x-httpd-php "/php/php-cgi.exe"
  • Now write a simple php program and test whether php script is working.
    Eg. Save the following script as "phpinfo.php" in the "htdocs" folder.
    <html>
    <body>
    <?php
    phpinfo();
    ?>
    </body>
    </html>
    Then load it by typing "http://localhost/phpinfo.php"
    If you see the php configuration info you've successfully configured PHP with apache.
Enabling MySQL with PHP 5+
  • Open the "php.ini" file in the "C:/Windows" directory.
  • Find a text as "extension_dir" in the "php.ini" file.
  • Replace the line with "extension_dir = "D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\PHP\ext"".
  • Then find the text "extension=php_mysql.dll" in the "php.ini" file. If it's commented (That is if there is a semicolon(";") before the text), remove the semicolon to uncomment it.
  • Now you have enabled MySQL in PHP.
  • So now you can link to the MySQL databases.

Other Important Things

  • Turn “register_globals = On” and register_long_arrays = On” in the “php.ini” file to handle “get” and “post” variables.