Installing Apache, MySQL and PHP on Windows

This post is primarily for my benefit, to document a process I have gone through many times before, but hopefully it might be useful to some other people who want to use AMP (Apache, MySQL, PHP), but are forced for whatever reason to use it with Windows rather than the Linux. If you spot any mistakes or omissions, please let me know in the comments.

Before we start this process, if you are just after a simple install of the “AMP Stack” on Windows, you might want to check out one of the excellent bundles such as WAMP Server and XAMPP. These make it very easy to do everything I describe in this post through a simple install Wizard. So why install them separately? Mainly because it keeps you in control and helps you to understand how the 3 components interact. The packages are great to get you going, but if you haven’t installed and configured these applications yourself, it is difficult to diagnose and fix problems when they inevitably occur.

Apache

  1. Download the Apache .msi
  2. Install for All users on Port 80 as a Service (this means it starts automatically when any user is logged in)
  3. Give a suitable Admin email address, Network Domain and Server Name can remain as default unless you have reason to change. (If this is just a development machine, you can leave this as default).
  4. Choose Typical installation
  5. If installation appears to have been successful, type either localhost or 127.0.0.1 into your web browser and it should say “It worked!”

Document Root

The Document Root is the directory where all the files you want to be accessible to your web visitors go. Nothing outside of this directory is accessible to users through a web browser. By default on Windows this directory is in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs but you may want to move it somewhere else either for simplicity or to make backups easier. If you wish to do this, open httpd.conf, search for the DocumentRoot line and change to wherever you want it to be. Important Note: Any changes to httpd.conf require a restart of Apache before they take affect. To do this, double click on the Apache icon in the task bar and click Restart. MySQL

  1. Install MySQL .msi (32 bit Essentials version, unless you have good reason to choose otherwise!)
  2. Choose Typical Installation
  3. Choose “Configure MySQL Server now” on the last part of wizard Most of the settings can remain as default unless you have reason to change, however make sure that sufficient concurrent users are allowed, and that it is set to run as a service. If this is a public web server, ensure a secure root password is created when asked. If this is a private development machine, you can untick the modify security settings box and the root account will have no password.

Optional Step: Install SQLyog community edition. This is a great database management tool, it allows you to create databases, browse them and query them. It is invaluable for administrating databases, but it is not required on the Server, it can be installed on your local machine and connect to the database installed on the server. PHP

Download the VC6 Threadsafe version from the PHP Windows download page. Although there are MSI versions of PHP that in theory install everything for you, I have always found it easier and more reliable to use the manual installation method described here.

  • unzip to C:/PHP (could be anywhere, but update path references below if you choose a different location)
  • Change either php.ini-development or php.ini-production (depending on the purpose) to just php.ini.
  • make the following alterations to c:/php/php.ini. Search for these lines, alter as shown or if not present, add them. (Some or all of them may be present, but “commented out”, with a ; character at the beginning of the line.

Configuring PHP

PHP comes with a number of extensions. You won’t need them all, and shouldn’t activate them unless you need them, but you will certainly want the MySQL drivers. Below are my suggested minimum changes to your php.ini file:

short_open_tag = On
max_execution_time = 60
upload_max_filesize = 10M
auto_detect_line_endings = On ;(makes it compatible with text files created on mac/unix)
extension_dir = "C:\php\ext"
extension=php_mysql.dll
extension=php_mysqli.dll

Most of these configurations will already exist in your php.ini, so do a search for them before adding them. Some may be “commented out” which means the line is not active, you can tell if this is the case if the line starts with a semi colon. Removing this semi colon will activate this line. Normally after changing php.ini you will need to restart Apache before you benefit from the changes, but at this point we haven’t configured Apache to use PHP, so it makes no difference. We’ll get onto that now. Configuring Apache

The final stage of this process is to configure Apache to use PHP. There are two changes you need to make, both in the httpd.conf file which should be in the following directory: C:\Program Files\Apache Software Foundation\Apache2.2\conf\

First add these lines to the file:

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

The first of those lines loads the php module, the next one registers php files as a valid type for Apache to load, the final line sets the directory where the php.ini file can be found. For the next step, search for the directive DirectoryIndex and add index.php to the list. It should end up looking something like this

DirectoryIndex index.html index.htm index.php

It may have more file types to it, you can leave them or keep them. These are the file names and extensions that you want Apache to load up by default when someone navigates to a directory on your site not an explicit page. They are prioritised from left to right, so if more than one index page is found it will load the left most listed one. With this in mind, you may wish to put index.php first, but there is probably no need to.