How to Install Nginx Web Server on Ubuntu 18.04

Introduction

Nginx is a powerful web server that is used to host websites and web applications. It is a popular choice for web hosting due to its high performance, scalability, and reliability. In this tutorial, we will show you how to install Nginx web server on Ubuntu 18.04. We will also cover how to configure Nginx to serve static content, how to enable HTTPS support, and how to configure virtual hosts. By the end of this tutorial, you will have a fully functional Nginx web server running on your Ubuntu 18.04 server.

How to Install Nginx Web Server on Ubuntu 18.04

1. Update the apt package index:

sudo apt update

2. Install Nginx:

sudo apt install nginx

3. Check the status of Nginx:

sudo systemctl status nginx

4. Start Nginx:

sudo systemctl start nginx

5. Enable Nginx to start on boot:

sudo systemctl enable nginx

6. Check the Nginx version:

nginx -v

Nginx – pronounced “Engine X” – is an open-source server utility. It was designed to work as a reverse proxy, intercepting client requests and routing them to an appropriate server.

Since then, it has grown to include load balancing, anonymizing, and scaling features. It also has features that can handle static resource requests more quickly than Apache, allowing you to balance requests to improve performance and resource usage strategically.

This guide will walk you thru you setting up and installing Nginx on Ubuntu 18.04 (Bionic Beaver).

how to install nginx web server on ubuntu

Prerequisites

Steps to Installing Nginx on Ubuntu

Update Software Repositories

Log into your Server via SSH as the root user

ssh root@hostname

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.

Enter the following:

sudo apt update

Allow the process to finish.

Install Nginx on Ubuntu

Enter the following to install Nginx on Ubuntu:

sudo apt install nginx

This may take some time for the system to download the software packages and install them. Allow it to complete before moving on.

Verify Nginx Service is Running

Use the following command to check the status of the Nginx service:

sudo systemctl status nginx

The system should return a list of information about the Nginx service. The active line indicates whether the service is running or not. If you need to start the service, use the following:

sudo systemctl start nginx

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

  • sudo systemctl stop nginx– stops the Nginx service
  • sudo systemctl enable nginx– enables Nginx to load at startup
  • sudo systemctl disable nginx– prevents Nginx from loading at startup

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
  • Nginx HTTP– Opens Port 80 for normal web traffic
  • Nginx HTTPS– 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.

Note: You can also create a rule to allow all traffic on Port 80. But, this can open your system to vulnerabilities. A better practice is only to create a rule to allow only the traffic you need.

Test Nginx in a Web Browser

Open a web browser, such as Firefox.

Enter your system’s IP address in the address bar or type localhost.

Your browser should display a page welcoming you to Nginx.

Note: You can check your system’s IP address from the terminal window with the command: ip a

Define Server Blocks

Nginx uses a configuration file to determine how it behaves. One way to use the configuration file is to define server blocks, which work similar to an Apache Virtual Host.

Nginx is designed to act as a front for multiple servers, which is done by creating server blocks.

By default, the main Nginx configuration file is located at /etc/nginx/nginx.conf. Server block configuration files are located at /etc/nginx/sites-available.

To view the contents of the default server block configuration file, enter the following command in a terminal:

sudo vi /etc/nginx/sites-available/default

This should open the default configuration file in the Vi text editor, which should look something like this:

# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
[...]
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
[...]
}
  • The listen commands tell Nginx which ports to look at for traffic
  • Default_server defines this as the block to be delivered unless otherwise specified by the client
  • Root determines which directory holds the root directory for the website that’s being served
  • Server_name allows you to specify a name for a particular server block, which is used in more advanced configurations
  • Location allows you to direct the location where Nginx should route traffic

Create a Sample Server Block

Set up an HTML File

Going through a sample configuration is helpful. In a terminal window, enter the following command to create a “test” directory to work with:

sudo mkdir /var/www/example

Create and open a basic HTML index file to work as a test webpage:

sudo vi /var/www/example/index.html

In the Vi text editor (you can substitute your preferred text editor if you’d like), enter the following:

Welcome to the Example Website!

Save the file and exit.

Set up a Simple Server Block

Use the following command to create a new server block file for our Test website:

sudo vi /etc/nginx/sites-available/example.com

This should launch the Vi text editor and create a new server block file.
Enter the following lines into the text file:

server {
listen 80;
root /var/www/example;
index index.html;
server_name www.example.com;
}

This tells Nginx to look at the /var/www/example directory for the files to serve, and to use the index.html file we created as the front page for the website.
Save the file and exit.

Create a Symbolic Link to Activate Server Block

In the terminal window, enter the following command:

sudo ln –s /s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled

This creates a link and enables your test website in Nginx. Restart the Nginx service to apply the changes:

sudo systemctl restart nginx

Start Testing

In a browser window, visit www.example.com.

Nginx should intercept the request, and display the text we entered in the HTML file.

Conclusion

In this guide, you have learned how to configure, setup, and install Nginx on Ubuntu 18.04.

Nginx is a powerful tool for managing servers and web traffic. It is configured for tasks such as load balancing or operating as a reverse proxy.

How to Install Nginx Web Server on Ubuntu 18.04

Nginx is a popular open-source web server and reverse proxy, known for its high performance, stability, rich feature set, simple configuration, and low resource consumption. In this tutorial, we will show you how to install Nginx on an Ubuntu 18.04 server.

Prerequisites

Before you begin with this guide, you should have a non-root user with sudo privileges configured on your server. You can learn how to set up a user with these privileges by following our initial server setup guide for Ubuntu 18.04.

Step 1 — Installing Nginx

The Nginx packages are included in the default Ubuntu 18.04 repositories. We can install them by typing:

sudo apt update
sudo apt install nginx

Once the installation is complete, the Nginx service will start automatically. You can check with the following command:

sudo systemctl status nginx

The output should look something like this:

● 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 Thu 2018-04-19 11:45:50 UTC; 1min 11s ago
     Docs: man:nginx(8)
 Main PID: 1589 (nginx)
    Tasks: 2 (limit: 1153)
   CGroup: /system.slice/nginx.service
           ├─1589 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─1590 nginx: worker process

This confirms that Nginx is running. You can also check it by visiting your server’s public IP address in your web browser. You should see the default Nginx welcome page, which is located in the /var/www/html directory.

Step 2 — Adjusting the Firewall

If you have the ufw firewall enabled, as recommended in the prerequisite guides, you need to adjust the firewall to allow access to Nginx. You can allow this by typing:

sudo ufw allow 'Nginx HTTP'

You can verify the change by typing:

sudo ufw status

The output should look like this:

Status: active

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

Step 3 — Managing the Nginx Process

Now that Nginx is installed, we can manage the process using systemd. To stop the service, you can type:

sudo systemctl stop nginx

To start the service when it is stopped, type:

sudo systemctl start nginx

To stop and then start the service again, type:

sudo systemctl restart nginx

To reload the configuration without dropping the connections, type:

sudo systemctl reload nginx

By default, Nginx is configured to start automatically when the server boots. If this is not what you want, you can disable this behavior by typing:

sudo systemctl disable nginx

To re-enable the service to start up at boot, you can type:

sudo systemctl enable nginx

Conclusion

In this guide, we have shown you how to install Nginx on an Ubuntu 18.04 server. We have also shown you how to manage the Nginx process and adjust the firewall to allow access to the web server.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?