Running Multiple Websites on Apache
July 1st, 2007
You’ve been creating websites on your Apache installation on Windows - making a sub-directory for each site you want to design/develop. But wouldn’t it be good if you could run multiple websites on a single Apache installation - changing http://localhost/mynewsite1/, http://localhost/mynewsite2/, etc. to http://mynewsite1/, http://mynewsite2/, etc. Well you can! - with Apache Virtual Hosts. This article will show you how to run as many websites (or Virtual Hosts) as you like on your Apache installation on Windows by making a few simple additions to your Apache configuration file and your Windows hosts file.
What We’ll Assume
- You have a working installation of Apache 2.2.4 on Windows (if not, take a look at Installing Apache, MySQL and PHP on Windows).
- Your Apache DocumentRoot - where your local website files are - is in the default location of C:\Program Files\Apache Software Foundation\Apache2.2\htdocs (if not, simply replace all occurrences of C:/Program Files/Apache Software Foundation/Apache2.2/htdocs with whatever your DocumentRoot is - e.g. C:\www).
Let’s Get On With It
-
Create a new folder C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\mynewsite
-
Go to Start > Programs > Apache HTTP Server 2.2.4 > Configure Apache Server > Edit the Apache httpd.conf Configuration File
The Apache configuration file opens (httpd.conf)
-
Add this line at the very bottom of the file:
NameVirtualHost localhost
Apache will now allow multiple Virtual Hosts to run under localhost
-
Directly under the last line you added in httpd.conf, add the following VirtualHost directive:
<VirtualHost localhost> DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/mynewsite" ServerName mynewsite </VirtualHost> -
Save and close httpd.conf
-
Open C:\WINDOWS\system32\drivers\etc\hosts in Notepad
Your Windows hosts file opens
-
You should see this line somewhere in the file:
127.0.0.1 localhost
-
Directly below this line, add the following:
127.0.0.1 mynewsite
- Save and close hosts
- Go to Start > Programs > Apache HTTP Server 2.2.4 > Control Apache Server > Restart
- Open mynewsite in your web browser (http://mynewsite/)
- If all went well, your browser will show a page with Index of /
Installing Apache, MySQL and PHP on Windows
June 1st, 2007
This is it! The definitive guide to installing and configuring Apache 2.2.4, MySQL 5.0.27 and PHP 5.2.1 on Microsoft Windows XP (SP2) with support for both MySQL and MySQLi PHP extensions.
And, yes, if you follow these instructions to the letter, the software will actually work!
Before getting started with the installations, you’ll need to download the required software packages - all of which are conveniently listed in the following table.
| Where | What | What Exactly |
|---|---|---|
| Apache 2.2.4 | Win32 Binary (MSI Installer) | apache_2.2.4-win32-x86-no_ssl.msi |
| MySQL 5.0.27 | Windows Essentials (x86) | mysql-essential-5.0.27-win32.msi |
| PHP 5.2.1 | PHP 5.2.1 zip package | php-5.2.1-Win32.zip |
Installing Apache
-
Double-click apache_2.2.4-win32-x86-no_ssl.msi
The Apache HTTP Server 2.2 - Installation Wizard appears with Welcome to the Installation Wizard for Apache HTTP Server 2.2.4
Click Next
-
License Agreement
Ensure “I accept the terms in the license agreement” is selected
Click Next
-
Read This First
Click Next
-
Server Information
Enter the following details:
Network Domain: localhost
Server Domain: localhost
Administrators Email Address: admin@localhost
Ensure “for All Users, on Port 80, as a Service — Recommended” is selected
Click Next
-
Setup Type
Ensure “Typical” is selected
Click Next
-
Destination Folder
Accept the default destination folder (C:\Program Files\Apache Software Foundation\Apache2.2\)
Click Next
-
Ready to Install the Program
Click Install
-
Installing Apache HTTP Server 2.2.4
Wait while the Installation Wizard installs Apache
-
Installation Wizard Completed
Click Finish
Testing Your Apache Installation
In your web browser, go to http://localhost/. If Apache has been installed successfully, you will see the words “It works!”
The “It works!” web page - index.html - is located in the root directory of your Apache installation. The root directory can be found here:
C:\Program Files\Apache Software Foundation\Apache2.2\htdocs
Installing MySQL
-
Double-click mysql-essential-5.0.27-win32.msi
The MySQL Server 5.0 - Setup Wizard appears with Welcome to the Setup Wizard for MySQL Server 5.0
Click Next
-
Setup Type
Ensure “Typical” is selected
Click Next
-
Ready to Install the Program
Click Install
-
Installing MySQL Server 5.0
Wait while the Setup Wizard installs MySQL
-
MySQL.com Sign-Up
Ensure “Skip Sign-Up” is selected
Click Next
-
Wizard Completed
Ensure “Configure the MySQL Server now” is checked
Click Finish
-
The MySQL Server Instance Configuration Wizard appears with Welcome to the MySQL Server Instance Configuration Wizard 1.0.8
Click Next
-
Please select a configuration type
Ensure “Standard Configuration” is selected
Click Next
-
Please set the Windows options
Ensure “Install as a Windows Service” is checked
Ensure “MySQL” is selected for Service Name
Ensure “Launch the MySQL Server automatically” is checked
Ensure “Include Bin Directory in Windows PATH” is checked
Click Next
-
Please set the security options
Enter and Confirm (retype) your password
Click Next
-
Ready to execute …
Click Execute
-
Processing configuration …
Wait while the Server Instance Configuration Wizard configures MySQL
Click Finish
Connection Error
In some cases (usually when MySQL is being installed over a previous version), the MySQL Server Instance Configuration Wizard will give a Connection Error that looks something like this:
If this happens to you:
-
Go to Start > Programs > MySQL > MySQL Server 5.0 > MySQL Server Instance Config Wizard
The MySQL Server Instance Configuration Wizard appears
Click Next
-
Please choose a maintenance option
Ensure “Reconfigure Instance” is selected
Click Next
-
Please select a configuration type
Ensure “Standard Configuration” is selected
Click Next
-
Please set the Windows options
Ensure “Include Bin Directory in Windows PATH” is selected
Click Next
-
Please set the security options
Ensure “Modify Security Settings” is checked
Enter and Confirm (retype) your password
Click Next
-
Ready to execute
Click Execute
-
Processing configuration …
Wait while the MySQL Server Instance Configuration Wizard reconfigures MySQL
Click Finish
Testing Your MySQL Installation
-
Go to Start > Programs > MySQL > MySQL Server 5.0 > MySQL Command Line Client
The MySQL Command Line Client appears with a prompt to Enter Password
-
Enter the password you specified during the installation
Press Enter
-
The MySQL Command Line Client will connect to the MySQL Server and produce the following welcome message and mysql prompt:
Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 3 to server version: 5.0.27-community-nt Type 'help;' or 'h' for help. Type 'c' to clear the buffer. mysql>
Installing PHP
- Create a new folder C:\php
- Copy the contents of php-5.2.1-Win32.zip to C:\php
- On your desktop, right-click My Computer and select Properties
-
In the System Properties dialogue box select the Advanced tab
- Click the Environment Variables button
-
In the Environment Variables dialogue box under System variables select Path
- Click the Edit button
-
In the Edit System Variable dialogue box add C:\php to the Variable value field
- Click the OK button for all open dialogue boxes
Configuring PHP
- In C:\php, copy the file php.ini-recommended
- Rename the Copy of php.ini-recommended to php.ini
- Open php.ini in Notepad
-
In php.ini, find this line:
display_errors = Off
and enable the display of errors by changing the value to “On”:
display_errors = On
Note: You wouldn’t do this in a production environment - this is just for testing and development!
-
In php.ini, find this line:
extension_dir = "./"
and specify the directory in which the loadable extensions (modules) reside by changing the value to “ext”:
extension_dir = "ext"
-
In php.ini, find these lines:
;extension=php_mysql.dll ;extension=php_mysqli.dll
and enable both the MySQL and MySQLi extensions by uncommenting the lines:
extension=php_mysql.dll extension=php_mysqli.dll
-
In Windows Explorer, copy the following dll files:
C:\php\libmysql.dll
C:\php\ext\php_mysql.dll
C:\php\ext\php_mysqli.dll
-
Paste the dll files to C:\WINDOWS\system32
Note: Most installations don’t require you to copy libmysql.dll to system32, but some do. So it’s better to be safe than sorry!
-
Go to Start > Programs > Apache HTTP Server 2.2.4 > Configure Apache Server > Edit the Apache httpd.conf Configuration File
The Apache configuration file opens (httpd.conf)
-
Add the following three lines at the end of the file:
LoadModule php5_module "C:/php/php5apache2_2.dll" AddType application/x-httpd-php .php PHPIniDir "C:/php"
-
In httpd.conf, find this directive:
<IfModule dir_module> DirectoryIndex index.html </IfModule>Add index.php as a directory index:
<IfModule dir_module> DirectoryIndex index.php index.html </IfModule> - Save and close httpd.conf
-
Go to Start > Programs > Apache HTTP Server 2.2.4 > Control Apache Server > Restart
Note: You need to restart Apache any time you make changes to the php.ini and/or httpd.conf files
Testing PHP
- Open Notepad and type the following:
<?php phpinfo(); ?> - Save the file as info.php in your web root directory (C:\Program Files\Apache Software Foundation\Apache2.2\htdocs)
- Close info.php
-
Open info.php in your web browser (http://localhost/info.php)
If PHP is installed correctly, you will see a table of information pertaining to your PHP installation environment
-
Open Notepad and type the following:
<?php $mysqli = new mysqli('localhost', 'root', 'yourpassword', 'test'); if (mysqli_connect_errno()) { printf('Connect failed: %s', mysqli_connect_error()); exit(); } printf('Host information: %s', $mysqli->host_info); $mysqli->close(); ?> - Save the file as mysqli.php in your web root directory (C:\Program Files\Apache Software Foundation\Apache2.2\htdocs)
- Close mysqli.php
-
Open mysqli.php in your web browser (http://localhost/mysqli.php)
If MySQL is enabled, you will see the following output:
“Host information: localhost via TCP/IP”
Finished!
That’s it, you’re done. Hopefully it all came together without any major dramas. If not, you have my deepest sympathies. And if that’s not enough, please leave a comment specifying exactly where it all went pear-shaped for you and I’ll do my best to get to the bottom of it.