Introduction
Nginx is an open-source, high-performance web server that also functions as a reverse proxy, load balancer, and HTTP cache. By installing Nginx on your Raff VM, you get a lightweight server capable of handling thousands of concurrent connections with minimal memory usage, making it an excellent foundation for hosting websites and web applications.
Originally created by Igor Sysoev to solve the C10K problem (handling 10,000 simultaneous connections), Nginx has become one of the most widely used web servers on the internet. Its event-driven, asynchronous architecture gives it a significant performance advantage over traditional process-per-connection servers, especially under heavy load.
In this tutorial, you will install Nginx from the default Ubuntu package repository, configure the UFW firewall to allow web traffic, verify that the server is running, and set up a server block to host a custom website. By the end, you will have a fully functional Nginx web server ready to serve content.
Step 1 — Update the Package Index and Install Nginx
Nginx is available in Ubuntu 24.04's default package repository, so installation is straightforward using the APT package manager.
First, update your local package index to ensure you are installing the latest available version:
bashsudo apt update
Then install Nginx:
bashsudo apt install -y nginx
APT will install Nginx along with any required dependencies. Once the installation completes, Nginx starts automatically and is enabled to launch at boot.
Verify the installed version:
bashnginx -v
Expected output:
nginx version: nginx/1.24.0 (Ubuntu)
Note
The version from Ubuntu's default repository is a stable release. If you need the latest mainline version with the newest features, you can add the official Nginx repository instead. For most use cases, the default repository version is recommended.
Step 2 — Verify the Nginx Service
Nginx registers itself as a systemd service during installation. Confirm that the service is active and running:
bashsudo systemctl status nginx
You should see output that includes active (running), confirming Nginx is operational:
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; preset: enabled)
Active: active (running)
Here are the essential systemd commands for managing Nginx:
bashsudo systemctl stop nginx # Stop the web server
sudo systemctl start nginx # Start the web server
sudo systemctl restart nginx # Stop and restart the service
sudo systemctl reload nginx # Apply config changes without downtime
sudo systemctl enable nginx # Enable start at boot
sudo systemctl disable nginx # Disable start at boot
The reload command is particularly useful in production because it applies configuration changes without dropping active connections.
Step 3 — Configure the UFW Firewall
Uncomplicated Firewall (UFW) is the default firewall management tool on Ubuntu. If UFW is enabled on your server, you need to allow Nginx traffic through the firewall so visitors can reach your web server.
Nginx registers several UFW application profiles during installation. List them with:
bashsudo ufw app list
You will see three Nginx-related profiles:
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH
Each profile opens different ports:
- Nginx HTTP — Opens port 80 only (unencrypted web traffic)
- Nginx HTTPS — Opens port 443 only (encrypted TLS/SSL traffic)
- Nginx Full — Opens both port 80 and port 443
Since you have not yet configured SSL, allow HTTP traffic only for now:
bashsudo ufw allow 'Nginx HTTP'
Verify the firewall status:
bashsudo ufw status
Expected output:
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)
Warning
Always ensure that OpenSSH is allowed in UFW before enabling the firewall. Blocking SSH will lock you out of your server.
Tip
When you later set up SSL with Let's Encrypt, switch the rule to Nginx Full to allow both HTTP and HTTPS traffic.
Step 4 — Test the Default Nginx Page
With the firewall configured, verify that Nginx is serving content by accessing your server's public IP address in a web browser.
If you do not know your server's public IP, retrieve it with:
bashcurl -4 icanhazip.com
Open a browser and navigate to:
http://your_server_ip
Replace your_server_ip with the IP address returned by the command above. You should see the default Nginx welcome page with the message "Welcome to nginx!" This confirms that Nginx is installed, running, and accessible through the firewall.
The default web root directory is located at /var/www/html/. The welcome page you see is the index.nginx-debian.html file in that directory.
Step 5 — Set Up a Server Block
Nginx uses server blocks (similar to Apache's virtual hosts) to host multiple websites on a single server. Each server block defines a separate site with its own document root, domain name, and configuration.
By default, Nginx serves content from /var/www/html. You will create a new directory structure for your domain and configure a dedicated server block.
Create the document root directory for your site. Replace example.com with your actual domain name throughout this step:
bashsudo mkdir -p /var/www/example.com/html
Set the ownership to your current user:
bashsudo chown -R $USER:$USER /var/www/example.com/html
Set the correct permissions:
bashsudo chmod -R 755 /var/www/example.com
Create a simple HTML page to test the server block:
bashnano /var/www/example.com/html/index.html
Add the following content:
html<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome to example.com</title>
</head>
<body>
<h1>Your Nginx server block is working!</h1>
<p>This page is served from /var/www/example.com/html/ on your Raff VM.</p>
</body>
</html>
Save and close the file.
Now create the server block configuration file:
bashsudo nano /etc/nginx/sites-available/example.com
Add the following configuration:
nginxserver {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
This configuration tells Nginx to listen on port 80 for requests to example.com and www.example.com, serve files from the /var/www/example.com/html directory, and return a 404 error if a requested file is not found.
Enable the server block by creating a symbolic link to the sites-enabled directory:
bashsudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
To prevent a potential hash bucket memory issue when adding additional server names, uncomment one line in the main Nginx configuration:
bashsudo nano /etc/nginx/nginx.conf
Find and uncomment the server_names_hash_bucket_size directive:
nginxserver_names_hash_bucket_size 64;
Save and close the file.
Test the Nginx configuration for syntax errors:
bashsudo nginx -t
Expected output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
If you see any errors, review the configuration files for typos before proceeding.
Reload Nginx to apply the changes:
bashsudo systemctl reload nginx
Step 6 — Verify the Server Block
If your domain's DNS is already pointed to your server, open a browser and navigate to http://example.com. You should see the custom HTML page you created.
If you do not have a domain configured yet, you can test locally by adding a temporary entry to your local machine's hosts file. On your local computer (not the server), edit the hosts file:
- Linux/macOS:
/etc/hosts - Windows:
C:\Windows\System32\drivers\etc\hosts
Add this line:
your_server_ip example.com www.example.com
Then visit http://example.com in your browser. You should see the message "Your Nginx server block is working!" confirming that the server block is correctly configured.
Note
Remove the hosts file entry once you have verified the setup and configured proper DNS records.
Step 7 — Review Important Nginx Files and Directories
Now that Nginx is running, it helps to know where the key files and directories are located for ongoing management.
Content directories:
/var/www/html/— Default web content directory. Contains the default Nginx welcome page./var/www/example.com/html/— Your custom site directory (created in this tutorial).
Server configuration:
/etc/nginx/nginx.conf— The main Nginx configuration file. Controls global settings like worker processes, logging, and included modules./etc/nginx/sites-available/— Stores server block configuration files. Files here are not active until linked fromsites-enabled./etc/nginx/sites-enabled/— Contains symbolic links to active server block configurations. Nginx reads configurations from this directory./etc/nginx/conf.d/— An alternative directory for additional configuration snippets.
Log files:
/var/log/nginx/access.log— Records every request processed by Nginx. Useful for traffic analysis and debugging./var/log/nginx/error.log— Records errors encountered by Nginx. Check this file first when troubleshooting issues.
Conclusion
You have installed Nginx on your Raff Ubuntu 24.04 VM, configured the UFW firewall to allow web traffic, set up a server block to host a custom website, and familiarized yourself with the key configuration files and directories.
From here, you can:
- Secure your site with a free SSL certificate using Let's Encrypt
- Set up a full LEMP stack (Linux, Nginx, MySQL, PHP) to host dynamic web applications
- Configure Nginx as a reverse proxy for Node.js, Python, or other application servers
- Enable Gzip compression and browser caching to improve performance
Raff VMs include NVMe SSD storage and unmetered bandwidth on all tiers, which complement Nginx's high-concurrency architecture for fast content delivery under heavy traffic.