Web Servers 5d ago 4 views 3 min read

How to install Nginx on Ubuntu 24.04

Install the latest stable Nginx web server on a fresh Ubuntu 24.04 LTS instance using the official package repository. Configure basic security headers and verify the service is running correctly.

Riya K.
Updated 8h ago
Sponsored

Cloud VPS — scale in minutes

Instantly deploy SSD cloud VPS with guaranteed resources, snapshots and per-hour billing. Pay only for what you use.

This guide walks you through installing the latest stable version of Nginx on a fresh Ubuntu 24.04 LTS system. You will add the official repository, install the package, configure basic security headers, and verify the service is running correctly.

Prerequisites

  • Ubuntu 24.04 LTS (Jammy Jellyfish) running on a VPS or local machine.
  • Root access or sudo privileges to install software.
  • At least 500 MB of free disk space.
  • Active internet connection to download packages.

Step 1: Update the package index

Before installing any new software, ensure your local package index is up to date. This prevents the installation of outdated dependencies.

sudo apt update

You will see a list of packages to be upgraded if any are available. Press Enter to confirm the upgrade or simply run the command to proceed with the index update.

Step 2: Install Nginx

Install the Nginx package using the APT package manager. The command below installs the default stable version available in the Ubuntu repositories.

sudo apt install nginx -y

The installer will download Nginx version 1.26.x and its dependencies. You will see progress bars and a summary of packages installed. Press Enter when prompted to confirm the installation.

Step 3: Configure the default site

Nginx creates a default configuration file that serves a simple welcome page. You will edit this file to remove the default content and point it to your own project or leave it for testing.

sudo nano /etc/nginx/sites-available/default

Inside the editor, delete the existing block that starts with server { and contains the default welcome page text. Replace it with a simple block that returns a 200 OK status:

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

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

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

Save the file by pressing Ctrl+O, then Enter. Exit the editor by pressing Ctrl+X.

Step 4: Enable the Nginx service

Start the Nginx service and enable it to start automatically on system boot. This ensures the web server is always available.

sudo systemctl start nginx
sudo systemctl enable nginx

The first command starts the service immediately. The second command creates a symlink in the systemd directory to ensure the service starts after a reboot.

Step 5: Configure security headers

Add security headers to the default site configuration to improve protection against common web vulnerabilities. This includes disabling server version disclosure and enabling HTTP Strict Transport Security.

sudo nano /etc/nginx/sites-available/default

Add the following lines inside the server block, before the location / directive:

add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
add_header X-XSS-Protection "1; mode=block";
add_header Referrer-Policy "strict-origin-when-cross-origin";
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

Save the file and exit the editor as described in Step 3.

Verify the installation

Check the status of the Nginx service to ensure it is active and running without errors. You will see active (running) in the output.

sudo systemctl status nginx

Visit your server's IP address in a web browser. You should see the default Nginx welcome page or your custom content if you modified the index file. If you see a 404 error, check the file permissions on /var/www/html.

Troubleshooting

If the service fails to start, check the system logs for specific error messages related to configuration syntax or missing modules.

sudo journalctl -u nginx -n 50

Common error nginx: [emerg] unknown directive "x" indicates a typo in the configuration file. Ensure all directives are spelled correctly and are enclosed in the correct blocks. Run sudo nginx -t to test the configuration syntax before restarting the service.

If the service restarts automatically but the port is not listening, check if another process is using port 80. Use sudo lsof -i :80 to identify the process. Kill the conflicting process or change the port in the Nginx configuration.

If you cannot access the site via HTTP, ensure the firewall allows traffic on port 80. On Ubuntu 24.04 with UFW, run sudo ufw allow 'Nginx Full' or sudo ufw allow 80/tcp to open the port.

Sponsored

Linux Dedicated Server

Rock-solid Linux dedicated servers with root access, KVM-IPMI and fully managed options. CentOS, Ubuntu, Debian, Rocky and AlmaLinux.

Tags: UbuntuNginxWeb ServerSetup
0
Was this helpful?

Related tutorials

Comments 0

Login to leave a comment.

No comments yet — be the first to share your thoughts.