How to Setup Nginx on Ubuntu 18.04

You are here:
  • Main
  • Linux
  • How to Setup Nginx on Ubuntu 18.04
← All Topics

Steps to Setup Nginx on Ubuntu 18.04 Server

Nginx 1.14 is installed from ubuntu repository

$ sudo apt install nginx

#Nginx 1.14 installed
sudo nginx -v
 nginx version: nginx/1.14.0 (Ubuntu)

Upgrade to Nginx 1.16

$ sudo wget
$ sudo apt-key add nginx_signing.key
$ sudo apt update 

$ sudo nano /etc/apt/sources.list

  #Nginx 1.6 Repo
  deb bionic nginx
  deb-src bionic nginx

$ sudo apt list --upgradable
Listing... Done
nginx/stable 1.16.1-1~bionic all [upgradable from: 1.14.0-0ubuntu1.7]
N: There are six additional versions. Please use the '-a' switch to see them.

$ sudo apt install nginx
$ sudo nginx -v
  nginx version: nginx/1.16.1

#Restart Nginx
$ sudo systemctl status nginx
$ sudo systemctl start nginx

You should be able to access Nginx default web page via http://IP_ADDRESS now

Nginx configuration files

# Nginx Config File
$ sudo cat /etc/nginx/conf.d/default.conf
$ sudo cat /etc/nginx/nginx.conf

Multiple Web Sites

The following three sites will be created


Create a new directory for each website with a default index.html file and change the ownership to www-data

$ sudo mkdir /var/www/html/
$ sudo mkdir /var/www/html/
$ sudo mkdir /var/www/html/

$ sudo nano /var/www/html/
$ sudo nano /var/www/html/
$ sudo nano /var/www/html/

$ sudo chown -R www-data:www-data /var/www/html/
$ sudo chown -R www-data:www-data /var/www/html/
$ sudo chown -R www-data:www-data /var/www/html/

Create a Nginx config file for each site.

Modify the root & server_name to point to respective directory

$ sudo nano /etc/nginx/sites-available/

server {
        listen 80;
        root /var/www/html/;
        index index.html index.htm;

   location / {
       try_files $uri $uri/ =404;

Create a symbolic link for Nginx config file to /etc/nginx/conf.d/

$ sudo ln -s /etc/nginx/sites-available/  /etc/nginx/conf.d/
$ sudo ln -s /etc/nginx/sites-available/  /etc/nginx/conf.d/
$ sudo ln -s /etc/nginx/sites-available/  /etc/nginx/conf.d/

Rename the /etc/nginx/conf.d/default.conf to remove the Nginx default page

$ sudo mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak

Restart Nginx Service

$ sudo systemctl restart nginx

3 x Web Sites are up and running now

Setup Nginx on Ubuntu


Index.html file with Bootstrap Template

<!doctype html>
<html lang="en">
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <title>Hello, world!</title>
    <h1>Hello, world!</h1>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

Top Posts & Pages

Leave a Reply

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

Scroll to Top