How to Setup Nginx Server On Ubuntu:18.04 /Dockerfile
By: Abdhesh Kumar
Tags: Dockerfile, Docker, Nginx

Nginx-Server

Introduction:

Nginx is one of the most popular web servers in the world and is responsible for hosting some of the largest and highest-traffic sites on the internet. It is more resource-friendly than Apache in most cases and can be used as a web server or reverse proxy.

In this guide, we’ll discuss how to install Nginx on your Ubuntu 18.04 server.

Prerequisites:

Ubuntu 18.04. Set user account with sudo privileges. Allow Access to the firewall

Step-1: Installing Nginx on ubuntu:

Before installing new software, it is strongly recommended to update your local software database. Updating helps to make sure you’re installing the latest and best-patched software available.

sudo apt-get update

sudo apt-get install nginx -y

Step-2: Managing Nginx Process:

$ sudo systemctl status nginx

Start nginx webservices

$ sudo systemctl start nginx

You can also use the following commands in place of start:

sudo systemctl stop nginx – stops the Nginx service

– stops the Nginx service sudo systemctl enable nginx – enables Nginx to load at startup

– enables Nginx to load at startup sudo systemctl disable nginx – prevents Nginx from loading at startup

Step3: Allow Nginx Traffic through a Firewall:

You can generate a list of the firewall rules using the following command:

sudo ufw app list

This should generate a list of application profiles. On the list, you should see four entries related to Nginx:

Nginx full – opens Port 80 for normal web traffic, and Port 443 for secure encrypted web traffic

– opens Port 80 for normal web traffic, and Port 443 for secure encrypted web traffic Nginx HTTP – Opens Port 80 for normal web traffic

– Opens Port 80 for normal web traffic Nginx HTTPS – Opens Port 443 for encrypted web traffic

– Opens Port 443 for encrypted web traffic OpenSSH – This is a configuration for SecureShell operations, which allow you to log into a remote server through a secure, encrypted connection

To allow normal HTTP traffic to your Nginx server, use the Nginx HTTP profile with the following command:

sudo ufw allow ‘Nginx HTTP’

To check the status of your firewall, use the following command:

sudo ufw status

It should display a list of the kind of HTTP web traffic allowed to different services. Nginx HTTP should be listed as ALLOW and Anywhere.

Step-4: Familiar with Important Nginx Files & Directories.

Now that you know how to manage the Nginx service itself, you should take a few minutes to familiarize yourself with a few important directories and files.

Content:

/var/www/html: The actual web content, which by default only consists of the default Nginx page you saw earlier, is served out of the /var/www/html directory. This can be changed by altering Nginx configuration files.

Server Configuration

/etc/nginx: The Nginx configuration directory. All of the Nginx configuration files reside here.

The Nginx configuration directory. All of the Nginx configuration files reside here. /etc/nginx/nginx.conf: The main Nginx configuration file. This can be modified to make changes to the Nginx global configuration.

The main Nginx configuration file. This can be modified to make changes to the Nginx global configuration. /etc/nginx/sites-enabled: The directory where per-site server blocks can be stored. Nginx will not use the configuration files found in this directory unless they are linked to the sites-enabled directory. Typically, all server block configuration is done in this directory, and then enabled by linking to the other directory.

The directory where per-site server blocks can be stored. Nginx will not use the configuration files found in this directory unless they are linked to the directory. Typically, all server block configuration is done in this directory, and then enabled by linking to the other directory. /etc/nginx/sites-enabled: The directory where enabled per-site server blocks are stored. Typically, these are created by linking to configuration files found in the sites-available directory.

The directory where enabled per-site server blocks are stored. Typically, these are created by linking to configuration files found in the directory. /etc/nginx/snippets: This directory contains configuration fragments that can be included elsewhere in the Nginx configuration. Potentially repeatable configuration segments are good candidates for refactoring into snippets.

Step-5: In my case I am going to create to directories:

two directories

Inside this directories I am going to create two sub directories.

Create a two page & two diff directories

I have already created above types of scenario.

Step-6: nginx.conf file:

I am going to edit nginx configuration file.

I have commented /etc/nginx/conf.d/conf file location in this configuration.

Step-7: I’m go to /etc/nginx/sites-available directories.

I am create to different different configuration files.

First: example file configuration

Second: website file configuration

Step-8: Enable site in Nginx Server:

To enable a specific virtual host file, we have to use ln command to create a symlink. A symlink is kind of a shortcut to the original file. It means that you can edit your virtual host file inside sites-available directory and the changes will be applied in sites-enabled directory too.

Let’s create a symlink of one of our example site from sites-available directory to sites-enabled directory. Execute the following command to perform this task.

$ sudo ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/example $ ln -s /etc/nginx/sites-available/website /etc/nginx/sites-enabled/website

Create Symbolic link

Verifying nginx configuration files.

Step-9: Restart Nginx Service.

Successfully restart nginx Service

Step-10: Go to Web Browser and Access the page.

ip address: 23.88.56.174

http://23.88.56.174/page1 is successfully worked.

===> How to Setup Nginx Server On Docker< ====

Step-1: Dockerfile:

Dockerfile is the basic concept for building Docker images. … Dockerfile is a text file that contains a list of commands (instructions), which describes how a Docker image is built based on them. The command docker build tells Docker to build the image by following the content (instructions) inside the Dockerfile.

I’m going to create Dockerfile in docker_WIP workspace.

FROM ubuntu:18.04

RUN apt-get update -y

RUN apt-get install nginx -y

RUN apt-get install curl -y

RUN mkdir -p /var/www/abdhesh/tech

RUN rm -rf /var/www/html/index.nginx-debian.html

COPY index.html abdhesh.jpg /var/www/abdhesh/tech/

COPY abdhesh /etc/nginx/sites-available/

RUN rm -f /etc/nginx/nginx.conf

COPY ./nginx.conf /etc/nginx/

RUN rm -f /etc/nginx/sites-enabled/*

RUN ln -s /etc/nginx/sites-available/abdhesh /etc/nginx/sites-enabled/

EXPOSE 80:80

CMD ["/usr/sbin/nginx", "-g", "daemon off;"]

Github link: https://github.com/Abdheshkumar691/nginx-with_docker.git

Step-2: build docker nginx image.

I’am going to run following command to build docker images name called web:nginx

building docker images

I have successfully build docker images called web:nginx

Successfully build docker images

Step-4: showing Image successfully build or not.

show images

Step-3: Launch Container called webserver from web:nginx image.

Successfully launch container called webserver

Step-4: Server local Server IP

Step-5: go to browser & access page http://172.17.0.1/tech

Linkdn Account: https://www.linkedin.com/in/abdhesh-kumar-656b73179/

“I hope I have explained everything, and if you have any doubts or suggestions, you can comment on this blog or contact me on my LinkedIn.”