Tutorials - Servers > Installing Apache, MySQL and PHP
Tutorials & FAQs: Servers: Installing Apache, MySQL and PHPIn this tutorial I'll demonstrate how to create a fantastic PHP & MySQL testing and development environment using the industry standard web server Apache. Everything you require for this can be downloaded from the Internet for free and is easy to setup. The guide is aimed at Windows 2000/XP users, however if you use Windows 9x, the proceedure is similar - see the 'readme' files included with each download for specific changes.
MySQL
Firstly, let's start with MySQL. This is a completely standalone application, so it can be installed first. Head over to http://www.mysql.com and download the Windows version with installer. Unzip this somewhere on your desktop and run Setup.exe. Follow the wizard; the default install location is C:\mysql.
Finish the wizard and fire up Notepad. You need to specify where you installed MySQL - edit the example that can be found where you unzipped, e.g. at C:\MySQL\my-example.cnf. Save this in your root directory (for example C:) as mycnf.cnf
If you get stuck, there is a lot of helpful information in the 'Documentation' Section of the MySQL website, at http://www.mysql.com/documentation
The C:\MySQL\bin folder contains all the files for MySQL to run. Open up Command Prompt and enter
| C:\mysql\bin\mysqld-nt --install |
to run MySQL as a service.
Go to Start>Programs>Administrative Tools>Services. Find MySQL on the list and click start. You can test to see if MySQL is working by entering
| C:\mysql\bin\mysql test |
in Command Prompt.
Now you need to change the Root username and password for access. You need to enter MySQL in Command Prompt - use this as a guide:
| C:> C:\mysql\bin\mysql mysql mysql> DELETE FROM user WHERE Host='localhost' AND User=''; mysql> QUIT C:> C:\mysql\bin\mysql\admin reload C:> C:\mysql\bin\mysql\admin -u USERNAME password PASSWORD |
Apache
Now MySQL server is running, we need to install Apache. Go to http://httpd.apache.org. You need to download the Win32 version with the MSI installer. There are two releases of Apache to download - version 1.3.xx or 2.0.xx. Both are adequate for our use, but version 2.0.xx is the most recent and has several improvements over 1.3.xx. However at the time of writing, PHP support for Apache 2.0.xx is only experimental.
You can install Apache wherever you want - I'll use C:\Apache. In the "Server Information" box, you need to type localdomain in the Network Domain box, then either localhost or your IP address (if you want to allow external access to your server) in the Server Name box. Tick the All Users box.
Finishing this wizard will setup Apache shortcuts on your Start Menu. From here you can Start/Stop/Restart Apache, access the logs and change its settings. Settings for Apache are held in a file called httpd.conf. You'll need to edit this to install PHP.
PHP
We currently have both MySQL and Apache running - next job is to install PHP. Go to http://www.php.net - make sure you download the Win32 ZIP file (this has support for MySQL and Apache, the other doesn't). Extract the ZIP file to a sensible location, e.g. C:PHP .
Open the location you unzipped to, and find a file called php.ini-dist . Open it, then find the extension_dir setting and change it to where you unzipped, e.g. C:\PHP. Now save this in your Windows directory as php.ini. Note: this is also a file called php.ini-recommended. This could also be used, but is suggested for production servers.
Now you need to tell Apache what to do with PHP files. Click on the option in the Apache group on your Start Menu to "Edit the Apache httpd.conf configuration file". Scroll down to about 2/3rds of the file to find a section called AddType. Depending on what version of Apache you are running, add the following code (edit location if necessary). This code may change, especially with Apache 2.0, so double check your PHP readme file!
Version 1.3.xx
| LoadModule php4_module c:/php/sapi/php4apache.dll AddModule mod_php4.c AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps |
Version 2.0.xx
| LoadModule php4_module "c:/php/sapi/php4apache2.dll" AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps |
Now, save httpd.conf and exit. Click Restart in the Control section of the Apache group on your Start Menu. Now, your system is ready for testing.
In Apache, all the files that will appear on the server are kept in the "htdocs" directory, if you installed in the default location it would be C:\Apache\htdocs. Fire up notepad and type:
| <? phpinfo(); ?> |
Save this as info.php in your htdocs directory.
Now open an internet browser and type in http://localhost/info.php . You should get a page of information about PHP. Note, when you access Apache from your machine, you type in localhost, regardless of if you've set an IP as the Servername.
Test MySQL
Open Notepad again and type in:
| <html> <head> <title>MySQL Test</title> </head> <body> <h1> <?php $connection=mysql_connect("localhost","USERNAME","PASSWORD") or die("Could not connect to MySQL"); echo "Successfully connected to MySQL"; ?> </h1> </body></html> |
Replace the username and password with those you created when MySQL was installed. Then save it in your htdocs directory as mysqltest.php. Now go to http://localhost/mysqltest.php and see what happens!
phpMyAdmin
The last part is to install phpMyAdmin so you can manage MySQL databases and tables easily. Go to http://www.phpmyadmin.net and download the Zip file of the latest stable version.
Now unzip this to a folder in your htdocs directory, for example C:\Apachehtdocs\PHPMA . Find the file config.inc.php and open it up in Notepad.
You need to find and edit the following lines:
| $cfg['PmaAbsoluteUri'] = 'http://localhost/PHPMA/'; |
This sets the absolute address of phpMyAdmin. Change it depending on what folder in the htdocs you unzipped to.
| $cfg['blowfish_secret'] = 'PASSWORD'; |
Enter a password to use for keeping phpMyAdmin secure.
| $cfg['Servers'][$i]['host'] = 'localhost'; |
Set this to localhost if not already set.
| $cfg['Servers'][$i]['auth_type'] = 'cookie'; |
This determines how phpMyAdmin authorises your login. Cookie is recommended.
| $cfg['Servers'][$i]['user'] = 'USERNAME'; |
Enter your MySQL username.
Now save this file. You shouldn't change any of the other options unless you feel confident in doing so! Open up your web browser and go to http://localhost/PHPMA or wherever you installed to. Login with your MySQL Username and the blowfish password you just created.
Other Servers
A popular alternative to Apache as your webserver is Abyss. This is free and available for Windows, MacOS X, Linux, and FreeBSD operating systems. It is also very easy to install ASP, PHP or Perl onto it using the guides available on their website at http://www.aprelium.com/abyssws/
A good listing of free server tools can be found at http://www.webattack.com/freeware/categories/9/
Tutorial written by James Hancock and originally published on http://www.olate.com
Original Article by: mytton - Edited by: mytton