Virtual Hosts
Running several name-based web
sites on a single IP address.
You
must have the names in DNS, resolving to your IP
address, or nobody else will be able to see your web site. You
can put entries in your hosts file for local
testing,
Listen 80 #this is default
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example1.com
# Other directives here
</VirtualHost>
or
NameVirtualHost 172.20.30.40
<VirtualHost 172.20.30.40> ##172.20.30.40 is real ip of system
DocumentRoot /www/example2
ServerName www.example2.org
# Other directives here
</VirtualHost>
The asterisks match all addresses, so the main server serves no
requests. Due to the fact that
www.example1.com is first
in the configuration file, it has the highest priority and can be seen
as the
default or
primary server. That means
that if a request is received that does not match one of the specified
ServerName directives, it will be served by this first
VirtualHost.
------------------------------------------------------------------------------------------------
For example, suppose that you are serving the domain
www.domain.tld and you wish to add the virtual host
www.otherdomain.tld, which points at the same IP address.
Then you simply add the following to
httpd.conf:
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>
You can alternatively specify an explicit IP address in place of the
* in both the
NameVirtualHost and
<VirtualHost> directives. For example, you might want to do this
in order to run some name-based virtual hosts on one IP address, and either
IP-based, or another set of name-based virtual hosts on another address.
Many servers want to be accessible by more than one name. This is
possible with the
ServerAlias
directive, placed inside the
<VirtualHost> section. For example in the first
<VirtualHost> block above, the
ServerAlias directive indicates that
the listed names are other names which people can use to see that same
web site:
ServerAlias domain.tld *.domain.tld
then requests for all hosts in the
domain.tld domain will
be served by the
www.domain.tld virtual host. The wildcard
characters
* and
? can be used to match names.
Of course, you can't just make up names and place them in
ServerName or
ServerAlias. You must
first have your DNS server properly configured to map those names to an IP
address associated with your server.
The complete list of names in the
VirtualHost directive are treated just like a (non wildcard)
ServerAlias.
Finally, you can fine-tune the configuration of the virtual hosts
by placing other directives inside the
<VirtualHost> containers. Most directives can be
placed in these containers and will then change the configuration only of
the relevant virtual host. To find out if a particular directive is allowed,
check the
Context of the
directive. Configuration directives set in the
main server context
(outside any
<VirtualHost>
container) will be used only if they are not overridden by the virtual host
settings.
Now when a request arrives, the server will first check if it is using
an IP address that matches the
NameVirtualHost. If it is, then it will look at each
<VirtualHost> section with a matching
IP address and try to find one where the
ServerName or
ServerAlias matches the requested
hostname. If it finds one, then it uses the configuration for that server.
If no matching virtual host is found, then
the first listed virtual
host that matches the IP address will be used.
As a consequence, the first listed virtual host is the
default
virtual host. The
DocumentRoot from
the
main server will
never be used when an IP
address matches the
NameVirtualHost
directive. If you would like to have a special configuration for requests
that do not match any particular virtual host, simply put that configuration
in a
<VirtualHost>
container and list it first in the configuration file.
Name-based hosts on more than one
IP address.
# This is the other address
NameVirtualHost 172.20.30.50
<VirtualHost 172.20.30.50>
DocumentRoot /www/example1
ServerName www.example1.com
# Other directives here ...
</VirtualHost>
<VirtualHost 172.20.30.50>
DocumentRoot /www/example2
ServerName www.example2.org
# Other directives here ...
</VirtualHost>
Any request to an address other than 172.20.30.50 will be
served from the main server. A request to 172.20.30.50 with an
unknown hostname, or no Host: header, will be served from
www.example1.com.
Serving the same content on
different IP addresses (such as an internal and external
address).
The server machine has two IP addresses (
192.168.1.1
and
172.20.30.40). The machine is sitting between an
internal (intranet) network and an external (internet) network. Outside
of the network, the name
server.example.com resolves to
the external address (
172.20.30.40), but inside the
network, that same name resolves to the internal address
(
192.168.1.1).
The server can be made to respond to internal and external requests
with the same content, with just one
VirtualHost
section.
Server configuration
NameVirtualHost 192.168.1.1
NameVirtualHost 172.20.30.40
<VirtualHost 192.168.1.1 172.20.30.40>
DocumentRoot /www/server1
ServerName server.example.com
ServerAlias server
</VirtualHost>
Now requests from both networks will be served from the same
VirtualHost.
Note:
On the internal
network, one can just use the name
server rather
than the fully qualified host name
server.example.com.
Note also that, in the above example, you can replace the list
of IP addresses with
*, which will cause the server to
respond the same on all addresses.
Running different sites on different
ports.
You have multiple domains going to the same IP and also want to
serve multiple ports. By defining the ports in the "NameVirtualHost"
tag, you can allow this to work. If you try using <VirtualHost
name:port> without the NameVirtualHost name:port or you try to use
the Listen directive, your configuration will not work.
Server configuration
Listen 80
Listen 8080
NameVirtualHost 172.20.30.40:80
NameVirtualHost 172.20.30.40:8080
<VirtualHost 172.20.30.40:80>
ServerName www.example1.com
DocumentRoot /www/domain-80
</VirtualHost>
<VirtualHost 172.20.30.40:8080>
ServerName www.example1.com
DocumentRoot /www/domain-8080
</VirtualHost>
<VirtualHost 172.20.30.40:80>
ServerName www.example2.org
DocumentRoot /www/otherdomain-80
</VirtualHost>
<VirtualHost 172.20.30.40:8080>
ServerName www.example2.org
DocumentRoot /www/otherdomain-8080
</VirtualHost>
IP-based virtual hosting
The server has two IP addresses (
172.20.30.40 and
172.20.30.50) which resolve to the names
www.example1.com and
www.example2.org
respectively.
Server configuration
Listen 80
<VirtualHost 172.20.30.40>
DocumentRoot /www/example1
ServerName www.example1.com
</VirtualHost>
<VirtualHost 172.20.30.50>
DocumentRoot /www/example2
ServerName www.example2.org
</VirtualHost>
The server machine has two IP addresses (
172.20.30.40 and
172.20.30.50) which resolve to the names
www.example1.com and
www.example2.org
respectively. In each case, we want to run hosts on ports 80 and
8080.
Server configuration
Listen 172.20.30.40:80
Listen 172.20.30.40:8080
Listen 172.20.30.50:80
Listen 172.20.30.50:8080
<VirtualHost 172.20.30.40:80>
DocumentRoot /www/example1-80
ServerName www.example1.com
</VirtualHost>
<VirtualHost 172.20.30.40:8080>
DocumentRoot /www/example1-8080
ServerName www.example1.com
</VirtualHost>
<VirtualHost 172.20.30.50:80>
DocumentRoot /www/example2-80
ServerName www.example1.org
</VirtualHost>
<VirtualHost 172.20.30.50:8080>
DocumentRoot /www/example2-8080
ServerName www.example2.org
</VirtualHost>
Mixed name-based and IP-based
vhosts
Server configuration
Listen 80
NameVirtualHost 172.20.30.40
<VirtualHost 172.20.30.40>
DocumentRoot /www/example1
ServerName www.example1.com
</VirtualHost>
<VirtualHost 172.20.30.40>
DocumentRoot /www/example2
ServerName www.example2.org
</VirtualHost>
<VirtualHost 172.20.30.40>
DocumentRoot /www/example3
ServerName www.example3.net
</VirtualHost>
# IP-based
<VirtualHost 172.20.30.50>
DocumentRoot /www/example4
ServerName www.example4.edu
</VirtualHost>
<VirtualHost 172.20.30.60>
DocumentRoot /www/example5
ServerName www.example5.gov
</VirtualHost>
Multiple SSL Certificate
install on apache2 server [Ubuntu 12.04]
Enable SSL on your webserver
sudo a2enmod ssl
Follow
up by restarting Apache.
sudo service apache2
restart
Copy your SSL certificate and key to any location
Eg. Here we copied to /etc/apache2/ssl
Make sure permission of those files are 400 or 600 [root
only read/write]
In
sites/sites-available/<your domains>
vim btlg.com.au
<VirtualHost *:80>
ServerName btlg.com.au
ServerAlias www.btlg.com.au
DocumentRoot /home/bootleg/public_html
</VirtualHost>
<VirtualHost *:443>
ServerName btlg.com.au
ServerAlias www.btlg.com.au
DocumentRoot /home/bootleg/public_html
SSLEngine on
SSLCertificateFile
/etc/apache2/ssl/btlg/281027f6c6f445.crt
SSLCertificateKeyFile
/etc/apache2/ssl/btlg/www_btlg_com_au.key
SSLCertificateChainFile
/etc/apache2/ssl/btlg/sf_bundle-g2-g1.crt
</VirtualHost>
vim example.com.au
<VirtualHost *:80>
ServerName btlg.com.au
ServerAlias example.com.au
DocumentRoot /home/example.com/public_html
</VirtualHost>
<VirtualHost *:443>
ServerName example.com.au
ServerAlias www.example.com.au
DocumentRoot /home/example.com/public_html
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/example/281027f6c6f445.crt
SSLCertificateKeyFile
/etc/apache2/ssl/example/www_example_com_au.key
SSLCertificateChainFile
/etc/apache2/ssl/btlg/sf_bundle-g2-g1.crt
</VirtualHost>
Restart apache2 and browse to your domain with http or https
Restricting virtual host by ip address example
<virtualhost *:80>
ServerName mail.rpmanpower.com
ServerAlias mail.rpmanpower.com
ServerAdmin zadmin@localhost
DocumentRoot "/etc/zpanel/panel/etc/apps/webmail"
<Location />
Order deny,allow
Deny from all
Allow from 182.93.95.225 198.50.239.107
</Location>
</virtualhost>
this will restrict access to all ip address to mail.rpmanpower.com
EXCEPT 182.93.95.225 198.50.239.107 thest TWO CAN ACCESS
Deny only one ip 192.168.16.100
<Location /server-status>
SetHandler server-status
Order Allow,Deny
Deny from 192.168.16.100
Allow from 192.168.16.0/24
</Location>
Deny from all is not needed. In fact it will screw up because everything will match
all, and thus denied (and I think Apache is trying to be smart and do something stupid). I have always found Apache's
Order,
Allow and
Deny directives confusing, so always visualize things in a table (taken from
the docs):
Match | Allow,Deny result | Deny,Allow result
-------------------------------------------------------
Allow only | Allowed | Allowed
Deny only | Denied | Denied
No match | Default: Denied | Default: Allowed
Match both | Final match: Denied | Final match: Allowed
With the above settings:
- Requests from 192.168.16.100 get "Match both" and thus denied.
- Requests from 192.168.16.12 get "Allow only" and thus allowed.
- Requests from 123.123.123.123 get "No match" and thus denied.