Apache + Nginx Virtual Host

This guide helps anyone who is looking to host multiple domains with Apache HTTP or Nginx webservers.

Before you implement these instructions from this guide, you need your DNS mapped to a host server, such as attainpr.co.uk or attaindigital.co.uk

The structure of hosting multiple websites on a single server is known as a ‘Virtual Server’. Virtual servers allow you to use a single servers IP address and the host getting sent to a Document Root, configurated by the Virtual Server.

Configuration for Apache

V1: Log in to the Apache conf location (Path: /etc/httpd.conf is the default location after installation)
V2: Use a backup of your httpd.conf file for safety
V3: Use the snippet below to create a Virtual Host. In this example, we are showing two domains

<virtualhost *:80="">
    ServerName www.attainpr.co.uk
    DocumentRoot /var/www/attainpr/public_html
    <directory var="" www="" attainpr="" public_html="">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </directory>
    ErrorLog /var/log/weblogs/attainpr_error.log
    CustomLog /var/log/weblogs/attainpr_requests.log combined
</virtualhost>
<virtualhost *:80="">
    ServerName www.attaindigital.co.uk
    DocumentRoot /var/www/attaindigital/public_html
    <directory var="" www="" attaindigital="" public_html="">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </directory>
    ErrorLog /var/log/weblogs/attaindigital_error.log
    CustomLog /var/log/weblogs/attaindigital_requests.log combined
</virtualhost>

Please note: Placeholder values are in this example. Make sure you change the value for ServerAdmin, Servername, ErrorLog, CustomLog and DocumentRoot based on your values for your requirements. In this example, I created a new log directory in /var/www/weblogs, but you can use default locations.

V4: Restart Apache HTTP and test your URLs

Virtual Host Configuration for Nginx

N1: Log in to the Ngnix Server
N2: Go to virtual.conf location (Path: /etc/nginx/conf.d/virtual.conf is the standard location after configuration of NginX)
N3: Use a backup of your virtual.conf file as a precaution
N4: Use the code below to create a server block for both URLs. The example shown creates two blocks

server {
    listen 80;
    root /opt/htdocs/attaindigital;
    index indext.html;
    server_name attaindigital.co.uk;
    location / {
        try_files $uri $uri/ =404;
    }
}
server {
    listen 80;
    root /opt/htdocs/attainpr;
    index indext.html;
    server_name attainpr.co.uk;
    location / {
        try_files $uri $uri/ =404;
    }
}

Please note: Just as in the first example, placeholder names are examples. Make sure you change the root value, server_name based on your needs.

N5: Relaunch Nginx and check your URLs

This quick and straightforward guide to hosting multiple domains allows the possibility of using a single web instance, easy and straightforward.

Also, don’t forget your security! Once your website is live, we recommend you test it against potential security threats before any traffic comes in.

Categories:

One response

Leave a Reply

Your email address will not be published. Required fields are marked *