Setup and Configuration Multi Domain NGINX on Ubuntu

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, avrg: 5.00)

Ubuntu Server is a variant of Ubuntu as the Operating System. it’s developed by Canonical and open source for free to use. Setup configuration multi-domain Ubuntu can be used for Networks and various services.

NGINX (Engine-X) is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server. here we are going to use NGINX as the web server and make configuration for multiple domains on the NGINX server block. the purpose of this tutorial we may run or host multi-domain/websites on single machine.

Prerequisites

You have domains or subdomain name pointing to your public server IP. on the following tutorial we will use lab.axfon.comand lab2.axfon.com


Step 1: Install NGINX as Web Server

To install NGINX stable version by default repository Ubuntu, use the following single command to update and install NGINX

sudo apt update && sudo apt install nginx

Once NGINX already installed, to check and verify Nginx service status type the command below

sudo service nginx status

If it is running and working properly you will see the output NGINX active (running) like below

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2019-01-01 22:54:35 UTC; 31s ago
     Docs: man:nginx(8)
  Process: 13787 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, sta
  Process: 13776 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=ex
 Main PID: 13790 (nginx)
    Tasks: 2 (limit: 665)
   Memory: 4.5M
   CGroup: /system.slice/nginx.service
           ├─13790 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─13792 nginx: worker process
Jan 01 22:54:35 axfon-lab systemd[1]: Starting A high performance web server and a reverse pr
Jan 01 22:54:35 axfon-lab systemd[1]: nginx.service: Failed to parse PID from file /run/nginx
Jan 01 22:54:35 axfon-lab systemd[1]: Started A high performance web server and a reverse pro
lines 1-16/16 (END)

To exit Nginx service status just press q on the keyboard


Step 2: Configuration the Firewall with UFW

UFW (Uncomplicated Firewall) is an Iptable interface to easily configure a firewall on your system. If it does not yet enable, it’s recommended to enable and setup the rule for Nginx

For the first, we have to add rule for SSH, let’s se the command line below

sudo ufw allow OpenSSH

Add the rule receive HTTP to Nginx

sudo ufw allow 'Nginx HTTP'

Then you will see on terminal

Rule added
Rule added (v6)'

Now enable ufw for Firewall.

sudo ufw enable

If prompted just pres Y to accept and continue and to check UFW status type the command below

sudo ufw status

You will see the output UFW active (running) like below

Status: active
To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Nginx HTTP                 ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
Nginx HTTP (v6)            ALLOW       Anywhere (v6)

NGINX web server now is ready on Ubuntu, now you may go to your web browser and visit your domain or IP. If you have not configured domain name yet and don’t know your IP, use the following command find out

sudo ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'

If everything is OK you will be presented on your browser with the default Nginx landing page as below


Step 3: Create Directory and Grant Permission

On this guides, we are going to create the directory which contains the website files for domain lab.axfon.com and lab2.axfon.com. (you may change these with your own domain name).

To create directory in the location /var/www use the following command

sudo mkdir -p /var/www/lab.axfon.com/public_html
sudo mkdir -p /var/www/lab2.axfon.com/public_html

Set permission ownership for current user to be able to modify any files in these directories.

sudo chown -R $(whoami):$(whoami) /var/www/lab.axfon.com/public_html
sudo chown -R $(whoami):$(whoami) /var/www/lab2.axfon.com/public_html

Grant Directory Permission

sudo chmod -R 755 /var/www

Step 4: Configuration Server Blocks

Nginx Server Blocks allows to run or host one or multi-domain on a single server. To configure Server Block you have to specify document root for each domain and you can create sperately configuration for Security, SSL, Cache and more..

Nginx contains a default server block in /etc/nginx/sites-available/default Copy these default server block with the new name as domain name lab.axfon.com and lab2.axfon.com.

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/lab.axfon.com
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/lab2.axfon.com

After copying these file then edit these files using nano editor:

sudo nano /etc/nginx/sites-available/lab.axfon.com

You will be presented as following, on the code with the red line root is directory location that already created before and server_name is a server IP or domain name (change it with your own)

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # include snippets/snakeoil.conf;
        root /var/www/lab.axfon.com/public_html;
        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name lab.axfon.com www.lab.axfon;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }
}

Cmd/Ctr X and Y to exit and save

For the second server block, we have to remove default_server Only one server block can have the default_server on NGINX

listen 80 default_server;
listen [::]:80 default_server;

Edit the second server blocks

sudo nano /etc/nginx/sites-available/lab2.axfon.com

Let’s see as the following, on the code with the red line root is second directory location that already created before and server_name is a server IP or second domain name (change it with your own) and default_server was removed on listen port 80

server {
        listen 80;
        listen [::]:80;

        # include snippets/snakeoil.conf;
        root /var/www/lab2.axfon.com/public_html;
        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name lab2.axfon.com www.lab2.axfon;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }
}

Cmd/Ctr X and Y to exit and save


Step 5: Create Symbolic Link

Creating symbolic links from the sites-available directory to the sites-enabled directory, which NGINX reads at startup for domain lab.axfon.com and lab2.axfon.com (change it with your own).

First, we have to remove the symbolic link of default server block.

sudo rm /etc/nginx/sites-enabled/default

Then create new our domain symbolic link

sudo ln -s /etc/nginx/sites-available/lab.axfon.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/lab2.axfon.com /etc/nginx/sites-enabled/

Step 6: Create Testing File

Now we will create a file into each domain root directory

sudo echo "Thanks Axfon One" > /var/www/lab.axfon.com/public_html/index.html
sudo echo "Thanks Axfon Two" > /var/www/lab2.axfon.com/public_html/index.html

Step 7: Restart and Error Checking

Changes made in the NGINX configuration file will not be applied until the command to Restart and Before testing on the Browser we have to verify and make sure no error.

sudo nginx -t

If there is no error you will see as the follow

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Then Restart NGINX

sudo service nginx restart

Step 8: Testing Webpage on the Browser

You can now view this page in your web browser by visiting your server’s domain name or public IP address …Congrats..!!


If this tutorial could help you, please rate above Star button rating and share to help others find it! Feel free to leave a comment below.

Recommended For You

Axfon

About the Author: Axfon

Team committed to share our IT skills and experience through our website which may assist to formulate a task easy