How to install Nginx on Ubuntu 24.04
This guide walks you through installing Nginx 1.24.0 on Ubuntu 24.04 LTS. You will configure the default site, set up automatic updates, and verify the web server is running correctly.
You will install Nginx 1.24.0 on Ubuntu 24.04 LTS and configure the default virtual host. These steps target Ubuntu 24.04 LTS (Jammy Jellyfish) with a non-root user account. The process includes updating system packages, adding the official repository, and securing the server.
Prerequisites
- Ubuntu 24.04 LTS installed and updated
- SSH access to the server or a local terminal
- Root privileges or sudo access
- At least 200 MB of free disk space
Step 1: Update system packages
Before installing Nginx, ensure your package index is current. This prevents conflicts between system libraries and the web server.
sudo apt update
Expected output shows the number of packages to be upgraded:
Get:1 http://archive.ubuntu.com/ubuntu noble InRelease [180 kB]
Get:2 http://security.ubuntu.com/ubuntu noble-security InRelease [110 kB]
...
Fetched 14.5 MB in 3s (4516 kB/s)
Reading package lists... Done
Step 2: Install Nginx
Install the official Nginx package. This installs version 1.24.0 by default on Ubuntu 24.04.
sudo apt install nginx -y
The installer will display a summary of packages to be installed and ask for confirmation. Press Y to proceed.
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
nginx
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 22.5 MB of archives.
After this operation, 97.0 MB of additional disk space will be used.
Do you want to continue? [Y/n]
Step 3: Configure the default site
Nginx creates a default configuration file at /etc/nginx/sites-available/default. Open this file in a text editor to verify the server_name directive points to your domain or IP address.
sudo nano /etc/nginx/sites-available/default
Ensure the server_name line matches your domain name or leave it as localhost for testing. Save the file and exit the editor.
Step 4: Test the configuration
Run the built-in test command to check for syntax errors before restarting the service.
sudo nginx -t
Expected output confirms the configuration is valid:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Step 5: Restart Nginx
Apply the changes by restarting the Nginx service. This ensures the new configuration is loaded.
sudo systemctl restart nginx
Verify the installation
Open a web browser and navigate to http://your-server-ip. You should see the default Nginx welcome page. Alternatively, use curl from the terminal:
curl http://localhost
Expected output:
Welcome to nginx!
Welcome to nginx!
If you see this page, the web server is running correctly.
Troubleshooting
Error: "nginx: [emerg] bind() to 0.0.0.0:80 failed"
This error occurs when port 80 is already in use. Check for conflicting services with sudo lsof -i :80 and stop any conflicting processes. Ensure no other web server like Apache is running on the same port.
Error: "nginx: [emerg] unknown directive 'example_directive'"
This indicates a syntax error in your configuration file. Check the file mentioned in the error message for typos or unsupported directives. Run sudo nginx -t again after fixing the issue.
Error: "Permission denied: /var/www/html/index.html"
Ensure the web server user (www-data) has read permissions for the document root. Run sudo chown -R www-data:www-data /var/www/html to fix ownership issues.
Error: "upstream timed out (110: Connection timed out)"
This happens when a backend server is unreachable. Check the backend server's network connectivity and ensure it responds to requests. Verify the upstream block in your Nginx configuration file is correct.