• Home
  • Knowledgebase
  • Blog
  • Home
  • Knowledgebase
  • Blog
home/Knowledge Base/Ubuntu/How To Install Nginx on Ubuntu 20.04

How To Install Nginx on Ubuntu 20.04

38 views 0 November 24, 2022 Updated on February 1, 2023 host_know_user

Introduction

One of the most well-liked web servers in the entire globe, Nginx is in charge of hosting some of the busiest and biggest websites on the internet. It is a compact option that can function as a reverse proxy or web server.

We’ll go over installing Nginx on your Ubuntu 20.04 server, configuring the firewall, managing the Nginx process, and configuring server blocks for hosting multiple domains from a single server in this guide.

 

Prerequisites

You should have a regular, non-root user with sudo capabilities set up on your server before starting this guide.

When a user account becomes available, log in as that user to get started.

 

Step 1:Installing Nginx

The apt packaging system can be used to install Nginx from Ubuntu’s default repositories because it is present there.

In order to get access to the most recent package listings as this is our first interaction with the apt packaging system in this session, we will update our local package index. Following that, we can install nginx:

# sudo apt update
# sudo apt install nginx

Apt will install Nginx and any other dependencies to your server when you approve the operation.

Step 2 – Adjusting the Firewall

The firewall programme needs to be modified to permit access to the service prior to testing Nginx. Upon installation, Nginx registers with ufw as a service, making it simple to provide Nginx access.

Type the following to see a list of the application configurations that ufw can handle:

# sudo ufw app list

A list of the application profiles should be obtained:

Output
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH

There are three Nginx profiles available, as shown by the output:

  • Full Nginx: Both port 80 (for regular, unencrypted web traffic) and port 443 (for TLS/SSL-encrypted traffic) are opened by this profile.
  • Nginx HTTP: Only port 80 is open in this profile
  • Nginx HTTPS: This profile only allows traffic on port 443 that is SSL/TLS encrypted.

It is advised that you activate the most stringent profile that still permits the configured traffic. We simply need to let port 80 traffic for the time being.

You can activate this by keying in:

# sudo ufw allow 'Nginx HTTP'

By typing: you can confirm the change.

# sudo ufw status

Which HTTP traffic is permitted will be mentioned in the output:

Output
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:Checking your Web Server

After the installation is complete, Ubuntu 20.04 launches Nginx. The web server ought to be operational by now.

By typing: we can verify that the service is active with the systemd init system.

# systemctl status nginx

Output
● 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 Fri 2020-04-20 16:08:19 UTC; 3 days ago
Docs: man:nginx(8)
Main PID: 2369 (nginx)
Tasks: 2 (limit: 1153)
Memory: 3.5M
CGroup: /system.slice/nginx.service
├─2369 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─2380 nginx: worker process

This indicates that the service has launched successfully. However, really requesting a page from Nginx is the most effective approach to test this.

By going to your server’s IP address, you may visit the default Nginx landing page to verify that the programme is functioning as intended. The icanhazip.com tool can be used to determine your server’s IP address if you are unsure about it. It will provide you with your public IP address as it was acquired from another internet location:

# curl -4 icanhazip.com

When you know your server’s IP address, type it into the address bar of your browser as follows:

http://your_server_ip

You ought to see the standard Nginx landing page:

Your server is functioning properly and is prepared for management if you are viewing this page.

Step 4: Taking control of the Nginx process

Now that your web server is operational, let’s go over some fundamental management commands.

Type: to terminate your web server.

# sudo systemctl stop nginx

When the web server is stopped, type: to restart it.

# sudo systemctl start nginx

Type: to halt the service, then restart it.

# sudo systemctl restart nginx

Nginx can frequently reload without disconnecting connections if you are only making configuration changes. To do this, type:

# sudo systemctl reload nginx

Nginx is set up by default to launch immediately when the server boots. You can stop this behaviour if it’s not what you want by typing:

# sudo systemctl disable nginx

You can type: to enable the service to start up again at boot.

# sudo systemctl enable nginx

You should be prepared to set up the website to host several domains now that you are familiar with the fundamental management commands.

Step 5 – Setting Up Server Blocks (Recommended)

When utilising the Nginx web server, server blocks can be used to encapsulate configuration information and host several domains from a single server (akin to virtual hosts in Apache) Test_domain will be the domain name we create, but you should swap it out for your own.

On Ubuntu 20.04, Nginx has one server block enabled by default that is set up to serve files from the /var/www/html directory. While this is effective for a single site, hosting many sites might make it cumbersome. Instead of making changes to /var/www/html, let’s build a directory structure within /var/www for our test_domain site and leave /var/www/html in place to act as the default directory if a client request doesn’t match any other sites.

Use the -p parameter to establish any required parent directories when creating the directory for test_domain:

# sudo mkdir -p /var/www/ test_domain/html

Next, use the $USER environment variable to specify who owns the directory:

# sudo chown -R $USER:$USER /var/www/ test_domain/html

If you haven’t changed the umask value, which determines the default file permissions, the permissions of your web roots should be accurate. You can enter the following command to verify that your permissions are valid and provide the owner read, write, and execute access to the files while only providing groups and other users read and execute access:

# sudo chmod -R 755 /var/www/test_domain

Next, using nano or your preferred editor, make a sample index.html page as follows:

# sudo nano /var/www/test_domain/html/index.html

Add the following example HTML inside:

/var/www/test_domain/html/index.html

<html>
<head>
<title>Welcome to test_domain!</title>
</head>
<body>
<h1>Success! The test_domain server block is working!</h1>
</body>
</html>

By clicking Ctrl+X to quit, Y to accept the offer to save, and then Enter, you can save and close the file.

Create a server block with the appropriate directives so that Nginx may deliver this content. Let’s create a new configuration file at /etc/nginx/sites-available/test_domain rather than just altering the default one:

# sudo nano /etc/nginx/sites-available/test_domain

The configuration block below should be pasted in; it is similar to the default but updated for our new directory and domain name:

/etc/nginx/sites-available/test_domain

server {
listen 80;
listen [::]:80;

root /var/www/test_domain/html;
index index.html index.htm index.nginx-debian.html;

server_name test_domain www.test_domain;

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

You’ll see that we changed the server name to our domain name and the root configuration to point to our new directory.

Now that the file has been enabled, let’s link to the sites-enabled directory, which Nginx reads from when it starts up:

# sudo ln -s /etc/nginx/sites-available/test_domain /etc/nginx/sites-enabled/

Notes: Nginx keeps track of which of your server blocks are active using a popular technique known as symbolic links, or symlinks. Making a symlink is similar to making a shortcut on your computer’s hard drive, allowing you to keep the server block in the sites-available directory and erase the shortcut from the sites-enabled directory at a later time if you decide to enable it.

Now that two server blocks have been enabled, they have been set up to react to requests in accordance with the listen and server_name directives.

  • Responses to requests for both test_domain and www.test_domain are provided by your domain.
  • default, all requests on port 80 that do not match the other two blocks will be reacted to.

It is necessary to change a single value in the /etc/nginx/nginx.conf file in order to prevent a potential hash bucket memory issue that may result from adding extra server names. Open the document:

# sudo nano /etc/nginx/nginx.conf

Locate the servers_names_hash_bucket_size directive and remove the # to uncomment the line. By hitting CTRL and w when using nano, you may fast search for words within a file.

Note: Code can also be disabled without really being deleted by being commented out, which is done by beginning a line with the symbol #. Multiple options are often commented out in configuration files so that they can be activated or disabled by switching between the active code and the documentation.

/etc/nginx/nginx.conf

…
http {
…
server_names_hash_bucket_size 64;
…
}
…

When you’re done, save and shut the file.

Next, check all of your Nginx files to ensure there are no syntax errors:

# sudo nginx –t

Restart Nginx to apply your changes if there are no issues:

# sudo systemctl restart nginx

Your domain name should now be served by Nginx. You may verify this by going to http://test_domain, where you ought to see the following:

Step 6 – Getting Familiar with Important Nginx Files and Directories

After learning how to control the Nginx service, you should spend some time getting acquainted with a few crucial directories and files.

Content
  • /var/www/html: The actual web content is served from the /var/www/html directory, which by default only contains the default Nginx page you previously saw. By making changes to the Nginx configuration files, this can be adjusted.
Server Configuration
  • /etc/nginx:the directory for Nginx settings. Here are all of the Nginx configuration files.
  • /etc/nginx/nginx.conf: the main configuration file for Nginx. To alter the overall settings of Nginx, modify this.
  • /etc/nginx/sites-available/: the directory that can house server blocks specific to each site. Unless they are linked to the sites-enabled directory, Nginx won’t use the configuration files in this directory. Normally, all server block configuration is completed in this directory, and it is linked to the other directory to enable it.
  • /etc/nginx/sites-enabled/: the directory that houses per-site server blocks that have been enabled. Links to configuration files located in the sites-available directory are typically used to build these.
  • /etc/nginx/snippets: The configuration pieces found in this directory can be used elsewhere in the Nginx setup. Potentially repeated configuration portions make excellent candidates for snippet refactoring.
Server Logs
  • /var/log/nginx/access.log: Unless Nginx is configured otherwise, every request to your web server is logged in this log file.
  • /var/log/nginx/error.log: This log will contain any Nginx errors.
Conclusion
  • After setting up your web server, you have a variety of options for the material you want to provide and the technologies you want to employ to give users a richer experience.

Was this helpful?

Yes  No
Related Articles
  • How To Set Up vsftpd for a User’s Directory on Ubuntu 20.04
  • How To Install the Apache Web Server on Ubuntu 18.04
  • How To Reset Your MySQL or MariaDB Root Password ubuntu 16.4
  • How To Install Node.js on Ubuntu 18.04
  • How to Config Initial Server Setup With Ubuntu 18.04
  • How To Install MySQL on Ubuntu 18.04

Didn't find your answer? Contact Us

Leave A Comment Cancel reply

Ubuntu
  • How To Install Nginx on Ubuntu 20.04
  • How To Install MySQL on Ubuntu 18.04
  • How to Config Initial Server Setup With Ubuntu 18.04
  • How To Install Node.js on Ubuntu 18.04
  • How To Reset Your MySQL or MariaDB Root Password ubuntu 16.4
  • How To Install the Apache Web Server on Ubuntu 18.04
  • How To Set Up vsftpd for a User’s Directory on Ubuntu 20.04
All Categories
  • Centos
  • container
  • Ubuntu
  • Debian
  • Linux Basics

  How To Install the Apache Web Server on Ubuntu 18.04

How To Set Up vsftpd for a User’s Directory on Ubuntu 20.04  

Support
  • Live chat
  • Knowledge Base
  • Blog
Manual Head Office
Toll free : 1800 572 8782
  • Copyright 2022 Hostzop Expert Solutions. All Rights Reserved.