Saturday, February 03, 2007

Complete Idiot's Guide to WAMP

So, you got it in your head for some reason that you want to run a webserver on your Windows machine? With PHP, MySQL and all that fancy jazz? Well so did I, so I wrote this little guide after mucking about for two days, following various guides that didn't work, were incomplete, incorrect or outdated.

If you're like me, you have been using one of the preconfigured packages, like EasyPHP or XAMPP. The problem is, after using that for about a year, new versions of PHP, Apache and MySQL popped up left and right. And new versions of those handy packages don't come out overnight. Lucky for you, it's possible to download the components separately, mash it all together and have it come out a working WAMP server!

The following howto is a guide helping you on your way with setting up Apache 2.2, PHP 5 and MySQL 5.0 on a Windows XP machine. Windows 2000 or newer works as well, Windows 98/ME will NOT (stick with Apache 1.3 for those). I tried to make it as detailed as possible without including screenshots of each step in the install process and adding 'click Next on this screen'.

Get the software

First, you'll need to download everything so you can install everything in one big swoop.
  • Apache 2.2 - the actual webserver. Select a mirror, then click the link for Win32 Binary (MSI Installer).
  • PHP 5 - the scripting language that will be the middle man between Apache and the MySQL database. Click the link for PHP 5.2.0 zip package (the installer does not have all extensions).
  • MySQL 5 - the database software. Click on 'MySQL Community Server', then click 'Select a mirror' for Windows (x86) ZIP/Setup.EXE. Skip past the form and click one of the HTTP links for a mirror close to your location.
  • phpMyAdmin - a PHP 'program' that allows you to configure MySQL. You could use the commandline programs that come with MySQL, but this is way easier. Click the link for english.zip.

IMPORTANT: If you plan on doing ANY kindof IP-based filtering in Apache, or use PHP for any kind of IP-based verification (with $_SERVER['REMOTE_ADDR']), do NOT get Apache v2.2.4!! There is a bug in this Apache version together with PHP 5 that makes it return 0.0.0.0 instead of the correct IP. This bug also affects any kind of IP address filtering with allow/deny in httpd.conf/.htaccess. Get Apache v2.2.3 instead, it works fine and does not have this bug.

Install everything

This is dead easy. Follow my lead.

Base directory: to keep things together, you might want to create a central folder where you're going to put everything. Preferably without spaces in the name. I chose to make C:\WAMP. If you prefer something else, substitute your path wherever it says C:\WAMP in the below steps.

Apache: Open the MSI installer file and click through the screens. Accept all the default settings until you reach the Destination Folder, where you will change it to C:\WAMP\Apache. If all goes well, you should be seeing a little Apache feather with a green icon in the system tray:

The Apache Monitor in the system tray
This is the Apache Service Monitor, which keeps an eye on the Apache process itself. It's handy for controlling Apache, but if you like, you can shut down the monitor without closing Apache itself.
Now go to http://localhost. If you get the message below, you did everything right.

It works!

PHP: Open the PHP install zip and extract everything to C:\WAMP\PHP. Make sure all the subdirectories are created.

PHP directory

MySQL: Open the MySQL install zip and double-click the Setup.exe file inside to start it. Pick the Custom install, then change the install path to C:\WAMP\MySQL. Let it install MySQL, skip the 'MySQL.com Sign-Up' and continue to configuring the MySQL server.
At the screen for 'default character set', select your best option.
At the screen for 'set security options', enter a root password.
Everywhere else, accept the default setting. Click Execute at the end to configure the MySQL server and you're done!

phpMyAdmin: Open the phpMyAdmin install zip and extract everything to C:\WAMP\Apache\htdocs. Make sure all the subdirectories are created. Open the install folder and rename the long-ass phpMyAdmin-2.x.x-english directory to pma.

phpMyAdmin directory

You now have four separate pieces of software installed! Next up: tieing everything together into a neat WAMP package.

Configuring

If you did everything correctly, Apache is already up and running. Now all we need to do is:
  1. Make Apache understand PHP
  2. Make PHP understand MySQL
  3. Make Apache understand MSQL
  4. Configure phpMyAdmin
Sounds easy? Well it's not really all that easy, but that's what this guide is for.

Step 1:

Find the file C:\WAMP\Apache\conf\httpd.conf and open it in Notepad (or whatever you prefer).

Find the section with all the LoadModule lines, and add this one to the bottom:
LoadModule php5_module "c:/WAMP/php/php5apache2_2.dll" (note: use forward slashes)
This makes Apache load the PHP module.

Next, find the line that starts with DirectoryIndex. Change it so it reads:
DirectoryIndex index.php index.html
This makes Apache understand index.php (default) files.

Now find the section with the AddType lines. Add these two:
AddType application/x-httpd-php .php .phtml .inc .php3
AddType application/x-httpd-php-source .phps

This makes Apache know what to do with the .php filetype.

Save the file and close it.
Next, go back to your C:\WAMP\PHP directory and do this:
  • Copy the php5ts.dll file to C:\WAMP\Apache\bin.
  • Copy the php.ini-recommended file to C:\WAMP\Apache.
  • Rename the file to php.ini.

The copied PHP.ini file

Open the php.ini file in Notepad, and change this line:
short_open_tag = Off
to:
short_open_tag = On
This allows for writing php using the short <? .. ?> tags.

While you're at it, change this line:
;session.save_path = "/tmp"
to:
session.save_path = "c:\windows\temp" (or wherever your temp folder is)
This lets PHP know where to store sessions.

Double-click the Apache icon in the system tray and click Restart to make Apache read the new configuration. Apache is now configured to understand PHP!

To test this, create C:\WAMP\Apache\htdocs\index.php. Then open it in Notepad and type <?phpinfo()?>. Save the file and close it.
Go back to http://localhost and refresh. If you see a boatload of PHP information, you did it!

phpinfo() demo

Step 2:

Now the next part is adding MySQL support to PHP.

Find the file C:\WAMP\Apache\php.ini and open it. Find the line:
extension_dir = "./"
and change it to:
extension_dir = "C:\WAMP\PHP\ext"
This lets Apache know where to find the MySQL extension for PHP.

Find the line:
;extension=php_mbstring.dll
and change it to:
extension=php_mbstring.dll
This loads the multi-byte string extension so phpMyAdmin can handle strings correctly.

Find the line:
;extension=php_mysql.dll
and change it to:
extension=php_mysql.dll
extension=php_mysqli.dll

This lets Apache load the two MySQL extensions from the PHP extension directory.

Save the file and close it. PHP is now configured to use MySQL!

Step 3:

All you need to do now is make Apache load MySQL. Easy enough!
Find the file:
C:\WAMP\MySQL\bin\libmySQL.dll
Copy it to:
C:\WAMP\Apache\bin\libmySQL.dll
This makes Apache load the MySQL library.

Go back to the Apache Service monitor again (double-click the icon in the system tray) and restart Apache once more to load all the new settings and libraries.

Step 4:

Now go to http://localhost/pma to load phpMyAdmin.
Since you haven't configured it yet, it can't access the MySQL database.

phpMyAdmin error screen

To do this:
  • Go to the C:\WAMP\Apache\htdocs\pma directory.
  • Rename the config.sample.inc.php file to config.inc.php.
  • Open the file in Notepad and find this line:
    $cfg['Servers'][$i]['auth_type'] = 'cookie';
  • Change it to:
    $cfg['Servers'][$i]['auth_type'] = 'config';
  • Below it, add this line:
    $cfg['Servers'][$i]['user'] = 'root';
  • Below that, add this line:
    $cfg['Servers'][$i]['password'] = 'your MySQL root password';
  • Find the line with controluser and delete the part pmauser between the quotes.
  • Find the line with controlpass and delete the part pmapass between the quotes.
  • Save the file and close it.
If you reload http://localhost/pma now, it should give you the main screen for phpMyAdmin.

phpMyAdmin main screen

You're done! Everything is setup now. Your WAMP server is finished!


Problems?

  • If you can't get to http://localhost, Apache isn't running. Go to Start > Run > services.msc, find Apache2 in the list and start it. If it's not in the list, reinstall Apache.
  • If Apache doesn't start, there is an error in the httpd.conf configuration file.
  • If no one can get to your webserver, make sure your firewall and/or router allows it.
  • Make sure you did everything correctly!

10 comments:

xingweb said...
This comment has been removed by a blog administrator.
Erik said...

I don't think step 3 is needed. Apache doesn't need to know about mysql, since it isn't accessing the database. However, libmysql needs to be in the PATH environmental variable. Your Apache bin directory is probably in the PATH, that's why copying the file there works. It's probably a nicer solution to just put the php-dir in the PATH. See also the php site on mysql under installation on windows systems.

Erik said...

Also, be aware that turning on short tags means you can't just write out an "<? xml" declaration (you'll have to use "echo"), and that your source won't be valid xml if you use short tags in your own code.

Also, the comment about the PATH also goes for the copying in step 1 of php5ts.dll and php.ini. This isn't needed if you add the php-dir to your PATH.

M said...

While you're correct about the PATH stuff, I don't think a 'complete idiot' can change the PATH global env variable. :)
Plus, it requires that you reboot.

bhp said...

thanks much, but i have a question and i am a complete idiot - so prepare yourself...
on my apache set up instead of the making use of the default htdocs directory i am using another, so in step 4 when asked to check http://loacalhost/pma i added an ALIAS directive to point to the pma directory in C:\wamp\apache\htdocs - i do get to the "redirected" pma directory however it don't jive - check out address bar - http://localhost/pma/error.php?lang=en&dir=ltr&type=Error&error=Cannot+load+%5Ba%40http%3A%2F%2Fphp.net%2Fmysql%40Documentation%5D%5Bem%5Dmysql%5B%2Fem%5D%5B%2Fa%5D+extension.+Please+check+your+PHP+configuration.+-+%5Ba%40.%2FDocumentation.html%23faqmysql%40documentation%5DDocumentation%5B%2Fa%5D&
what to do?

bhp

LMo said...

Your guide has been amazingly helpful. I followed all the steps and yet am still in need of help. When I restarted Apache and made the new file, etc, I did not get a boatload of PHP info. I went back and rechecked everything...three or more times. I still can't figure out where I went wrong. This also means that all the steps afterward are not working. Help please?

LMo said...

Ok,it is now undercontrol. I think it was an extra character somewhere so that step is taken care of.

LMo said...

I just learned that to be more syntactically (sp?) correct and because default configurations don't always have php short tags enabled, the proper information for the PHP test should be:

"To test this, create C:\WAMP\Apache\htdocs\index.php. Then open it in Notepad and type <\?php phpinfo(); ?>. Note: Don't included the period - it's the end of a sentence. Also, remove the backslash. It was required because this blog doesn't allow PHP tags to be posted. Save the file and close it.
Go back to http://localhost and refresh. If you see a boatload of PHP information, you did it!"

Dan said...

I'm having real problems getting the localhost page to play along.
I'm not getting the boatload of PHP info, after lots of attempts of going through the steps.
I've no idea where I'm going wrong.

eshaman said...

THANK YOU THANK YOU THANK YOU!!!!

I've been battling a wamp server that suddenly decided not to work anymore (or rather Apache decided not to work) and after reading this I figured it out. I had forgotten that I modified httpd.conf! :)

Removed the changes and I'm back in business, thanks again :)