How to host multiple websites with an Apache server

Contents

Apache Virtual Hosts is a feature that enables you to host multiple independent websites from one Apache installation. Each site has its own file system directory and domain name. You can offer different sites to different visitors depending on the domain they are using.

Virtual hosts are created using the <VirtualHost> directive in Apache configuration files. They are regularly stored in the sites-available directory inside Apache installation location. On Debian systems, this is usually /etc/apache2; other distributions may use /etc/httpd.

We will assume that you already have Apache up and running.. We will create two separate virtual hosts, a.example.com Y b.example.com. There is no limit to the number of virtual hosts you can use; if you need a dozen sites on one server, Apache will.

RELATED: How to find the Apache configuration folder

Configure the sites

Each site needs its own filesystem directory. Will place the web portal files, as HTML, CSS y JavaScript, within this directory. It's called the DocumentRoot for Apache, since it is the root from which documents are served.

You can locate your sites anywhere on your system. You will often see virtual hosts stored in /var/www, with each site getting its own folder:

sudo mkdir -p /var/www/a.example.com
sudo mkdir -p /var/www/b.example.com

/var/www it is regularly owned by root, so we are using sudo to create the subdirectories. Then, change the ownership of the document roots to your own user and set the appropriate permissions. this will allow apache to read files while giving you write access so you can add your content.

sudo chown -R $USER:$USER /var/www/a.example.com
sudo chown -R $USER:$USER /var/www/b.example.com
sudo chmod -R 755 /var/www

Copy your web portal files to directories. We will use two simple index.html records:

a.example.com/index.html

<html>
    <body>
        <h1>a.example.com</h1>
    </body>
</html>

b.example.com/index.html

<html>
    <body>
        <h1>b.example.com</h1>
    </body>
</html>

Configuring Apache

You are now ready to configure your virtual hosts. create a new virtual host file for each of the sites. No matter the name of the file; by convention, generally matches the host name of your site.

Minimally, each virtual host must declare two properties:

  • ServerName – The host name (domain) from which the site will be useful.
  • DocumentRoot – The file system location to use for this virtual host.

Here is an example configuration for our two sites:

/etc/apache2/sites-available/a.example.com.conf

<VirtualHost *:80>
    ServerName a.example.com
    DocumentRoot /var/www/a.example.com
</VirtualHost>

/etc/apache2/sites-available/b.example.com.conf

<VirtualHost *:80>
    ServerName b.example.com
    DocumentRoot /var/www/b.example.com
</VirtualHost>

This basic configuration is enough for the two sites to be active!! the *:80 means that apache will consider using this virtual host for any requests that reach the port 80. ServerName statements.

Enabling Virtual Hosts

Add a virtual host to sites-available does it available but not active. you must manually enable each site that you want to use. Virtual host configurations to enable must be symbolically linked to /etc/apache2/sites-enabled (O /etc/httpd/sites-enabled).

On Debian systems, you can use the a2ensite command to simplify this step:

a2ensite a.example.com
a2ensite b.example.com

In other distributions, use el ln command to create a symbolic link manually.

sudo ln -s /etc/httpd/sites-available/a.example.com.conf /etc/httpd/sites-enabled/a.example.com.conf
sudo ln -s /etc/httpd/sites-available/a.example.com.conf /etc/httpd/sites-enabled/b.example.com.conf

By last, restart Apache to apply your new configuration. Virtual host changes require a server restart after you have added them to sites-enabled.

sudo apache2ctl restart

You should now be able to see Apache serving its two different sites. If you don't have DNS resolving on your server, you can modify the /etc/hosts file to test your configuration.

Add the following lines to /etc/hosts:

127.0.0.1 a.example.com
127.0.0.1 b.example.com

This will force both domains to resolve on their own system. You can now visit them in your browser to see the two different virtual host pages.

Site aliases

If you intend to serve the same site on multiple domains, Use the ServerAlias directive on your virtual host. Each alias will be considered when Apache compares your virtual host with incoming requests. The virtual host will be used if the request Host header matches a ServerAlias o la ServerName.

<VirtualHost *:80>
    ServerName a.example.com
    ServerAlias example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/a.example.com
</VirtualHost>

This example would serve a site in three domains, without having to repeat any configuration details. you should use ServerAlias to set multiple domains – Repeating ServerName is feasible but will override previous uses.

Other configuration options

Many of the Apache server configuration alternatives can be used with virtual hosts. Override the server's global configuration when Apache handles a request using the virtual host.

<VirtualHost *:80>
    ServerName a.example.com
    DocumentRoot /var/www/a.example.com
    DirectoryIndex my-index.html

    <Directory /var/www/a.example.com>
        AllowOverride All
        Options -Indexes
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

this virtual host applies many more configurations. my-index.html is served by default, instead of index.htmland custom registration locations are used. At the same time, additional settings are applied to the root of the document itself, using the Directory block. .htaccess Cancellations are possible (AllowOverride All) and Apache's default directory listing pages are disabled (Options -Indexes).

IP address hosts

Apache also supports IP-based hosts at the same time as the name-based hosts we've seen so far.. IP-based hosts are ideal when your server has multiple network interfaces, as an internal company network and the public Internet. you can serve a different site depending on the network interface used.

<VirtualHost 172.17.0.1>
    ServerName a.example.com
    DocumentRoot /var/www/a
</VirtualHost>

<VirtualHost 172.17.0.1>
    ServerName b.example.com
    DocumentRoot /var/www/b
</VirtualHost>

<VirtualHost 192.168.0.1>
    ServerName a.example.com
    DocumentRoot /var/www/a2
</VirtualHost

In this example, requests made to a.example.com via the interface with IP 192.168.0.1 would receive content other than those sent through 172.17.0.1. At the same time, users of the latter IP could have access b.example.com. This site is not exposed to users who connect through 192.168.0.1.

This approach enables you to use one server for your corporate intranet and public web portal.. Link internal sites to your server's LAN IP; run public hosts against your WAN address.

Virtual host match

Apache virtual host resolution routine it is well documented and quite simple.

In general, Apache tries to match the Host request header against a ServerName O ServerAlias field in a virtual host configuration. If there are multiple matches, Apache will use the First matching the virtual host it found.

Virtual hosts are sorted by file name. If you need one site to match before another, rename your config file so that it is sorted before the target site. You can add a numeric prefix like 000-a.example.com.conf. Most of the time, this will be unnecessary: if all your sites have unique domains, I should never find a conflict.

Conclution

Apache virtual hosts enable you to divide multi-site configuration into self-contained definition files. You can enable and disable each site independently by linking to sites-enabled.

A lots of configuration options are available for virtual hosts. You can override some Apache server settings by host, so you have full control over the configuration of each virtualized site.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.