Tag:

How to set up a local web server on your computer with Apache, PHP and MySQL

Posted: July 19th, 2010     Web Development

In this tutorial, I’m going to explain how to set up a web server on your computer (Windows) with Apache, PHP and MySQL. I’m also going to show you how to configure name-based virtual hosting so that you can run multiple sites off the web server. I’m then going to validate the set up by installing WordPress.

Why would you want to do this?

The main reason for setting up a local web server is so that you can develop and test your website(s) very quickly, without having to constantly upload to another server. It’s a good way to “stage” your content in an environment which somewhat mimics what the live server environment would be. Whether you’re developing themes or plug-ins for wordpress, or creating a site from scratch using PHP/MySQL, this is a great way to develop and test stuff.

Explanation

This is basically a three step process (and it would be the same on a Mac, though I’m doing this on a PC):

  1. Install XAMPP (or MAMPP)
  2. Configure Apache virtual hosts file
  3. Configure host file

Install XAMPP

Installing XAMPP is dead easy. Download it and install it.

The one thing that I would recommend to do before jumping into configuring virtual hosting is that you fire up XAMPP to make sure that the installation works. You can start XAMPP either by using the GUI which is conveniently installed to your Start Menu. Look for “XAMPP Control Panel”. Or you can start using the command line by clicking Start->Run and typing “cmd”. Type “cd c:\xampp”. Then “xampp_start”.

Make sure you have other network-listening apps such as Skype turned off as they may prevent Apache from starting.

You should now be able to fire up a web browser and visit http://localhost, which will load up the default XAMPP page.

Configuring Virtual Hosting

Virtual hosting is basically a way of hosting multiple websites through the same server. It’s as if it’s running several servers but it’s all running off one server and IP address. So…for example… if you have a server and it has the IP address of 127.0.0.1, you can run 10, 20, 100 different websites on that server. You’d also be able to call the individual websites by their name. For example in this case I have “upvector.local”.

Once you’ve validated that the installation of XAMPP works, you’ll need to set up the Apache virtual hosts file. This is typically in:

XAMPP installation directory\apache\conf\extra\httpd-vhosts.conf

Look for this line:

#NameVirtualHost *:80

Un-comment it so that it reads without the pound sign before it:

NameVirtualHost *:80

Each “virtual host” has its own set up. You’ll need one for the default “localhost” name, so that when you type “localhost”, you get all the XAMPP utilities.

<VirtualHost *:80>
    DocumentRoot "C:\xampp\htdocs"
    ServerName localhost
    <Directory "C:\xampp\htdocs">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

To create any arbitrary virtual host, you basically create a new definition using <VirtualHost *:80>. E.g. to create one for “upvector.local”, you’d use this code:

<VirtualHost *:80>
    DocumentRoot "C:\webprojects\upvector"
    ServerName upvector.local
    <Directory "C:\webprojects\upvector">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

Configuring the Hosts file

Once you’ve configured Apache, you’ll need to configure your operating system’s “hosts” file. This will let your OS know that when you call a particular network host name, it will route it back to your computer.

To find your hosts file, go to this page on Wikipedia where they have documented the hosts file on almost every operating system.

On Windows, it is typically here:

C:\Windows\system32\drivers\etc\hosts

I recommend that you use Notepad ++ to edit text files. Make sure that when you run Notepad++ (or whatever text editing app), that you RIGHT-CLICK and RUN AS ADMINISTRATOR. If you don’t run the program as an administrator, you’ll be unable to write to the hosts file.

For each of the virtual hosts that you want to run, add:

127.0.0.1        host_name

E.g.

127.0.0.1        upvector.local

Restart Apache. If you did everything correctly, it should start back up and you’ll be able to type the host name into your browser and it’ll serve the correct website.

Star Wars Subway Car

Posted: July 15th, 2010     Nerdicious   Rambling

Because…you know…we’re nerds… :)

YouTube Preview Image
 
Leonard Teo
CEO, Ballistiq
Montreal, Canada