How to Install Nginx on Ubuntu – Step-by-Step Guide

Nginx installation on Ubuntu is the first step toward building a high-performance web environment for your projects. Whether you are hosting a small blog or a high-traffic application, you need a reliable web server. In this guide, you will learn exactly how to set up Nginx from scratch.

Nginx (pronounced “engine-ex”) is famous for its speed and ability to handle many connections at once. It is a popular choice for server administration for beginners because it is powerful yet lightweight. Many of the world’s busiest websites use it.

First, we will prepare your system. Next, we will run the installation commands. Finally, we will configure the server to host your own website. By the end of this Ubuntu server configuration guide, you will have a fully functional web server running on your machine.

What is Nginx and Why Use It?

Before we dive into the Nginx on Ubuntu installation steps, it helps to know what this software actually does. Nginx is open-source software that acts as a web server, reverse proxy, and load balancer.

When someone types your website address into their browser, Nginx is the program that delivers your web page to them.

Here is why many users prefer setting up a web server with Nginx over alternatives like Apache:

  • Speed: It handles static content (like images and HTML) incredibly fast.
  • Efficiency: It uses very little memory, even under heavy loads.
  • Scalability: It manages thousands of concurrent connections easily.

If you are looking for a robust Linux web server setup tutorial, you have made the right choice with Nginx.

Prerequisites

To follow this beginner’s guide to Nginx installation, you need a few things ready.

  1. An Ubuntu Server: This guide works for Ubuntu 20.04 and shows how to install Nginx in Ubuntu 22.04.
  2. User Privileges: You need a non-root user with sudo privileges. This ensures you can install software safely.
  3. Command Line Access: You should be able to access the terminal (SSH) of your server.

Once you have these items, you are ready to begin web hosting on Ubuntu Linux.

Step 1: Update the System Packages

The first step in any server management task is ensuring your software list is current. This prevents conflicts and ensures security.

Open your terminal. run the following command to update your package index:codeBash

sudo apt updateCode language: Bash (bash)

Explanation of the command:

  • sudo: This runs the command with administrative (root) privileges.
  • apt: This is the package manager tool for Ubuntu.
  • update: This tells the system to check for the latest versions of packages listed in the repositories.

After this command finishes, your system knows about the latest software versions available. This is crucial before installing Nginx using apt-get commands.

Step 2: Install Nginx on Ubuntu

Now that our package list is fresh, we proceed with the Nginx installation on Ubuntu.

Run the following command to install the server:codeBash

sudo apt install nginxCode language: Bash (bash)

Explanation of the command:

  • install: This flag tells the package manager to download and set up the specified software.
  • nginx: This is the specific name of the package we want to install.

The terminal will calculate the disk space required. It will ask you to confirm the installation. Press Y and then Enter to continue.

The system will now download Nginx and all necessary dependencies. This process usually takes less than a minute. Once it completes, the Nginx service should start automatically.

Step 3: Adjust the Firewall Configuration

Security is vital for web hosting configuration. If you have the UFW (Uncomplicated Firewall) enabled, you must allow access to Nginx. If you don’t do this, no one can view your website.

First, check which applications UFW knows about by typing:codeBash

sudo ufw app listCode language: Bash (bash)

You should see an output that looks like this:

  • Nginx Full: Opens both port 80 (normal traffic) and port 443 (encrypted SSL traffic).
  • Nginx HTTP: Opens only port 80 (unencrypted).
  • Nginx HTTPS: Opens only port 443 (encrypted).

Since we have not set up an SSL certificate yet, we will only allow regular traffic for now.

Run this command:codeBash

sudo ufw allow 'Nginx HTTP'Code language: Bash (bash)

Explanation of the command:

  • ufw allow: Tells the firewall to let traffic pass through.
  • ‘Nginx HTTP’: Specifies the rule profile for standard web traffic on port 80.

To verify the change, check the status:codeBash

sudo ufw statusCode language: Bash (bash)

You should see that “Nginx HTTP” is listed as “ALLOW”.

Step 4: Check the Web Server Status

Now, we must verify that the Nginx installation on Ubuntu was successful. We need to confirm the service is running correctly.

Type this command into your terminal:codeBash

systemctl status nginxCode language: Bash (bash)

Explanation of the command:

  • systemctl: This is the utility used to control the systemd system and service manager.
  • status: This asks for the current state of the service.

Look for a line that says active (running) in green text. If you see this, your server is operational.

Next, let’s test it in a real web browser. You need your server’s public IP address. If you don’t know it, you can find it by typing:codeBash

curl -4 icanhazip.comCode language: Bash (bash)

Copy that IP address. Open Chrome, Firefox, or Safari. Paste the IP address into the address bar:

http://your_server_ip

You should see the “Welcome to nginx!” default landing page. This page confirms that the server is installed and working correctly.

Step 5: Managing the Nginx Process

As part of your server administration for beginners training, you need to know how to manage the server process. You won’t just install it and forget it. You will need to stop, start, or restart it occasionally.

Here are the essential commands:

To stop the web server:codeBash

sudo systemctl stop nginxCode language: Bash (bash)

Use this if you need to perform maintenance.

To start the web server:codeBash

sudo systemctl start nginxCode language: Bash (bash)

Use this to turn it back on after stopping it.

To restart the web server:codeBash

sudo systemctl restart nginxCode language: Bash (bash)

Use this if the server is acting glitchy or if you made major changes.

To reload the web server:codeBash

sudo systemctl reload nginxCode language: Bash (bash)

Important: Use reload instead of restart when you change Nginx configuration files. It applies changes without dropping active connections. This keeps your site live for users while you update settings.

To disable automatic startup:codeBash

sudo systemctl disable nginxCode language: Bash (bash)

This stops Nginx from starting when the server boots up.

To enable automatic startup:codeBash

sudo systemctl enable nginxCode language: Bash (bash)

This ensures Nginx starts automatically if you reboot your server. This is enabled by default.

Step 6: Setting Up Server Blocks (Virtual Hosts)

Using the default configuration is fine for testing. However, for a real website, you should use Server Blocks. In the Apache world, these are called Virtual Hosts.

Server blocks allow you to host multiple websites on a single server. This is a key part of an advanced Ubuntu server configuration guide.

For this example, we will set up a domain called your_domain.com. Replace this with your actual domain name.

Step 6.1: Create the Directory

First, create a directory for your domain. We use the -p flag to create any necessary parent directories.codeBash

sudo mkdir -p /var/www/your_domain.com/htmlCode language: Bash (bash)

Step 6.2: Assign Ownership

Currently, the directory is owned by the root user. We need to transfer ownership to the current user so you can edit files.codeBash

sudo chown -R $USER:$USER /var/www/your_domain.com/htmlCode language: Bash (bash)

Explanation:

  • chown: Change owner.
  • -R: Recursive (applies to all files inside).
  • $USER: A variable that represents your current logged-in user.

Step 6.3: Create a Sample Page

Let’s create a simple HTML file to test the block. Open a text editor:codeBash

nano /var/www/your_domain.com/html/index.htmlCode language: Bash (bash)

Inside this file, paste the following HTML code:codeHtml

<html>
    <head>
        <title>Welcome to your_domain!</title>
    </head>
    <body>
        <h1>Success! The your_domain server block is working!</h1>
    </body>
</html>Code language: HTML, XML (xml)

Press Ctrl+X, then Y, then Enter to save and exit.

Step 6.4: Create the Server Block Configuration

Now, we must tell Nginx about this new directory. We create a new configuration file in the sites-available directory.codeBash

sudo nano /etc/nginx/sites-available/your_domain.comCode language: Bash (bash)

Paste in the following configuration. This is the core of setting up a web server with Nginx:codeNginx

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

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

        server_name your_domain.com www.your_domain.com;

        location / {
                try_files $uri $uri/ =404;
        }
}Code language: HTML, XML (xml)

Explanation of the code:

  • listen 80: The server listens for traffic on port 80.
  • root: This defines where the website files are stored.
  • index: This lists the files to look for when a user visits the domain.
  • server_name: This matches the request to your specific domain name.
  • location /: This block checks if the requested file exists; if not, it returns a 404 error.

Save and close the file.

Step 6.5: Enable the Server Block

We created the file in sites-available, but Nginx won’t use it yet. We must link it to the sites-enabled directory.

Run this command:codeBash

sudo ln -s /etc/nginx/sites-available/your_domain.com /etc/nginx/sites-enabled/Code language: Bash (bash)

Step 6.6: Test and Restart

It is crucial to check for syntax errors before restarting. A typo can crash your web server.codeBash

sudo nginx -tCode language: Bash (bash)

If you see “syntax is ok” and “test is successful,” you are safe.

Finally, restart Nginx to apply the changes:codeBash

sudo systemctl restart nginxCode language: Bash (bash)

Now, if your domain is pointed to your server’s IP, visiting http://your_domain.com will show your custom HTML page.

Important Nginx Configuration Files

To master Nginx installation on Ubuntu, you must understand the file structure. Knowing where files are located helps with troubleshooting.

Here are the critical directories and files:

  • /etc/nginx: The main configuration directory. All Nginx config files reside here.
  • /etc/nginx/nginx.conf: The global configuration file. You modify this to change settings that affect all websites hosted on the server.
  • /etc/nginx/sites-available/: The storage folder for per-site “server blocks.” Nginx does not use these files unless they are linked to the enabled folder.
  • /etc/nginx/sites-enabled/: The folder where active sites are listed.
  • /var/log/nginx/access.log: Every request to your web server is recorded here. This is great for analytics.
  • /var/log/nginx/error.log: If Nginx crashes or fails to serve a page, the error reason will be here.

Understanding these files is a major part of Linux web server setup tutorial best practices.

Troubleshooting Common Issues

Even with a perfect Nginx on Ubuntu installation steps guide, things can go wrong. Here are two common issues beginners face.

1. The Default Page Still Shows

If you configured a server block but still see “Welcome to nginx,” check your server_name directive. Ensure it matches your domain exactly. Also, clear your browser cache.

2. Permissions Errors

If you see a “403 Forbidden” error, it usually means Nginx cannot read your files. Ensure the folder permissions are correct. The folder /var/www generally needs to be readable by the www-data user or the public.

Conclusion

You have successfully completed the Nginx installation on Ubuntu. You started by updating your system and installing the software. Then, you configured the firewall and verified the service status.

Furthermore, you learned how to set up server blocks to host your own custom domains. This is a critical skill for web hosting on Ubuntu Linux.

Nginx installation on Ubuntu is just the beginning. From here, you should consider securing your site with HTTPS using Let’s Encrypt. You might also want to install PHP and MySQL to create a full LEMP stack.

You now possess the foundational knowledge for managing a high-performance web server. Keep practicing your server management skills, and exploring the powerful configuration options Nginx offers.

Follow SOS-Admins on Google

SOS-Admins is your ultimate source for the latest updates, exclusive guides, and news. Don t miss our upcoming content, add us to your favorites and stay connected with the community on Google!