Install Nginx Web Server on Ubuntu 22.04

This is a short guide to show you how you can install Nginx Web Server on Ubuntu 22.04. It is a powerful and open-source web server that can be used to host more traffic at greater speeds compared to other web servers.

Nginx can be used for content caching, load balancing, as a reverse proxy for multiple protocols and serve multiple applications.

Install Nginx Web Server on Ubuntu 22.04

Nginx web server is already packaged in Ubuntu 22.04 repository making installation easy.

sudo apt update

Install Nginx web server on Ubuntu 22.04:

sudo apt install nginx

The Nginx web server runs automatically after installation and you can check the status by:

sudo systemctl status nginx


[email protected]:~$ sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset:>
     Active: active (running) since Sun 2022-05-01 01:00:43 EAT; 18min ago
       Docs: man:nginx(8)
   Main PID: 6363 (nginx)
      Tasks: 2 (limit: 3498)
     Memory: 5.1M
        CPU: 82ms
     CGroup: /system.slice/nginx.service
             ├─6363 "nginx: master process /usr/sbin/nginx -g daemon on; master>
             └─6364 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "">

Mei 01 01:00:40 itnixpro-VirtualBox systemd[1]: Starting A high performance web>
Mei 01 01:00:43 itnixpro-VirtualBox systemd[1]: Started A high performance web >
lines 1-14/14 (END)

Note that if Apache2 server is already installed in Ubuntu 22.04 and running, both web servers cannot run on the same port.

Change the port of one server or simply stop apache2 server by running the command:

sudo systemctl disable --now apache2

Then restart Nginx:

sudo systemctl restart nginx

Allow connection through firewall

Ubuntu 22.04 has ufw firewall running by default. Enable connection through ports 80 (HTTP) and 443 (HTTPS).

sudo ufw allow proto tcp from any to any port 80,443

Enable the firewall if it is not running:

sudo ufw enable

Test Nginx installation by opening your browser and typing in localhost or your server IP address. The page loads:

That is it! You have been able to install Nginx Web Server on Ubuntu 22.04. I will show you how you can serve you site using Nginx server.

Managing Nginx process

Just the same way you restarted the Nginx web server, you can use the same command to perform functions like start, stop, restart, reload, enable and disable.

Replace the word function in the command below with the function you want to do.

sudo systemctl [start|stop|restart|reload|enable|disable] nginx

Settings up server blocks with Nginx web server (Optional)

You can set server blocks to enable hosting multiple sites from the same web server.

Configure a different directory to serve clients who does not match the sites you have. Note that this is not necessary if you are hosting a single site.

Create a directory with your domain name i.e. under /var/www

sudo mkdir -p /var/www/

Create a simple HTML file for testing:

sudo nano /var/www/

Add the content below, save and close the file

        <title>Welcome to!</title>
        <p>It works!  Thank you for visiting <b></b>!</p>

Now create a server block telling Nginx to serve your sites with correct directives.

sudo nano /etc/nginx/sites-available/

Paste the content below, save and close the file:

# Virtual Host configuration for
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
server {
        listen 80;
        listen [::]:80;

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

        # here replace with you domain 

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

Next, enable this file to link with the sites-enabled directory during startup by Nginx.

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

Save and exit the file

Ensure the syntax is okay by running the command:

sudo nginx -t

Finally, restart Nginx web server:

sudo systemctl restart nginx

Now open your browser enter your url: http://your_domain, in my case Your site will displays:

Install Nginx Web Server on Ubuntu 22.04
Serving your site using Nginx

Note: If your browser redirects HTTP requests to HTTPS, you will not be able to open the domain. For Firefox browser, go to about:config, toggle network.stricttransportsecurity.preloadlist and browser.fixup.fallback-to-https to false then restart your browser.

Uninstall Nginx on Ubuntu 22.04

Enter the command below to uninstall Nginx:

sudo apt remove --purge --auto-remove nginx

You have reached the end of this guide of how to install Nginx Web Server on Ubuntu 22.04. It was nice to have you here and I hope the guide was helpful. More information about the web server can be found in Nginx documentation.

More similar tutorials

Install NFS Server on Ubuntu 22.04

Install Apache HTTP Server on Debian 11

Install Cacti on Ubuntu 22.04

Install Nginx on Debian 11

Android Developer | Linux | Technical Writer | Backend Developer

Leave a Comment