Apache2 virtual hosts not working

16,505
  1. Make sure you have virtual hosts enabled in httpd.conf:

    NameVirtualHost *:80
    NameVirtualHost *:443
    
  2. ServerName in each VirtualHost directive must reflect the hostname it will be serving.

    ServerAlias should only be used when necessary. It should be additional name(s) the virtual host should respond to. It should not be the same as ServerName.

    The first VirtualHost directive will be used as the default when no other matches or the client does not provide a Host: header (older versions of HTTP don't require this header).

  3. The <Directory /> directive should probably be in the global configuration instead of being repeated in every virtual host configuration.. Works either way, I'm just a stickler for clean configs.

Make sure you're restarting Apache after any changes to the configuration file(s).

Edit:
I saw your attempt at responding, indicating that you're receiving an error that you have no VirtualHosts configured on *:80. Your virtual hosts are in an include file, so it must be getting included in the configuration for some reason.

My first guess is that the include path is wrong (though I would think that'd come from the package that way, so it aught to be right - of course the various Linux distributions make Apache so needlessly complex that it drives me straight up the wall... but I digress). Usually a relative path is based in Apache's install base. Since I'm not sure if that's correct, and it's apparently not loading the files, you might want to change that to the absolute path and see what happens (ie /opt/apache22/enabled-sites, or whatever it is on your system).

Also, you can comment out the line that's there with a standard hash (#), then add your own, so you don't lose the original configuration.

Share:
16,505
Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    Running Ubuntu 12.04 LTS, Apache2.2.22. Running this on a home server through one IP. Problem running virtual hosts. I have a domain name registered with godaddy. lets say mysite.com, goal is to have www.mysite.com and test.mysite.com. Currently only www.mysite.com is working, if I try test.mysite.com it displays the content of www.mysite.com(I did make sure the content of both sites is different). I have created two files in sites-available and ran the appropriate A2ensite www.mysite.com and A2ensite test.mysite.com. Below are the two virtual host files:

    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/test.mysite.com
        ServerName mysite.com
        ServerAlias test.mysite.com
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/test.mysite.com/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all         
        </Directory>
    

    Here is www.mysite.com virtual host file:

    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www.mysite.com
        ServerName mysite.com
        ServerAlias www.mysite.com
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www.mysite.com/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>
    

    On Godaddy domain management I have @ pointed to my public IP, I have an "A" record for www pointed to my public IP and I have a "A" record for test pointed to public ip. Any help to what I am doing wrong would be greatly appreciated. Thank you all.

  • Admin
    Admin over 10 years
    Thanks for the response, I just tried and no difference.
  • Thalys
    Thalys over 10 years
    You can use the "contact us" link to merge your accounts.