Apache2 virtual hosts not working
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 and test.mysite.com. Currently only is working, if I try test.mysite.com it displays the content of (I did make sure the content of both sites is different). I have created two files in sites-available and ran the appropriate A2ensite 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/ Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory>Here is virtual host file:
<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/ ServerName mysite.com ServerAlias <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/ 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.
14 Answers
Make sure you have virtual hosts enabled in
httpd.conf:NameVirtualHost *:80 NameVirtualHost *:443ServerNamein eachVirtualHostdirective must reflect the hostname it will be serving.ServerAliasshould only be used when necessary. It should be additional name(s) the virtual host should respond to. It should not be the same asServerName.The first
VirtualHostdirective 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).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.
For test.mysite.com set ServerName to test.mysite.com
Since I can't comment, I will update what fixed the problem so others may find it helpful. It was a case of user error. I overlooked checking the sites-enabled folder to view which sites were enabled, and test.mysite.com was not there. I ran a2ensite test.mysite.com and restarted Apache and now it is working.
The right configs should be this for each sites
for test.mysite.com
<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/test.mysite.com ServerName test.mysite.com <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/ Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory>For mysite.com
<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/ ServerName mysite.com <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/ Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory>The ServerAlias work like this.
If you want the same virtualhost to have other addres than you use ServerAlias Example:
ServerName mysite.com
ServerAl