Installing PHP on Windows
By Michael Ross
This article was published by DeveloperTutorials.com, 2008-03-05.
A brochure-style website may be sufficient for a business that simply wants a basic Web presence — some static pages to inform the visitor of the company's off-line contact information. But most organizations want a website that can interact with the visitor, delivering custom content in dynamically-generated pages, and storing visitor information in a database. Such a website requires several technologies: a Web server, a database system, and a scripting language for creating the Web pages. Currently the most popular choices are Apache, MySQL, and PHP, respectively.
Your PC can be turned into a fully capable Web development environment, by installing those three technologies, and configuring them so they communicate with one another. Admittedly, you can simply install and use PHP for its command-line processing. But for creating and testing dynamically generated Web pages on your local machine, then Apache needs to be already installed and working.
In this tutorial, we will go through the steps to install and configure PHP — as well as phpMyAdmin, an invaluable MySQL management application — on a Windows PC. We will assume that you already have the latest version of the Apache Web server installed and working. For detailed instructions on how to set up Apache on a Windows machine, please see my earlier tutorial, "Installing Apache on Windows".
You should install Apache before installing PHP, so the former will be in place when referenced during the PHP installation.
PHP Already Installed?
If this is not the first time that you have installed PHP on this particular partition on your PC (since it was created), then you may have a leftover instance of PHP. In that case, it would be best to locate that old version. Check in the directories that you typically use for applications, such as the default Windows installation directory, "C:\Program Files".
If your PC does have an earlier version, then you may wish to save the PHP configuration file, php.ini, before removing the leftover version of PHP. Even though you will be creating a new configuration file — and we will cover that later in this tutorial — it is usually helpful to be able to refer to any customizations you had made in the past. Note that the file is not necessarily stored in the PHP installation directory, as you might expect, but could be anywhere on disk. Most installation guides direct readers to place that configuration file in C:\WINDOWS, so it may be found there.
Delete the old PHP installation directory, or directories if you tried multiple versions of PHP. It is always safer to start with a clean install, so you can be completely sure that any difficulties are not being caused by leftover files that are incompatible with the new version that you are trying to install.
We will be using the latest stable version of PHP, which at this time is version 5.2.5. You should download it from the appropriate section on the PHP download page.
You have the option of downloading the full source code for PHP, in case you would like to compile yourself or examine the code. But for Windows development environments, the Windows binaries is the recommended option, to avoid wasting time compiling PHP from source and possibly avoid problems that might arise during that process. For the Windows binaries, you have the option of downloading either a Zip archive file or a larger file with a built-in installer. Anyone experienced with setting up PHP would likely have no problems using the archive file. But for anyone new to installing PHP, it would be best to use the installer version, which is what we will do here. The download section also lists the MD5 value for those who wish to confirm the integrity of the file they download to their computer.
When you click on the "PHP 5.2.5 installer" link, you will be taken to a page where you can choose a mirror site in your country or one that is close to you. Clicking on the mirror link will start the download process. Save the file, php-5.2.5-win32-installer.msi, to a place on your PC where you can easily find it. Then open the file, which will start the PHP Setup Wizard.
At the welcome dialog box, click the "Next" button.
At the license dialog box, check the radio button indicating that you accept the license, and then click the "Next" button.
By default, Apache will be installed in "C:\Program Files\PHP". But in this tutorial we will be using "C:\_a\PHP". The reasons and advantages for this directory naming scheme are presented in my earlier tutorial, "Installing MySQL on Windows".
To change the destination folder, click the "Browse" button. When you're finished, click the "Next" button.
In order to be able to serve Web pages, PHP, like any Web scripting language, needs to work in conjunction with a Web server. If you have followed the instructions in the aforementioned Apache tutorial, you will have already installed Apache version 2.2 on your PC. At the "Web Server Setup" dialog box, click the "Apache 2.2.X Module" radio button, and then the "Next" button.
The PHP Installation Wizard needs to be told where to find your Apache configuration files, of which httpd.conf is the primary one. After you have specified the correct directory, click the "Next" button.
In addition to the core PHP files, you can also install any of a large number of available extensions, ranging from extensions for ActiveScript to zlib_filter. In this example, we will install extensions for the following: cURL, a client URL library (incorrectly listed under "Curl"); the GD image library ("GD2"); mbstring, for multibyte character sets; MySQL and MySQLi, for accessing MySQL databases. Note that the disk space usage numbers are incorrect. For instance, it claims that cURL will consume "1504KB" of space, but in reality the cURL extension DLL file, php_curl.dll, is only 233 KB.
You can also install the PHP Extension and Application Repository (PEAR) and the PHP manual — both listed under the "Extras" node. We will not be installing either one in this tutorial. After you have made all of your selections, click the "Next" button
You are now ready to install PHP's files to your disk. Click the "Install" button, which begins the process.
When the Setup Wizard has completed, it will display a final dialog box.
Click the "Finish" button. You are now ready to verify that the installation was successful.
If you check your Apache configuration file, httpd.conf, you will find that the PHP Setup Wizard made some changes, after creating a backup copy, httpd.conf.bak, in the same directory. Specifically, at the bottom of the file, it added the following:
#BEGIN PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL PHPIniDir "C:/_a/PHP/" LoadModule php5_module "C:/_a/PHP/php5apache2_2.dll" #END PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL
To make these Apache configuration changes take effect, restart your Apache Web server. This can be done in Windows services (Start > Settings > Control Panel > Administrative Tools > Services), or from the Apache program group menu (Start > All Programs > Apache HTTP Server 2.2 > Control Apache Server > Restart).
To verify that PHP is working fine with Apache, create a simple PHP script:
<?php phpinfo(); ?>
Name it phpinfo.php and place it somewhere within your Apache document root (in our example, "C:\_a\Apache\htdocs"). Then run the script in a Web browser by going to the address http://localhost/phpinfo.php. You should see a Web page whose upper portion looks similar to the figure below.
Browse through the rest of the phpinfo() output. You'll find that it is a wealth of information about your Apache server, including environment variables, as well as PHP settings and global variables. Note that there is no mention of MySQL, because we have yet to connect MySQL to PHP.
This final section of the tutorial is for those readers who have installed the MySQL database server. For detailed instructions on how to add MySQL to your development environment, please see my tutorial titled "Installing MySQL on Windows".
In order for MySQL to be able to communicate with PHP, you need to add some dynamic link library (DLL) files to your Apache setup. First verify that the extensions directory for PHP — in this tutorial, "C:\_a\PHP\ext" — contains the two needed DLL files: php_mysql.dll and php_mysqli.dll. Then confirm that the following lines have been added to your PHP configuration file, C:\_a\PHP\php.ini (probably at the very bottom), by the PHP Setup Wizard:
Next, copy the DLL file libmySQL.dll from C:\_a\MySQL\bin to C:\WINDOWS\system32. Restart your Apache service.
To verify that everything works, in your Web browser go to http://localhost/phpinfo.php, which should now contain two new sections, labeled "mysql" and "mysqli". With the heading "MySQL".
MySQL Versions of DLLs
On the MySQL website, the "Download Connector/PHP" page offers their own versions of the three aforesaid MySQL DLLs. The page states that their versions should be used instead of the ones available from the php.net site — presumably, the versions that are part of the installation packages. Apparently the mysql.com versions are based on the latest MySQL client libraries, unlike the php.net versions. The page claims that mysqli_stmt_bind_param() fails when you try to access a DECIMAL column on a MySQL 5.0.27 server, if you use php.net's files. Perhaps that has been fixed with MySQL 5.0.51a and PHP 5.2.5, because a simple INSERT test shows that mysqli_stmt_bind_param() can access DECIMAL columns without any problem.
Regardless of which set of DLLs you use, once they have been put into the directories specified above, the "extension" lines have been added (if not already done so by the Setup Wizard), and Apache has been restarted, then all three of the primary elements of your Web development environment — Apache, MySQL, and PHP — should be interoperable.
For some developers — especially those with limited experience using these technologies — installing Apache, MySQL, and PHP, and getting all of them to work together, can prove daunting. This tutorial, in conjunction with the other two, should alleviate much if not all of a beginner's anxiety. Nonetheless, someone may still have problems getting everything working properly, or may simply want to install everything at once, to save time.
Intended to meet this need, XAMPP combines Apache, MySQL, PHP, and Perl, already bundled together, and working together. Like all of its individual components, XAMPP is free and open-source. It is available for Linux, Windows, Mac OS X, and Solaris.
This is one of several bundled packages available, most of which offer some sort of control panel which allows you to stop and restart the individual components, and to administer them if needed. This can save you time and frustration getting started. On the other hand, they typically are not set up for maximum security, and veteran developers oftentimes do not care for the preset choices.
Regardless of whether you install PHP individually or as part of a package, you will no doubt find that it is an extremely capable language — particularly for creating Web-based applications and sites that need to change dynamically and communicate with database systems such as MySQL.
Copyright © 2008 Michael J. Ross. All rights reserved.