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.
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.
Step 2: Create a Compute Node (CPU Virtual Machine)
After accessing your account, you should see a dashboard (see image), now:
- Navigate to the menu on the left side.
- Click on the Compute Nodes option.
- Click on Start to start creating your very first compute node.
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
- 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%).
- Next, select a geographical region from the Region dropdown where you want to launch your VM (e.g., United States).
- Most importantly, select the correct specifications for your VM according to your workload requirements by sliding the bars for each option.
Step 4: Choose VM Configuration and Image
- 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.
- 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.
Step 5: Choose the Billing cycle and Authentication Method
- 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.
- 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.
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.
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.
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!
Once your node shows this status, follow the below steps to connect to the running VM via SSH:
- 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
2. In some cases, your terminal may take your consent before connecting. Enter ‘yes’.
3. A prompt will request a password. Type the ssh password, and you should be connected.
Output:
Step 8: Install Nginx
Let’s start with preparing the system before installation.
- Update the Ubuntu package source-list.
sudo apt update
Output:
2. Upgrade system packages
sudo apt upgrade -y
Output:
3. Install NGINX
sudo apt install nginx -y
Output:
4. Confirm Installation
nginx -v
Output:
5. Check the status of the NGINX service
sudo systemctl status nginx
Now, in some cases, as you hit the above command, you may encounter an error like this:
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:
- 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
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>
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
b) Remove/comment the line “listen [::]:80 default_server;"
It should look like this in the file:
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.
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
Output:
b) Enable ufw
sudo ufw enable
Output:
c) Allow NGINX traffic
sudo ufw allow 'Nginx Full'
Output:
Step 10: Start NGINX
- Restart NGINX
sudo systemctl restart nginx
2. Check service status
sudo systemctl status nginx
Output:
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:
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.