I am attempting to setup virtual hosts so that I can host multiple sites on Apache2.

I have currently got two files in my /etc/apache2/sites-available folder, each containing the following:

<VirtualHost *:80>

    ServerName www.domain.com
    ServerAlias domain.com

    DirectoryIndex index.php
    DocmentRoot /var/www/domain

</VirtualHost>

Obviously the 'domain' changes to the domain for the two websites I am trying to host.

The problem is that when I go to domain.com and then domain2.com it only loads up domain.com in both examples...

Please could someone suggest what is going on?
I am using Ubunt 12.04

Thanks

Dani AI

Generated

— the most likely causes are a small typo in your vhost file and Apache still using a single “default” vhost. is right to point at /etc/hosts/DNS, but also check the vhost syntax and which sites are actually enabled. Name-based vhosting must be configured and each vhost file must be parsed correctly (a misspelled directive will stop Apache from creating a separate host).

Quick checklist and fixes to try:

  • Fix the typo DocumentRoot (your post shows DocmentRoot).

  • Make sure name-based vhosting is enabled (on Ubuntu 12.04 / Apache 2.2 you need NameVirtualHost *:80 in /etc/apache2/ports.conf).

  • Enable your sites and disable the default site so Apache doesn’t always serve the first vhost:

    sudo a2ensite yoursite.conf
    sudo a2dissite 000-default.conf
    sudo apache2ctl -t
    sudo service apache2 reload
  • Example vhost (Apache 2.2 style used on Ubuntu 12.04 — use Require all granted for Apache 2.4):

    <VirtualHost *:80>
        ServerName domain.com
        ServerAlias www.domain.com
        DocumentRoot /var/www/domain
        <Directory /var/www/domain>
            Options Indexes FollowSymLinks
            AllowOverride All
            Order allow,deny
            Allow from all
        </Directory>
    </VirtualHost>

Testing and diagnostics:

  • Verify which vhosts are enabled: ls -l /etc/apache2/sites-enabled (Apache serves the first enabled vhost as the default).
  • Test HTTP host routing from the server: curl -I -H "Host: domain2.com" http://127.0.0.1/ to see which vhost responds.
  • Check sudo tail -n 50 /var/log/apache2/error.log for parse errors and apache2ctl -t for config syntax issues.
  • Ensure DNS or /etc/hosts points both names to your server IP and that the DocumentRoot folder exists and is readable by www-data.

Following these steps will show whether the issue is name resolution, a parsing error (typo), or an enabled/default-site ordering problem.

Check /etc/hosts or DNS

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.