Installing PHP 5, Apache 2 and MySQL 5 on Windows

Menu
Downloading the software
Installing MySQL
Installing PHP
Installing Apache
Setting up PHP as a Apache module
Tools for MySQL

Downloading the software

First of all you need to download the latest stable version of all three products.

For PHP go to the php.net download page and get the zip package from the windows binaries section (I'm working with PHP 5.2).

What follows next is not true (ie: outdated). You can now download the latest 2.2.x version of Apache and it will work without any problems, but some file paths and structures are changed so you may have some trouble. I will need to update this, but I will only do it if I will receive any requests (by mail or otherwise).

Now to get the http server, go to the download page of the Apache project and click on the latest 2.0.x version which has an MSI installer (now it's apache 2.0.59). I'm not using the latest Apache version (from the 2.2 family) because of a simple reason: it doesn't work with the current stable version of PHP.

The last download will be the database server. Go to the downloads page and click on the current release (here it's MySQL 5.0). For (the 32bit version of) Windows there should be three options: Windows Essentials, Windows and Without installer. You should download the middle option (Windows) which has an installer.

Installing MySQL

Unzip the downloaded archive and double click the setup.exe file. Click next on the first window and you should now have 3 options: typical, complete and custom. Select the typical setup, click next and then install.

If everything went ok you should have a page asking you to login or create a new account on mysql.com. Select skip sign-up and then next. On the next window, check the Configure the MySQL Server now checkbox (actually it is checked by default) and press finish.

Installing MySQL is now over, the only thing left is to configure it.

You should see a page welcoming you to the configuration wizard. Click next and choose the detailed configuration and press next again.

Select the developer machine option and click next again. On the next window pick the non-transactional database only and click next again. The next window will ask you for the number of the concurrent connections to the server and the default will do so press next again. The default settings (both checked) for the networking options should also be ok (you have to make sure that the 3306 port si available - that is you don't have another server running on that port) so press next. For the character set I recommend you to make UTF8 the default character set, but if you think you aren't going to need it you can make your own choice - in any case, press next.

The next, windows-related, settings are a bit more important. You have the option to install MySQL as a service, which is the recommended way, but if you didn't install apache as a service, you'll probably also want this unchecked. If you want it installed as a service choose the MySQL501 service name. You can also select if you want the MySQL server to start when Windows starts (this is how I have it). Also, I recommend checking the second option, because this will allow you to type (for example) mysql -uroot -p instead of C:\Program Files\MySQL\MySQL Server 5.0\bin\mysql -uroot -p.

The last step gives you these options:

Press next and then execute to start the installation. You should see some messages detailing how everything went ok and after you press finish, you're done.

As with apache, if you have a firewall, you will probably be asked to allow mysql access to the internet - you must choose allow if you want it to work.

To test if everything is ok, you can start a command prompt (click Start, Run, type cmd and press enter) type mysql -uroot -p (if you haven't changed the password for root you don't have to type the -p parameter and you will be logged in automatically) press enter, type your password, press enter again and you should be logged in - type quit to exit.

Installing PHP

Unzip the archive you've downloaded from php.net in a directory called C:\php5. I suggest creating the folder first, copy and paste the archive in it and unzip there (this is because the archive doesn't have a containing top level folder). You can delete the archive from the folder afterwards, but it's best to keep a copy until you've finished with everything so in case something goes wrong you still have a backup without having to download it again.

Rename the file C:\php5\php.ini-dist to C:\php5\php.ini and open it with your favourite text editor. Search for extension_dir inside the file and set it's value to C:/php5/ext. Now, search for Windows extensions and you'll find a list of extensions which you can activate in order to be able to use. For this setup I'll recommend you uncomment (that is remove the ; at the start of each corresponding line) only two lines: the one which features the mbstring extension and the one that features the mysql extension. You can uncomment/enable as many extensions as you need, but sometimes your Apache server might not start anymore because of one extension or another.

There are two extensions (actually three if you count PDO - and I'd advise you to take a look at it) that allow you to work with a MySQL database: MySQL and MySQL Improved. As the name suggests, the latter extension is better and offers more features, but it only works with MySQL servers version 4.1 or more. In order to activate the MySQLi extension, you'll need to duplicate the mysql.dll line in your php.ini file and add an i to mysql: extension=php_mysqli.dll If you are sure you don't need to work with older servers, you can remove the mysql line completly.

Note: You'll probably notice there is a file called php.ini-recommended in C:\php5. This is, as it's name suggests, the recommended file to be renamed to php.ini, because the settings in it are designed for safety and performance, but this is recommended for a production server. For development (and for beginners) I think it's better to use the dist file, as it will save you from a few headackes until you learn about all the settings in that file.

Installing Apache

Double click the msi file downloaded, then click next, agree with the license, click next again and you should see a page named Server Information.

Next you can replace yourdomain with your domain or, if you don't have one, you can type anything you want.

I've checked the radio button which installs apache as a service for all users on port 80, but you can choose the other option...if you have some reason for it.

Now click next, let the typical checkbox selected, click next again, pick a location where the server should be installed (I'll assume it's the default location in the rest of the tutorial), click next again and install. You should see apache installing, eventually some command prompts appearing, but in the end you should see the message Installation wizard completed and in the system tray a white circle with a green triangle in it. Press the finsh button and you're done.

In case you have a firewall installed, it is probable you will get a pop-up asking you to allow or deny the apache service access to the internet. You must click allow if you want to use the server, otherwise it's useless. In case you've pressed deny by mistake, go to the firewall control center and change the settings.

The icon in the sistem tray can be used to start, stop and restart the server (just click on it and you will see the options).

To test if everything is alright, fire up a browser (no, Internet Explorer is not a browser, go get firefox), type localhost in the address bar and hit enter - you should see a page telling you the installation was successfull.

If the installation didn't go so well, you probably have another server installed (like IIS) or something (Skype is an example) is blocking port 80 - you will have to uninstall everything and then try again (or you could try and change their default port).

Setting up PHP as an Apache module

Now that the easy part is done, let's focus on the most "complex" part of the operation, "connecting" php and apache.

From now on I'll consider you've installed Apache in the default location, that is C:\Program Files\Apache Group\Apache2 and PHP in C:\php5. If you've installed them in different directories everything will remain the same, but you'll have to change the paths in the examples below.

First let's do PHP, because it's only one step. You have to make sure the dll files in the PHP distribution are found by the Apache server, so you'll have to add the directory where you've installed PHP to the PATH environment variable. To do this, right click on My Computer, click on Properties and go to the Advanced tab. Here you must click the button labeled Environment variables, which will bring up a page containing two lists of variables: user variables for the current user, in the upper part, and system variables in the lower part. In the system variables list you must scroll until you find the variable Path, select it and the click Edit. Be carefull NOT to change anything, only add ;C:\php5 at the end of the Variable value field. Then click OK, OK on the System variables page and once again OK in the properties window. That should do it, the only thing left is to configure Apache.

The main configuration file for Apache is C:\Program Files\Apache Group\Apache2\conf\httpd.conf and here you'll make most of the changes. So open the file in your favorite text editor (if you don't have one I recommend Crimson Editor) and let's start setting things up.

If you scroll down a little you'll notice quite a few LoadModule directives. After the last directive paste this line: LoadModule php5_module "C:/php5/php5apache2.dll" This will load PHP as a module when Apache is started.

Now scroll even more (or use CTRL+F) until you find a few AddType directives. Right beneath them paste this 2 lines: AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
The first line tells apache to run the files with the .php extension through the PHP module to be parsed and output the result, while the second tells the server to pass the files with the .phps extension to the php module to highlight the code (be carefull, this means they will not be parsed; this is usefull if you want an easy way to see the syntax of your PHP files).

There's only one more line you have to add at the end of this file: PHPIniDir "C:/php5/php.ini" This tells Apache where the php.ini file is so that it can load PHP with the proper settings. Now you only have to change a few existing directives and you're done.

First you have to change the DocumentRoot (CTRL+F to find it) directive. This tells the server where you want to store the files that should be accessible on the web. By default this points to the htdocs directory of where you've installed apache, but I recommend mapping this to another partition than the one with Windows on it, because in case something happens and you need to format the partition you won't lose your data (this is somewhat equivalent for the database server, but I haven't advised changing the default location of the folder where your databases are located because it's not recommended to copy the files from one installation to another, but keep sql dumps of your database and import those when you migrate - so remember to make periodical backups). To get back to the subject, I've edited my location like this: DocumentRoot "D:/Work/www" There is only one thing left to change: below the DocumentRoot line there must be a Directory directive pointing to the old location (old in case you've changed it) of the DocumentRoot. You must change this to point to the same directory as the DocumentRoot directive, for example (in my case): <Directory "D:/Work/www"> Now you have to restart the server (actually, after any change in the httpd.conf or php.ini file you have to restart Apache for the changes to take effect) and if you type localhost in the browser address bar you should see an empty directory (in case there is nothing in the D:/Work/www directory). Create a index.php file in that directory, open it and write: <?php phpinfo(); ?> Save it, refresh the page and click on the file - you should see all the details of your PHP installation.

Note: if you want your server to automatically load the index.php file (as it does with index.html files) you need to edit the httpd.conf file again. So open the file, CTRL+F for index.html and you should see a line like this: DirectoryIndex index.html index.html.var which can be changed by adding any filename you want separated by a white space. Here is mine: DirectoryIndex index.html index.htm index.php Now restart the server and if you go to localhost and there is an index.php in the coresponding directory it will be loaded automatically.

MySQL Tools

Please note that this tools are not necessary in order to use MySQL, but they will ease your work a lot.