How to Test Multiple Websites on One PC With Apache Virtual Hosts

    Craig Buckler
    Craig Buckler
    Share
    Run multiple sites
    It is rare to find a web developer with responsibility for just one website. In this article, we will configure your development PC so you can test any number of websites using a dedicated domain name for each one. You will require a local installation of Apache 2.2 and, optionally, PHP and MySQL.

    The Heavenly Hosts File

    When you enter an address in your browser, the domain name is normally converted to an IP address by a domain name server. Just prior to that, the computer will check its own hosts file. Host files are available on nearly every operating system:
    • Windows NT, 2000, XP, 2003 and Vista: %WinDir%system32driversetchosts (note this location can be changed in the registry key HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesTcpipParametersDataBasePath)
    • Windows 95, 98, ME: %WinDir%hosts
    • Linux, Unix, BSD: /etc/hosts
    • Mac OS X: /private/etc/hosts
    In most cases, your hosts files will contain a single entry:
    
    127.0.0.1  localhost
    
    Entering ‘http://localhost/’ in your browser will resolve to the TCP/IP loop-back address 127.0.0.1 (your PC). The hosts file can contain any number of domain mappings. Assume we want to test two sites locally: www.mysite1.com and www.mysite2.org. We can create two domains in our hosts file for testing purposes, e.g. mysite1 and mysite2
    :
    
    127.0.0.1  localhost
    127.0.0.1  mysite1
    127.0.0.1  mysite2
    
    Some operating systems will implement the change as soon as the hosts file is saved. Windows users should enter “nbtstat -R” at the command line. Try a reboot if all else fails.

    Configuring Apache Virtual Hosts

    Apache can run any number of websites on a single machine. (Note: so can the server versions of Microsoft IIS, but not the Windows Professional or VisualStudio.NET editions). Shut down Apache and load its configuration file, confhttpd.conf, in a text editor. Assuming we want the domain mysite1 to use the files in D:WebPagesmysite1 and mysite2 to use the files in D:WebPagesmysite2, we would add the following Virtual Host definitions to the bottom of the file:
    
    # Virtual hosts
    NameVirtualHost *:80
    
    # Any unspecified domain
    <VirtualHost *:80>
    	DocumentRoot D:/WebPages
    </VirtualHost>
    
    # mysite1 domain
    <VirtualHost *:80>
    	ServerName mysite1
    	DocumentRoot D:/WebPages/mysite1
    </VirtualHost>
    
    # mysite2 domain
    <VirtualHost *:80>
    	ServerName mysite2
    	DocumentRoot D:/WebPages/mysite2
    </VirtualHost>
    
    Save the file and restart Apache.

    Testing Your Sites

    Copy your website files to D:WebPagesmysite1 and D:WebPagesmysite2 accordingly. You can now enter either http://mysite1/ or http://mysite2/ in your browser to test the appropriate site. Bonus tip for PHP coders It can be useful to know whether the site is running on the live or development environment, e.g. debugging messages are not shown on the live site. You can detect the domain using code such as:
    
    <?php
    $debug == ($_SERVER['HTTP_HOST'] == 'mysite1');
    if ($debug) echo "debug message";
    ?>
    
    See also: Do you have any other tips for testing multiple websites on a single PC?

    Frequently Asked Questions on Hosting Multiple Websites with Apache Virtual Hosts

    What are the benefits of using Apache Virtual Hosts for multiple websites?

    Apache Virtual Hosts offer several benefits for hosting multiple websites. Firstly, it allows you to host multiple websites on a single server, which can significantly reduce hosting costs. Secondly, it provides isolation between websites, ensuring that issues with one site do not affect the others. Lastly, it offers flexibility as you can easily add, remove, or modify websites without affecting the server’s overall functionality.

    How can I secure my websites when using Apache Virtual Hosts?

    Securing your websites when using Apache Virtual Hosts involves several steps. Firstly, ensure that you regularly update your Apache server to the latest version to benefit from the latest security patches. Secondly, use SSL certificates for your websites to encrypt data and protect it from interception. Lastly, configure your server to limit access to sensitive areas and use strong, unique passwords for all accounts.

    Can I use different PHP versions for my websites with Apache Virtual Hosts?

    Yes, you can use different PHP versions for your websites when using Apache Virtual Hosts. This is particularly useful if you have websites that require different PHP versions to function correctly. You can specify the PHP version for each virtual host in the virtual host configuration file.

    How can I optimize the performance of my websites when using Apache Virtual Hosts?

    Optimizing the performance of your websites when using Apache Virtual Hosts involves several steps. Firstly, enable caching to reduce server load and improve page load times. Secondly, compress your website files to reduce their size and improve load times. Lastly, regularly monitor your server’s performance and adjust your configuration as needed to ensure optimal performance.

    Can I use Apache Virtual Hosts with a shared hosting plan?

    While it’s technically possible to use Apache Virtual Hosts with a shared hosting plan, it’s generally not recommended. Shared hosting plans often have restrictions that can limit the functionality of Apache Virtual Hosts. For the best results, it’s recommended to use a VPS or dedicated server.

    How can I troubleshoot issues with my Apache Virtual Hosts?

    Troubleshooting issues with your Apache Virtual Hosts involves checking several areas. Firstly, check your virtual host configuration files for errors. Secondly, check the Apache error logs for any error messages. Lastly, use tools like Apache’s mod_status to monitor your server’s performance and identify any potential issues.

    Can I use Apache Virtual Hosts to host websites for different clients?

    Yes, you can use Apache Virtual Hosts to host websites for different clients. Each client’s website can be hosted on a separate virtual host, providing isolation and allowing for individual configuration.

    How can I migrate my websites to a new server using Apache Virtual Hosts?

    Migrating your websites to a new server using Apache Virtual Hosts involves several steps. Firstly, backup your website files and databases. Secondly, configure the virtual hosts on the new server. Lastly, restore your website files and databases on the new server.

    Can I use Apache Virtual Hosts with a content management system like WordPress?

    Yes, you can use Apache Virtual Hosts with a content management system like WordPress. Each WordPress site can be hosted on a separate virtual host, allowing for individual configuration and isolation.

    How can I automate the management of my Apache Virtual Hosts?

    Automating the management of your Apache Virtual Hosts can be achieved using various tools. For example, you can use a control panel like cPanel or Plesk, which provides a graphical interface for managing your virtual hosts. Alternatively, you can use a configuration management tool like Ansible or Puppet, which allows you to automate the configuration of your virtual hosts.