
How to Install NGINX on Ubuntu 22.04: A Complete Step-by-Step Guide
Setting up a high-performance NGINX web server on Ubuntu 22.04 is a fundamental skill for any developer, system administrator, or web enthusiast. NGINX is renowned for its stability, rich feature set, and low resource consumption, making it an excellent choice for hosting everything from a personal blog to a high-traffic enterprise application.
This guide provides a clear, step-by-step walkthrough for installing and configuring NGINX on a fresh Ubuntu 22.04 server. We’ll cover the installation, firewall configuration, and essential service management commands to get you up and running securely.
Prerequisites
Before we begin, ensure you have access to an Ubuntu 22.04 server and a non-root user account with sudo
privileges.
Step 1: Update Your Server’s Package Index
First, it’s always best practice to update your server’s local package index to ensure you have access to the latest versions of software and security patches. This helps prevent potential conflicts and vulnerabilities.
Open your terminal and run the following command:
sudo apt update
You may also wish to upgrade any outdated packages on your system, though this is not strictly required for the NGINX installation itself.
sudo apt upgrade -y
Step 2: Install the NGINX Web Server
With your package list updated, installing NGINX is straightforward. Ubuntu’s default repositories contain the NGINX package, so we can install it directly using the apt
package manager.
Execute this command to install NGINX:
sudo apt install nginx -y
Once the installation completes, the NGINX service will automatically start and be enabled to launch on system boot.
Step 3: Configure the Firewall for NGINX Traffic
A properly configured firewall is crucial for server security. Ubuntu servers often use the Uncomplicated Firewall (UFW). Assuming you have UFW enabled, you need to allow traffic to NGINX.
NGINX registers a few application profiles with UFW upon installation, making this process simple. You can view the available profiles by running:
sudo ufw app list
You will see an output similar to this:
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH
These profiles correspond to:
- Nginx HTTP: Opens port 80 (standard, unencrypted web traffic).
- Nginx HTTPS: Opens port 443 (TLS/SSL encrypted traffic).
- Nginx Full: Opens both port 80 and 443.
For now, we will only allow standard HTTP traffic on port 80. If you plan to configure an SSL certificate later (which is highly recommended), you will need to use ‘Nginx Full’ or ‘Nginx HTTPS’.
Allow NGINX HTTP traffic through your firewall:
sudo ufw allow 'Nginx HTTP'
You can verify the change by checking the firewall’s status:
sudo ufw status
The output should now show that Nginx HTTP
is allowed.
Step 4: Verify Your NGINX Installation
Now it’s time to confirm that your web server is running correctly. First, check the status of the NGINX service using the systemd
init system.
systemctl status nginx
If everything is working, you will see an active (running)
status in green:
● 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 Tue 2023-10-24 14:20:01 UTC; 5min ago
The ultimate test is to access the default NGINX landing page from your web browser. To do this, you’ll need your server’s public IP address. If you don’t know it, you can find it with this command:
curl -4 icanhazip.com
Open a web browser and navigate to http://YOUR_SERVER_IP
. You should be greeted by the “Welcome to nginx!” page. Seeing this page confirms that your NGINX web server is installed and accessible.
Step 5: Essential NGINX Process Management
You now have a running NGINX instance. It’s important to know how to manage the NGINX service for maintenance and configuration updates. Here are the key commands you will use:
To stop the web server:
sudo systemctl stop nginx
To start the web server when it is stopped:
sudo systemctl start nginx
To stop and then start the service again (a hard restart):
sudo systemctl restart nginx
To reload the configuration without dropping connections (the preferred method for applying changes):
sudo systemctl reload nginx
This is a crucial command. When you edit NGINX configuration files, a
reload
gracefully applies the changes without interrupting active user connections.To disable NGINX from starting automatically at boot:
sudo systemctl disable nginx
To re-enable NGINX to start at boot:
bash
sudo systemctl enable nginx
Key NGINX File and Directory Locations
As you move forward, knowing where to find important NGINX files is essential for configuration and troubleshooting:
- /etc/nginx: The main NGINX configuration directory. All NGINX configuration files reside here.
- /etc/nginx/nginx.conf: The primary NGINX configuration file. Global settings are defined here.
- /etc/nginx/sites-available: This directory contains the configuration files for your individual websites (known as “server blocks”).
- /etc/nginx/sites-enabled: This directory contains symbolic links to the configuration files in
sites-available
that you want to be active. - /var/log/nginx: NGINX log files are stored here. Check
access.log
anderror.log
for troubleshooting connection and server issues.
Conclusion
Congratulations! You have successfully installed and configured the NGINX web server on your Ubuntu 22.04 system. You’ve also secured it by adjusting the firewall and learned the essential commands to manage the NGINX service.
Your next steps will likely involve creating server blocks to host one or more domains and securing your sites with a Let’s Encrypt SSL certificate to enable HTTPS. With this solid foundation, you are well on your way to deploying fast and reliable websites and applications.
Source: https://www.redswitches.com/blog/install-nginx-on-ubuntu/