The other day I was looking for a tutorial to install Apache, php and MySQL on Windows and I wasn’t really able to find anything that cover most basic issues.
I don’t want to replace my current webserver, (IIS), I want to install Apache next to
Basic notes before we start.
- You must be the Administrator on the machine.
- This is not a how-to install a website on a server, this is how to install a website on your own development machine, (so some server security settings will be ignored).
- It is best to fist follow all the steps as I have listed them and then make changes that fit your needs.
- Each step has a ‘test’ to make sure it works, if it did not then don’t go any further, it will only make things worse. Installing PHP will not magically make Apache work.
- On an English version of Windows all programs are installed in the c:\program files\ directory, but it it called something else in other languages, (the variable is %programfiles%).
If you are using a 64 bit machine then the folder is different for 86 bit applications.
By default the installed will direct you to the program files folder, just pay attention to where that folder is.
- I used port number 8001, you can choose another number if you want, (but not 80 or 8080 just yet).
- If you have some weird and wonderful firewall you might want to turn it off during the installation, turn it back one once you know it is working. If it does not work with the firewall then you know what needs to be fixed.
- Turn off your Anti-virus as well if you don’t think it is clever enough to recognize Apache, PHP or MySQL.
- Make a note of all the folders you install things in. Don’t just blindly press ‘next’, ‘next’, …
1) Install Apache
Go to the Apache website and download the latest version, don’t get the ‘Alpha’ or ‘Beta’ version but rather get the one labelled, “the best available version’. I got version 2.2.17, (without crypto).
- Run the install and accept all the basic settings.
- in Apache folder, (c:\Program Files\Apache Software Foundation\Apache2.2\), look for the http.conf file and edit it, (it is a normal text file).
- Change the Listen value:
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
- And the change the ServerName value:
# If your host doesn’t have a registered DNS name, enter its IP address here.
- Double click on the Apache icon on the task bar, that will bring up the ‘Apache service monitor’
The server is running, (if you don’t have IIS installed), or not running, (if there was an issue with IIS).
- Select the [Start] or [Restart] button, it should then start ok …
If it does not start there is a problem, double check the settings. If you are certain that the settings are correct try another port. Instead of 9001 for example, try 8001 or something like that.
- Open your web browser and navigate to http://localhost:8001/ you should get a simple message “It works!!!“
- Change the location of your website to something more developer friendly.
in C:\Program Files\Apache Software Foundation\Apache2.2\conf\http.conf change:
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
# This should be changed to whatever you set DocumentRoot to.
- Don’t add a ‘/’ at the end of the path, so don’t write ‘<Directory “c:/website/“>‘
- Create a folder c:/website/, don’t put any files or anything in it.
- Restart the Apache server again, (on the task bar, select the apache server, “Open Apache Monitor”, select [Start] or [Restart], it should then start ok…).
- Open your web browser and navigate to http://localhost:8001/ you should get a message “Index of /“
2) Install PHP
Go to the php.net website and download the latest stable version. Get the windows installer, I got version 5.2.14.
- Run the install and accept all the default settings.
- During the install you will be prompted to select a server, select Apache 2.2
- It will then ask you for the location of the Apache configuration file, select “C:\Program Files\Apache Software Foundation\Apache2.2\http.conf“
- After the install, double check that the Apache configuration file was updated properly.
In C:\Program Files\Apache Software Foundation\Apache2.2\http.conf at the end of the file the php installer will have added
#BEGIN PHP INSTALLER EDITS – REMOVE ONLY ON UNINSTALL
PHPIniDir “C:/Program Files/PHP/”
LoadModule php5_module “C:/Program Files/PHP/php5apache2_2.dll”
#END PHP INSTALLER EDITS – REMOVE ONLY ON UNINSTALL
- Edit the php.ini file to display all errors. By default the file is located in “C:/Program Files/PHP/”
Replace, “display_errors = Off” with “display_errors = On“.
; To output errors to STDERR with CGI/CLI:
;display_errors = “stderr”
display_errors = On
- One more time, restart the Apache server, (see above).
2.1) Create a php test file.
- Open a blank document
echo “Hello from my website”
- Save the document as “index.php” in your website root folder, (c:/website)
- go to http://localhost:8001/index.php
- You should see “Hello from my website
- If you see the code () then it means you did not restart the Apache service
- if you see nothing there might be a type in your code.
- Make sure that you have nothing before ”<?php’ and after ‘?>’
2.2 ) Make index.php the default
- In Apache configuration file, (“C:\Program Files\Apache Software Foundation\Apache2.2\conf”), Update DirectoryIndex and add add index.php
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
DirectoryIndex index.html index.php
- The order is important, if you have both, index.html and index.php in your website then index.html will be displayed.
- Restart the Apache server, “Open Apache Monitor”, select [Start] or [Restart], it should then start ok…
- Go to http://localhost:8001/ and you should see “Hello from my website”
- if you see “Index of /” you didn’t restart the Apache service
- if you don’t see “Hello from my website” or “Index of /“, do you have an index.html file that is showing?
3) Install MySQL
Go to the MySQL website and download the latest stable version. Get the windows installer, I got version 5.1.52 You can get version 32bit or 64bit. Get the recommended, ‘Essential’ download.
For version 5.2, (and earlier), you also need to download mysql extension (PHP 5.2.0).
- Do the typical install and accept all settings.
- The last step is to configure
3.1) Configure the MySQL server now.
Select the following, (mostly default), options
- Detailed configuration.
- Developer machine.
- Multifunctional database
- Select/Edit “c:\website\data\” rather than the default, (this is easier to locate and/or port to another machine).
- DSS/OLAP server instance configuration.
- port number 3306.
- Best support for Multilingualism, (you will need it at some stage and it does not harm even if you think you won’t need it).
- Service name “MySQL”.
- Set the root password, (and write it down!). Even for development it is good practice to use a strong password.
- Press the ‘Execute …’, (takes a bit of time). You should get no errors.
If you get an error on the last step it is almost because of a security issue, (a Windows/Firewall/Anti virus issue).
3.2 Tell PHP about MySQL.
In the old days PHP would install the MySQL extension by default, in 5.x that changed and you had to download it. In 5.3 it is bundled again.
Either way, make sure that PHP knows how to work with PHP, the following steps might already be done for you by the installer(s), if it is don’t overwrite the settings.
- Unzip the mysql extension you downloaded earlier, (http://dev.mysql.com/downloads/mysql/) and copy them to your php directory.
By default located in c:\program files\php\
There should be 2 files, libmysql.dll and php_mysql.dll, copy them both.
If the files already exist, do not overwrite those files.
- Edit your php.ini file (By default located in c:\program files\php\php.ini)
Add the mysql extensions.
; Dynamic Extensions ;
- On the task bar, select the Apache server, “Open Apache Monitor”, select [Start] or [Restart], it should then start ok…
- If Apache does not restart, , did you copy both dlls or just one?
- Did you get the right files, (on the download site, did you get the right MySQL version and right php version)?
- Did you download the mysqli extension or the mysql extension, (note the extra ‘i’)?
3.3 Create a mysql/php test files.
- Open/create a new, empty file.
- Type the following
// Report all PHP errors (see changelog)
$con = mysql_connect(‘localhost’,’root’,’<yourpassword>‘);
die(‘Could not connect: ‘ . mysql_error());
echo “Connected to database!!”;
- Save the file “c:\website\connect.php“
- Go to your site and check that you are connected to your MySQL database
- If you get ‘Could not connect’, did you put the right user name and password, (replace <yourpassword>)?
- If you see nothing at all check your php.ini file to display errors, (see above).
- If you see “Call to undefined function mysql_connect()” check your php.ini file for mysql settings. If they look ok, restart Apache again and make sure it has no errors.
This should be it, your web server should be up and running, you can now restart your firewall, check that the website is still running, then restart your anti virus.
Once all this is running you can play with various settings, remember that you, (almost always), need to restart Apache once you made a change to the http.conf or php.ini files.