Web Servers

How to Install Nginx on Ubuntu 20.04

In this article, we’ll discuss how to install NGINX on Ubuntu 20.04.

Ngnix (Pronounce as Engine X) is a free, open source web server which can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. Unlike traditional servers, NGINX doesn’t rely on threads to handle requests. Instead it uses a much more scalable event-driven (asynchronous) architecture. This architecture uses small, but more importantly, predictable amounts of memory under load.

How to Install NGINX on Ubuntu 20.04

Prerequisites

  • You need to install Ubuntu 20.04 on KVM VPS or dedicated server.
  • SSH to the server using root user or normal user with administrative privileges.

1. Keep the server up-to-date:

# apt update -y

# apt upgrade -y

2. Install NGINX using following command:

# apt install nginx -y

3. Configure firewall

If you are using ufw firewall, enable Nginx HTTP and HTTPS. Use following command to enable it:

# ufw allow ‘Nginx HTTP’

# ufw allow ‘Nginx HTTPS’

4. Create and configure server blocks

If you want to host multiple websites in the server, you need to configure server block for each website. On CentOS 8, server blocks are defined in .conf files located at /etc/nginx/conf.d.

The default server root directory is /usr/share/nginx/html this is works for single website but if you want to host multiple website, it can become unmanageable. Instead of that we’ll create a directory structure withing /var/www for website_name.

Create the directory for website_name as follows, using the -p flag to create any necessary parent directories:

# mkdir -p /var/www/website_name/html

Next, assign ownership of the directory with the $USER environment variable, which should reference your current system user:

# chown -R $USER:$USER /var/www/your_domain/html

Next, we’ll create a index.html file to test the server block configuration.

# vi /var/www/website_name/html/index.html

Inside that file, add the following HTML code:

<html>
<head>
<title>Welcome to Website name</title>
</head>
<body>
<h1>Success! Your Nginx server is successfully configured for <em>your_domain</em>. </h1>
<p>This is a sample page.</p>
</body>
</html>

Save and close.

Now, create a server block.

Instead of using default server block, we can create a new server block in /etc/nginx/sites-available/ as shown below:

# vi /etc/nginx/sites-available/website_name

Copy and paste following content:

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

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

server_name website_name www.website_name;

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

Next, we need to enable the newly created server block into the sites-enabled directory, which Nginx reads from during startup:

# ln -s /etc/nginx/sites-available/website_name /etc/nginx/sites-enabled/

To avoid a possible hash bucket memory problem that can arise from adding additional server names, it is necessary to adjust a single value in the /etc/nginx/nginx.conf file. Open the file:

# vi /etc/nginx/nginx.conf

Find the server_names_hash_bucket_size directive and remove the # symbol to uncomment the line.


http {

server_names_hash_bucket_size 64;

}

Save and close the file when you are finished.

Next, test to make sure that there are no syntax errors in any of your Nginx files:

# nginx -t

If there aren’t any problems, restart Nginx to enable your changes:

# systemctl restart nginx

Now open your browser and navigate to http://website_name.

We have successfully installed and configured Nginx.

In this article, we have seen how to install NGINX on Ubuntu 20.04.

Related Articles