Contents

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 https://nginx.org/keys/nginx_signing.key
$ sudo apt-key add nginx_signing.key
$ sudo apt update 

$ sudo nano /etc/apt/sources.list

  #Nginx 1.6 Repo
  deb https://nginx.org/packages/ubuntu/ bionic nginx
  deb-src https://nginx.org/packages/ubuntu/ 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

  • uat.aventislab.info
  • prod.aventislab.info
  • test.aventislab.info

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/uat.aventislab.info
$ sudo mkdir /var/www/html/prod.aventislab.info
$ sudo mkdir /var/www/html/test.aventislab.info

$ sudo nano /var/www/html/uat.aventislab.info/index.html
$ sudo nano /var/www/html/prod.aventislab.info/index.html
$ sudo nano /var/www/html/test.aventislab.info/index.html

$ sudo chown -R www-data:www-data /var/www/html/uat.aventislab.info/
$ sudo chown -R www-data:www-data /var/www/html/prod.aventislab.info/
$ sudo chown -R www-data:www-data /var/www/html/test.aventislab.info/

Create a Nginx config file for each site.

Modify the root & server_name to point to respective directory

$ sudo nano /etc/nginx/sites-available/uat.aventislab.info.conf

server {
        listen 80;
        root /var/www/html/uat.aventislab.info;
        index index.html index.htm;
        server_name uat.aventislab.info;

   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/test.aventislab.info.conf  /etc/nginx/conf.d/
$ sudo ln -s /etc/nginx/sites-available/uat.aventislab.info.conf  /etc/nginx/conf.d/
$ sudo ln -s /etc/nginx/sites-available/prod.aventislab.info.conf  /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

Appendix

Index.html file with Bootstrap Template

<!doctype html>
<html lang="en">
  <head>
    <!-- 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="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

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

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
  </body>
</html>

Top Posts & Pages

Leave a Reply

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

Scroll to Top