DEV Community

Cover image for How to Install NGINX on Ubuntu 22.04
Aditi Bindal for NodeShift

Posted on

How to Install NGINX on Ubuntu 22.04

NGINX is a high-performance, open-source web server and reverse proxy server widely recognized for its speed, scalability, and low resource consumption. It was originally designed as an HTTP server to address the performance limitations of other web servers; however, now it has evolved into a powerful tool that can serve static content, act as a reverse proxy, load balancer, and even a web application firewall. Its event-driven architecture enables it to handle thousands of simultaneous connections with minimal resources, making it ideal for high-traffic websites and web applications that demand efficiency and reliability.

hero-image

In this article, we'll cover a step-by-step approach to installing and setting up your own NGINX web server. We'll also look into some common errors and issues that arise during the installation and how to troubleshoot them to get your NGINX server up and running.

Prerequisites

  • A Virtual Machine (such as the ones provided by NodeShift) with at least:

    • 1 vCPUs
    • 512 MB RAM
    • 1 GB SSD
  • Ubuntu 22.04 VM

Note: The prerequisites for this are highly variable across use cases. One could use a high-end configuration for a large-scale deployment.

Step-by-step process to install NGINX on Ubuntu 22.04

For this tutorial, we'll use a CPU-powered Virtual Machine by NodeShift, which provides high-compute Virtual Machines at a very affordable cost on a scale that meets GDPR, SOC2, and ISO27001 requirements. It also offers an intuitive and user-friendly interface, making it easier for beginners to get started with Cloud deployments. However, feel free to use any cloud provider you choose and follow the same steps for the rest of the tutorial.

Step 1: Setting up a NodeShift Account

Visit app.nodeshift.com and create an account by filling in basic details, or continue signing up with your Google/GitHub account.

If you already have an account, login straight to your dashboard.

Image-step1-1

Step 2: Create a Compute Node (CPU Virtual Machine)

After accessing your account, you should see a dashboard (see image), now:

1) Navigate to the menu on the left side.

2) Click on the Compute Nodes option.

Image-step2-1

3) Click on Start to start creating your very first compute node.

Image-step2-2

These Compute nodes are CPU-powered virtual machines by NodeShift. These nodes are highly customizable and let you control different environmental configurations, such as vCPUs, RAM, and storage, according to your needs.

Step 3: Select configuration for VM

1) The first option you see is the Reliability dropdown. This option lets you choose the uptime guarantee level you seek for your VM (e.g., 99.9%).

Image-step3-1

2) Next, select a geographical region from the Region dropdown where you want to launch your VM (e.g., United States).

Image-step3-2

3) Most importantly, select the correct specifications for your VM according to your workload requirements by sliding the bars for each option.

Image-step3-3

Step 4: Choose VM Configuration and Image

1) After selecting your required configuration options, you'll see the available VMs in your region and as per (or very close to) your configuration. In our case, we'll choose a '1 vCPUs/1GB/25GB SSD' Compute node.

2) Next, you'll need to choose an image for your Virtual Machine. For the scope of this tutorial, we'll select Ubuntu, as we will deploy NGINX on Ubuntu.

Image-step4-1

Step 5: Choose the Billing cycle and Authentication Method

1) Two billing cycle options are available: Hourly, ideal for short-term usage, offering pay-as-you-go flexibility, and Monthly for long-term projects with a consistent usage rate and potentially lower cost.

Image-step5-1

2) Next, you'll need to select an authentication method. Two methods are available: Password and SSH Key. We recommend using SSH keys, as they are a more secure option. To create one, head over to our official documentation.

Image-step5-2

Step 6: Finalize Details and Create Deployment

Finally, you can also add a VPC (Virtual Private Cloud), which provides an isolated section to launch your cloud resources (Virtual machine, storage, etc.) in a secure, private environment. We're keeping this option as the default for now, but feel free to create a VPC according to your needs.

Also, you can deploy multiple nodes at once by clicking + in the Quantity option.

Image-step6-1

That's it! You are now ready to deploy the node. Finalize the configuration summary; if it looks good, go ahead and click Create to deploy the node.

Image-step6-2

Step 7: Connect to active Compute Node using SSH

As soon as you create the node, it will be deployed in a few seconds or a minute. Once deployed, you will see a status Running in green, meaning that our Compute node is ready to use!

Image-step7-1

Once your node shows this status, follow the below steps to connect to the running VM via SSH:

1) Open your terminal and run the below SSH command:

(replace root with your username and paste the IP of your VM in place of ip after copying it from the dashboard)

ssh root@ip
Enter fullscreen mode Exit fullscreen mode

2) In some cases, your terminal may take your consent before connecting. Enter ‘yes’.

  1. A prompt will request a password. Type the server's password, and you should be connected.

Output:

Image-step7-2

Step 8: Install Nginx

Let's start with preparing the system before installation.

1) Update the Ubuntu package source-list.

sudo apt update
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step8-1

2) Upgrade system packages

sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step8-2

3) Install NGINX

sudo apt install nginx -y
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step8-3

4) Confirm Installation

nginx -v
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step8-4

5) Check the status of the NGINX service

sudo systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

Now, in some cases, as you hit the above command, you may encounter an error like this:

Image-step8-5

This clearly shows an issue with the configuration since the service is not running and is showing as "failed".

These errors are quite common while setting up an NGINX server. In the next step, we'll go through some steps that you can take to fix these issues and get your server up and running.

Step 9: Troubleshoot NGINX Errors

Below, we have outlined some steps that work most of the time. Let's get into it one by one:

1) Check if another service is using PORT 80

NGINX is configured to use Port 80 by default; hence, it can throw an error if any conflicting services are currently running on Port 80.

Use the below command to check if another process (e.g., Apache) is using Port 80:

(Note: You may need to install net-tools to run this command if it's already not there)

sudo netstat -tuln | grep :80
Enter fullscreen mode Exit fullscreen mode

If the command executes successfully without any output, it means no other service is using Port 80.

However, if you see any service in the output, use the below-mentioned command to stop/disable it so it can free up the port for NGINX.

(replace <service_name> with the service that is mentioned in the output of the above command)

sudo systemctl stop <service_name>
sudo systemctl disable <service_name>
Enter fullscreen mode Exit fullscreen mode

2) Fix IPv6 Binding Issues

Another common reason could be that NGINX is configured to bind to IPv6 by default, and your system doesn't support IPv6 or is disabled at the Operating System level; hence, it is throwing an error.

To overcome this, we'll disable the IPv6 binding in the NGINX configuration.

a) Open the default NGINX configuration file using Nano:

sudo nano /etc/sites-available/default
Enter fullscreen mode Exit fullscreen mode

b) Remove/comment the line "listen [::]:80 default_server;"

It should look like this in the file:

Image-step9-1

Save and exit (Ctrl+O > Enter > Ctrl+X).

3) Check for any error logs

Additionally, you can use the below command to find any other issue that persists and causes the server to fail.

Image-step9-2

As you can see, the console is clean here, so it looks safe to proceed with further steps.

4) Enable Firewall ports

Now, before we move on to start the server, first allow traffic through the firewall.

a) Install ufw

sudo apt install ufw -y
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step9-3

b) Enable ufw

sudo ufw enable
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step9-4

c) Allow NGINX traffic

sudo ufw allow 'Nginx Full'
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step9-5

Step 10: Start NGINX

1) Restart NGINX

sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

2) Check service status

sudo systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

Output:

Image-step10-1

As you can see, the errors have gone and our server is successfully up and running.

3) Test the installation

Open your browser and visit your server’s IP address (e.g., http://your-server-ip). You should see the default NGINX welcome page.

Output:

Image-step10-2

If you see this page, it confirms that our NGINX server has been successfully installed and set up.

Conclusion

This guide walks you through installing NGINX on Ubuntu 22.04, involving a series of steps, from installing the NGINX package to troubleshooting common issues and configuring the server for optimal performance. By following this guide thoroughly, NGINX can be quickly set up as a powerful web server or reverse proxy. Deploying NGINX on NodeShift's cloud infrastructure further enhances scalability and flexibility, enabling seamless integration into modern cloud-based environments. This combination ensures that your application is well-optimized and ready to handle high traffic efficiently.

For more information about NodeShift:

Top comments (0)