WindowsMacSoftwareSettingsSecurityProductivityLinuxAndroidPerformanceConfigurationApple All

How to Set Up a Virtual Host in WAMPServer

Edited 1 day ago by ExtremeHow Editorial Team

WAMPServerVirtual HostConfigurationApacheSetupServerDevelopmentWindowsLocalhostWeb Development

How to Set Up a Virtual Host in WAMPServer

This content is available in 7 different language

WAMPServer is a Windows web development environment, which enables developers to create web applications with Apache2, PHP and MySQL database. One of the useful features that WAMPServer provides is the ability to set up virtual hosts. A virtual host allows you to run multiple websites on the same server. This is especially useful in a development environment, as it helps you create and test multiple projects on the same machine without any conflicts. In this detailed explanation, we will explore how to set up a virtual host in WAMPServer.

What is a virtual host?

A virtual host is a configuration that allows you to specify different domain names for different websites on the same server. This setup enables you to run multiple sites from one server by associating each site with its own separate hostname.

For example, you can access one website at www.example1.com and another at www.example2.com. Both websites may be hosted on the same server, but because of the virtual host they will behave as if they were hosted on different servers.

Pre-requisites for setting up a virtual host

Before you set up virtual hosts in WAMPServer, make sure you have the following:

Step-by-step guide to set up virtual hosts in WAMPServer

  1. Open WAMP Server

    Start by opening WAMPServer on your computer. You can do this by finding the WAMPServer icon on your desktop or in your Start menu and launching it. Once running, a green WAMPServer icon should appear in your system tray (the bottom right corner of your screen), indicating that all necessary services are running.

  2. Access the Apache configuration files

    Navigate to the WAMPServer directory on your system. By default, it is located at C:\wamp\ or C:\wamp64\ if you are using the 64-bit version. Inside this directory, locate bin folder, followed by apache folder. Inside apache folder, you will find folders named according to the installed version of Apache. Enter the appropriate folder and open conf subfolder.

  3. Modify httpd-vhosts.conf file

    Inside conf\extra folder, locate httpd-vhosts.conf file. This file holds the configuration for virtual hosts in Apache. Open this file using your chosen text editor.

    The file may already contain some example configurations that are commented out. These lines are for reference only and will not affect your setup unless commented out. To define a new virtual host, you must add new configuration blocks.

    Add the following configuration block to the file. Be sure to replace example.local with your desired domain name, and adjust the paths to point to the directory where your website files are stored.

    <VirtualHost *:80>
      ServerAdmin admin@example.local
      DocumentRoot "C:/wamp/www/example"
      ServerName example.local
      ServerAlias www.example.local
      ErrorLog "logs/example-error.log"
      CustomLog "logs/example-access.log" common
    </VirtualHost>

    Let's look at what each line in this block does:

    • <VirtualHost *:80>: This line tells Apache to listen for incoming requests on port 80 (HTTP). The asterisk (*) means it should accept connections from any IP address.
    • ServerAdmin: This specifies the email address of the server administrator. If there is a problem with the server, error messages may suggest contacting this address.
    • DocumentRoot: This line specifies the directory where the web files for this virtual host are stored. Apache will serve files from this directory for this virtual host.
    • ServerName: This is the domain name for the virtual host. You will use this name in your browser to access the site (for example, example.local).
    • ServerAlias: This is an optional directive that specifies additional names that should also be resolved in this virtual host. In this case, example.local and www.example.local would both point to the same site.
    • ErrorLog: This determines the location of the log file where Apache will record error messages for this site.
    • CustomLog: This sets a log file to record access requests to the site. The format specified here is common, which is a standard log format.
  4. Modify the Windows hosts file

    To access your new virtual host using a web browser, you must update the Windows hosts file. This file maps domain names to IP addresses. To do this, follow these steps:

    1. Go to C:\Windows\System32\drivers\etc directory.
    2. Open the hosts file with a text editor as administrator. Before opening the file you may need to right-click on the text editor and select "Run as administrator".
    3. Add the following line to the end of the file. Make sure to replace example.local with the domain name you used in httpd-vhosts.conf file.
    4. 127.0.0.1 example.local
    5. Save and close the file.

    This entry tells your computer to resolve example.local to the local IP 127.0.0.1 (your own machine).

  5. Restart Apache

    After making changes to the Apache configuration, you must restart the Apache server for the changes to take effect. You can do this via the WAMPServer icon in the system tray. Left-click on the icon, hover over the "Apache" menu item, and then click "Restart service". The icon will change from red to green, indicating that the server is running correctly.

  6. Test the virtual host

    Now it's time to test the new virtual host setup. Open a web browser and navigate to the domain name you specified, such as http://example.local. If configured correctly, you should see an index page or directory listing displayed on the site, possibly from your specified document root directory.

    If you see the WAMPServer homepage instead, it means either the browser isn't recognizing the virtual host, or there may be a mistake in your setup.

  7. Troubleshooting common problems

    If the virtual host doesn't work as expected, consider these troubleshooting tips:

    • Double-check the syntax in both httpd-vhosts.conf and the hosts files.
    • Make sure WAMPServer is running and all services are active (the icon should be green).
    • Check for firewall settings or antivirus software that may be blocking Apache.
    • Clear the browser cache or try restarting the browser.
    • Double-check the directory paths used in the configuration file.
  8. Configuring additional virtual hosts

    To add more virtual hosts, repeat steps 3 to 6, creating unique sections in the httpd-vhosts.conf file for each new host. Make sure they have different ServerName and DocumentRoot values. Don't forget to add corresponding entries in the hosts file.

By following these steps, you can effectively set up virtual hosts in WAMPServer. This allows you to work on multiple web projects simultaneously on your local machine. Understanding and using virtual hosts is a basic skill for developers working in a web-based environment, facilitating better organization and management of multiple projects.

If you find anything wrong with the article content, you can


Comments