Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How do I setup “name based” virtual hosts using Ubuntu 12.04?

I have followed the instructions given at https://help.ubuntu.com/12.04/serverguide/httpd.html#http-configuration .

I've done the following...

  1. cp default newsite
  2. replace “/var/www” with “/var/www/newsite” in newsite
  3. add “ServerName newsite.example.com” to newsite

After some research, I found a blog entry that stated I needed to disable the default site using a2dissite default. After I did that, it worked. Is that correct? This is never mentioned in the Ubuntu server guide. The guide also includes this line...

“The default virtual host has no ServerName directive specified, so it will respond to all requests that do not match a ServerName directive in another virtual host.”

Which seems to imply that both the default site and others can co-exist.

I'm running a fresh install of 12.04 Server and have reloaded the apache config each time I made an adjustment.

In summary... after adding a new file under /etc/apache2/sites-available (an altered copy of the “default” file with the ServerName directive added)  and the corresponding symlink under /etc/apache2/sites-enabled, is it necessary to disable or rename the default site symlink in order for the new site to function? The documentation and one answer given below seems to infer that it is not necessary to do so, but if so, what have I done wrong? Using the config below, when trying to access newsite.example.com I get the default site.

/etc/apache2/sites-available$ cat default

<VirtualHost *:80>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

/etc/apache2/sites-available$ cat newsite

<VirtualHost *:80>

        DocumentRoot /var/www/newsite
        ServerName newsite.example.com
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/newsite/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

/etc/apache2/sites-enabled# ls -l

root@sandbox:/etc/apache2/sites-enabled# ls -l
total 0
lrwxrwxrwx 1 root root 26 Mar 18 09:56 000-default -> ../sites-available/default
lrwxrwxrwx 1 root root 26 Mar  7 13:36 newsite -> ../sites-available/newsite
share|improve this question
    
since you're showing what you have under sites-available it is not clear what you have under sites-enabled. I suggest to cat files under sites-enabled since these are the ones apache actually loads. –  arielf Mar 18 '13 at 3:02
    
@arielf please see lastest edit –  Corey Mar 18 '13 at 14:00
    
Very confused about this, I setup a VM with a fresh install and followed the same steps and it worked as it should. As far as I can see the config files match, but the behavior doesn't. –  Corey Mar 18 '13 at 20:14
    
The newsite file/section has a ServerName, but the default site doesn't, so it matches any ServerName. And because default is loaded 1st ('d' comes before 'n') it wins. Clearer now? –  arielf Mar 19 '13 at 0:05
    
I don't believe that's correct. –  Corey Mar 19 '13 at 21:01

5 Answers 5

This is more of an apache configuration question, than a Ubuntu one.

Yes, you may run multiple virtual servers on one host, each serving separate content, provided that they all map (e.g. via DNS) to the same server.

The official documentation on how to create virtual servers (version 2.2 but this feature hasn't fundamentally changed between versions) can be found here:

httpd.apache.org/docs/2.2/vhosts/

The short answer is you need to:

  • define your virtual hosts
  • include some mapping between your host names and the content they serve

This is done by adding a virtual host clause to some apache config file, e.g. under /etc/apache2/sites-available/000-add-my-virtual-hosts (name designed specifically to precede the 000-default name in alphabetic order)

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName hostname1.mydomain.com
    DocumentRoot /home/www/hostname1
</VirtualHost>

<VirtualHost *:80>
    ServerName hostname2.mydomain.com
    DocumentRoot /home/www/hostname2
</VirtualHost>

Note that you may also need to add links from /etc/apache2/sites-enabled/ to /etc/apache2/sites-available if the site you need is already in the latter but not the former.

EDIT 1:
After reading the man page for a2dissite, it becomes clear that all it does is removing the symlink from /etc/apache2/sites-enabled/. The key is to understand that the order in which these configs are processed can affect the end result. The default site is called 000-default in order to be loaded first. If it matches all sites, i.e. acts as a 'anything else' wildcard, then you won't see the others. Try renaming the link to have a higher number like 999-default so it is loaded last (after the other sites matched).

EDIT 2: To your updated question: yes, it is necessary to rename or delete the default site because its config file name starts with '000' making it load first and 'take-over' due to the wildcard matching. I suppose the documentation can be improved on this point.

EDIT 3: The order in which server names appear, its importance and more is documented on this apache page in the section Name-based vhost One of the relevant sentences says:

The first vhost on this list (the first vhost in the config file with the
specified IP address) has the highest priority and catches any request to
an unknown server name or a request without a Host: header field.

and later under Observations:

... the ordering of name-based vhosts for a specific address set is significant.
The one name-based vhosts that comes first in the configuration file has the
highest priority for its corresponding address set.
share|improve this answer
3  
My question is specific to a Ubuntu based server. After following the official Ubuntu guide, I was unable to get my name based virtual host running. Only the default site was displaying. I had to disable the default site in-order for my new site to work. That is never mentioned in the guide. It seems to me that the guide is flawed or leaves a step out, but I'm looking for someone to confirm that or correct me. –  Corey Mar 11 '13 at 15:01
    
so it seems to me that the Ubuntu guide is leaving some steps out –  Corey Mar 11 '13 at 21:04
    
@Corey I see no problem with the instructions on the website. Though I've found when doing common things like this your best bet is to google something like "ubuntu apache virtual host" in lieu of starting with documentation pages –  David Mar 12 '13 at 2:40
    
@David I've updated the question and added a summary to clear up my true question –  Corey Mar 17 '13 at 14:38
    
@David The first quote in your 3rd edit seems to support the fact that deleting or altering the file name of the default host file is not necessary if the new host file includes a ServerName directive. My new site config includes the “ServerName newsite.example.com” directive, and therefore should respond to any request for that website. –  Corey Mar 17 '13 at 23:22

Using Ubuntu to host Apache the vhost definition in case of Debian based systems the definiton of websites is done on

/etc/apache2/sites-enabled/*.conf

where *conf corresponds to

internal1.conf internal2.conf internal3.conf internal4.conf

The vhost definition of each of these sites will be as follows

/etc/apache2/sites-enabled/internal1.example.conf

when you say

The default virtual host has no ServerName directive specified, so it will 

respond to all requests that do not match a ServerName directive in another virtual host.

that means

When apache serves websites from many vhost it reads the file names in alphabetical order that is the reason the default file which you mentioned had the number 000-default so naming of hosts is actually important.

if a request comes for something.something to your server and it reads vhosts files in alphabetical order in /etc/apache2/sites-enabled/

and it does not finds the required configuration file in sites-enabled directory
it (apache2) will serve the alphabetically first vhost file so a good tip is always to use a default 000-default file either blank or to point to some error page as you want

Now you can have configuration as follows for internal1.example.com

<virtualhost *:80>

    ServerAdmin webmaster@yoursite
    DocumentRoot /var/www/yoursite <--this is an important place should be there
    ServerName yourservername
    ServerAlias www.mydomain.com  <-- if you do not need this do not put it
    ErrorLog /var/logs/apache2/yoursite/error_log  <--logs can be customized 
    CustomLog /var/logs/apache2/yoursite/access_log common
</VirtualHost>

like above example you can make internal2.example.conf,internal3.example.conf and so on you do not need other lines in tags if you do not need them in your setup may be you can have a look at this link http://www.debian-administration.org/articles/18

share|improve this answer

I don't know why this is often glossed over but the important keyword in your site conf file is "ServerAlias" here is an example conf file with that set

NameVirtualHost *:80

<VirtualHost *:80>
ServerName www.domain.tld
ServerAlias domain.tld *.domain.tld
DocumentRoot /www/domain
</VirtualHost>

<VirtualHost *:80>
ServerName www.otherdomain.tld
DocumentRoot /www/otherdomain
</VirtualHost>
share|improve this answer

Just to add to David's post, although I'm not running Ubuntu I am running Apache and I noticed that I could get to the site using the full www.domain.com but when I used it without the www it would just use the first site defined (which is the default). Note that the DNS was set correctly in that www is set to @ (hostname) so that was correct.

So by adding the ServerAlias directive it solved the issue. I'm still going through the documentation so there may be other ways to solve it too.

My config file was (in part):

ServerName www.domain.com

When I changed it to (adding the ServerAlias line):

ServerName domain.com ServerAlias www.domain.com

that solved the problem. I plan on testing David's config options and using just an *.domain.com so things like ftp.domain.com or smtp.domain.com will work.

share|improve this answer

@Corey

It sounds like you have a handle on the apache configuration files. And you know where to look for the documentation. If I understand correctly your unsure about using the a2ensite utility. According to the man pages of a2ensite:

a2ensite is a script that enables the specified site (which contains a block) within the apache2 configuration.It does this by creating symlinks within /etc/apache2/sites-enabled. Likewise, a2dis- site disables a site by removing those symlinks. It is not an error to enable a site which is already enabled, or to disable one which is already disabled

.

Any help you might need can be found at the Apache website. It is very terse and dry documentation, but it is worth your time to familiarize yourself

share|improve this answer
    
I've updated the question and added a summary to clear up my true question –  Corey Mar 17 '13 at 14:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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