I have set up a Ubuntu Linux VPS with Moonshine (a Ruby on Rails deployment plugin/script) and hosting multiple sites on it (despite Moonshine being intended for one site/application), so far working well.

One little issue that I have with my server is that whenever I accidentally set up a vhost wrong or a vhost is not found, the browser is directed to the very first vhost site that I set up. Which concerns me because I don't want one site accidentally being associate with the other in case of some error or issue; what I would "rather" have it do is direct to some default place (like /var/www/default/oops.html) if there are any issues.

I've Googled the subject and the common answer is that order is everything in the vhost files, except I think that is assuming I have ALL my vhosts in one file. Instead, my vhosts are set up separately in the sites-available/enabled folders of apache (mydomain1.com, mydomain2.com, etc) and 'a2ensite' respectively, so how would I go about "ordering" these? (One of these files is a 'default' file, but doesn't seem to do anything.)

One other thing I saw was specify a default host in httpd.conf, except when I attempted to do so, it completely redirected ALL my vhost sites to the default.


On Ubuntu your vhosts will be loaded via Include statements within the /etc/apache2/apache2.conf configuration file. You're right that Apache treats the first VirtualHost it finds as the default if it can't match a request but just because the VirtualHost directives aren't in the same file doesn't stop you from ordering them.

Down the bottom of /etc/apache2/apache2.conf you can find the line:

# Include the virtual host configurations:
Include sites-enabled/

This is where Apache is picking up your VirtualHosts specified in /etc/apache2/sites-enabled/ so you've got two options:

Put an include to add a new VirtualHost above the above Include line in the apache2.cnf config file.

Or order the files in sites-enabled. I'm not sure on the exact mechanism Apache uses to read these files but I'm assuming it's alphabetical with numbers first (in the same way ls would list a directory). This is hinted at by the included default site on Ubuntu being called 000-default. So based on that, to create a new default VirtualHost, you need to create a new VirtualHost config in sites-available called (for instance) 00_default_vhost.conf and then enable it with a2ensite.

  • Completely didn't occur to me about the number ordering. I basically renamed default to 00-default and restarted Apache, now it goes exactly to the default where I specified. Thank you much. – Shannon May 14 '11 at 21:23

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.