File: /etc/hosts
127.0.0.1 site1.local
127.0.0.1 site2.local
127.0.0.1 site3.local
File: /etc/apache2/apache2.conf
[...]
NameVirtualHost *:80
[...]
Files: /etc/apache2/sites-available/site{i}.local.conf (instead of {i} insert number: 1 or 2 or 3)
<VirtualHost *:80>
ServerName site{i}.local
ServerAdmin webmaster@localhost
DocumentRoot /var/www/site{i}
[...]
</VirtualHost>
In directory /etc/apache2/sites-enabled/ create symbolic links:
@site{i}.local.conf
Restart apache and have fun :)
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.
Best Answer
When I had to do shuch things I proceed creating a VirtualHost whit a wildcard name:
/etc/hosts
file as127.0.0.1 *.example.com
mod_rewrite
create a wildcard VirtualHost in apache sites directory:
Now you can place any folder under
/srv/
and it will be the root for<foldername>.example.com
This is borrowed from memory, it could need some more minor adjustments and it may conflict with any other rewrite rules each webapp would have. But it fits me for my webapp testing needs.