I'm always excited to take on new projects and collaborate with innovative minds.

Phone

+91-9723273747

Email

contact@manojitaliya.com

Address

Surat, Gujarat, INDIA

Social Links

Web Servers

How to Install Nginx on Ubuntu 22.04 (Step-by-Step Guide)

How to Install Nginx on Ubuntu 22.04 (Step-by-Step Guide)

Nginx is a high-performance web server that can also be used as a reverse proxy, load balancer, and HTTP cache. It is known for its scalability and speed. In this tutorial, you’ll learn how to install Nginx on Ubuntu 22.04, manage the Nginx service, configure the firewall, and set up server blocks (virtual hosts) to host multiple websites on a single server.

Prerequisites

  • A server running Ubuntu 22.04
  • Root or sudo access to the server
  • Basic terminal/SSH knowledge

Step 1: Update System Packages

First, update the system package index:

sudo apt update && sudo apt upgrade -y

Step 2: Install Nginx

Use the default package manager to install Nginx:

sudo apt install nginx -y

After installation, verify it:

nginx -v

Step 3: Adjust Firewall Rules

Allow Nginx traffic through the UFW firewall:

sudo ufw allow 'Nginx Full'

Check UFW status:

sudo ufw status

Step 4: Start and Enable Nginx

Enable and start the Nginx service:

sudo systemctl enable nginx
sudo systemctl start nginx

Check status:

sudo systemctl status nginx

Step 5: Verify Nginx is Running

Open your browser and go to your server's IP address:

http://your_server_ip

You should see the default Nginx welcome page.

Step 6: Set Up Server Blocks (Virtual Hosts)

Server blocks allow you to host multiple domains on a single server. We’ll set up an example block for example.com.

Create Directory for Your Website

sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.com/html
sudo chmod -R 755 /var/www/example.com

Create a Sample Index Page

nano /var/www/example.com/html/index.html

Add the following content:

<html>
  <head>
    <title>Welcome to Example.com</title>
  </head>
  <body>
    <h1>Success! Your Nginx server block is working.</h1>
  </body>
</html>

Create a Server Block Configuration File

sudo nano /etc/nginx/sites-available/example.com

Paste the following configuration:

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

    root /var/www/example.com/html;
    index index.html index.htm;

    server_name example.com www.example.com;

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

Enable the Server Block

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Test Nginx Configuration

sudo nginx -t

Reload Nginx

sudo systemctl reload nginx

Step 7: Update DNS (Optional)

Point your domain’s DNS A record to your server's IP address so that example.com loads correctly.

Step 8: Add More Server Blocks

To host multiple domains, repeat steps above with new directories, index.html, and config files (e.g., test.com).

Optional: Secure with Let's Encrypt SSL

To secure your Nginx sites with HTTPS using Let's Encrypt:

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com

Set up automatic renewal:

sudo certbot renew --dry-run

Conclusion

You have successfully installed Nginx on Ubuntu 22.04, configured the firewall, and set up server blocks to host multiple websites. You can now extend this by adding SSL, reverse proxy setups, PHP integration, and performance optimization.

3 min read
Jun 24, 2025
By MANOJ ITALIYA
Share

Leave a comment

Your email address will not be published. Required fields are marked *

Related posts

Jun 24, 2025 • 3 min read
How to Install Docker on Ubuntu (Beginner to Pro Guide)

Learn how to install Docker on Ubuntu 22.04, 20.04, or 18.04 with this complete guide. Covers Docker...

Jun 24, 2025 • 3 min read
How to Install WordPress on Ubuntu
Your experience on this site will be improved by allowing cookies. Cookie Policy