Contents

How to Setup Nginx on Ubuntu 18.04

You are here:
← 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 -y
$ sudo nginx -v
  nginx version: nginx/1.16.1

#Start & Enable Nginx Service
$ sudo systemctl status nginx
$ sudo systemctl start nginx

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

Enable HTTPS & HTTP/2

Generate a Let’s Encrypt wildcard SSL Certificate by following this link

Transfer all *.pem file to the newly created directory called /etc/nginx/ssl using SCP

# Transfer *.pem file to Nginx File with SCP
$ scp *.pem [email protected]:/tmp
# Create a new directory - /etc/nginx/ssl
$ sudo mkdir /etc/nginx/ssl
# Transfer pem file from /tmp/ to /etc/nginx/ssl
$ sudo cp /tmp/*.pem /etc/nginx/ssl/

Enable HTTP & HTTP/2 support in Nginx Default Configuration file – /etc/nginx/conf.d/default.conf

# Make a copy of the default config file
$ sudo cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
# Enable https & http/2 in Server Block 
$ sudo nano /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

    #SSL
    listen 443 ssl http2;
    ssl_certificate /etc/nginx/ssl/cert.pem;
    ssl_certificate_key /etc/nginx/ssl/privkey.pem;
}

# Restart Nginx Service
$ sudo systemctl restart nginx

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

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

Replace the default index.html

Modify the default index.html file to include support for Bootstrap 5

$ sudo nano /usr/share/nginx/html/index.html
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/css/bootstrap.min.css" integrity="sha384-r4NyP46KrjDleawBgD5tp8Y7UzmLA05oM1iAEQ17CSuDqnUK2+k9luXQOfXJCJ4I" crossorigin="anonymous">

    <title>Hello, Bootstrap 5</title>
  </head>
  <body>
    <h1>Hello, Bootstrap 5!!!</h1>

    <!-- Optional JavaScript -->
    <!-- Popper.js first, then Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/js/bootstrap.min.js" integrity="sha384-oesi62hOLfzrys4LxRF63OJCXdXDipiYWBnvTl9Y9/TRlw5xlKIEHpNyvvDShgf/" crossorigin="anonymous"></script>
  </body>
</html>

Leave a Reply

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

Scroll to Top