How do I setup “name based” virtual hosts using Ubuntu 12.04?
Solution 1:
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.
Solution 2:
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>
Solution 3:
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