Nginx is a popular web server used to serve websites, reverse-proxy applications, and handle incoming HTTP and HTTPS traffic. If you've just created a Linux VPS, installing Nginx is one of the first steps toward hosting your own website.
In this tutorial, we'll install Nginx on Ubuntu Server 24.04 LTS, configure a website using an Nginx server block, and test that everything works correctly. By the end, you'll have a working web server serving a custom HTML page and know how to check its configuration and troubleshoot common problems.
Prerequisites: You need an Ubuntu VPS, SSH access with an account that can use
sudo, and a public IP address. If you're following our Linux VPS setup guide, your server should already be updated and have basic firewall rules configured.
Step 1: Update Ubuntu
Connect to your server through SSH, then refresh the package lists:
sudo apt update
Install available package upgrades:
sudo apt upgrade
Review the proposed changes and confirm when prompted. Keeping the operating system updated helps ensure you install current, security-maintained packages.
Step 2: Install Nginx
Install Nginx using Ubuntu's package manager:
sudo apt install nginx
Check the service status:
sudo systemctl status nginx
Look for a status similar to:
Active: active (running)
Press q to exit the status screen. Ubuntu's package starts Nginx during installation. Check whether it is enabled to start after a reboot:
systemctl is-enabled nginx
If the output is disabled, enable it:
sudo systemctl enable nginx
Check the installed version with:
nginx -v
The exact version depends on the Ubuntu package updates available to your server.
Step 3: Allow HTTP Through the Firewall
If you're using UFW, allow HTTP traffic on port 80:
sudo ufw allow 'Nginx HTTP'
Check the firewall rules:
sudo ufw status
You should see an HTTP rule allowing traffic on port 80. If UFW is inactive, don't enable it without first reviewing the existing rules and allowing your actual SSH port. Otherwise, you could lose remote access.
Also check your cloud provider's firewall or security group. It must allow inbound TCP traffic on port 80 for a public HTTP website to be reachable.
Step 4: Test the Default Nginx Website
Open a browser and visit your server's public IP address:
http://YOUR_SERVER_IP
Replace YOUR_SERVER_IP with the actual IP address of your VPS. You should see the default Nginx welcome page.
You can also test from the server itself:
curl -I http://localhost
A typical response begins with:
HTTP/1.1 200 OK
Server: nginx
This confirms that Nginx is responding locally. It doesn't, by itself, prove that the website is reachable from the internet. If the page doesn't load, check the service status, UFW rules, provider firewall, and server IP address before proceeding.
Step 5: Create a Directory for Your Website
We'll create a separate directory for a simple website named example.com. This domain is a placeholder; replace it with your own domain if you have one.
Create the website directory:
sudo mkdir -p /var/www/example.com/html
Set the directory owner to your current administrative user. The USER environment variable normally identifies the account running your shell:
sudo chown -R "$USER":"$USER" /var/www/example.com
Set directory and file permissions so that Nginx can read the site files:
sudo find /var/www/example.com -type d -exec chmod 755 {} \;
sudo find /var/www/example.com -type f -exec chmod 644 {} \;
Directories are readable and traversable by other users, while ordinary files remain readable but not writable by everyone.
Create a simple homepage:
nano /var/www/example.com/html/index.html
Add this HTML:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My First Nginx Website</title>
</head>
<body>
<h1>Hello from Nginx!</h1>
<p>My Ubuntu web server is working.</p>
</body>
</html>
Save the file and exit the editor.
Step 6: Configure an Nginx Server Block
Nginx uses server blocks to configure how it handles requests for different websites. Create a configuration file:
sudo nano /etc/nginx/sites-available/example.com
Add this configuration:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
The main directives are:
listen 80accepts HTTP connections over IPv4.listen [::]:80accepts HTTP connections over IPv6 when IPv6 is configured.server_namespecifies the domain names this server block handles.rootidentifies the directory containing the website files.indexdefines the default file to serve.try_fileschecks for the requested file or directory and returns a 404 error if neither exists.
If you don't yet have a domain, you can temporarily use your server's IP address for testing by replacing the server_name line with:
server_name YOUR_SERVER_IP;
Requests made to the IP may still be handled by the default server block if another configuration takes precedence. For a predictable result, test the custom server block using your actual domain or an explicit Host header.
Step 7: Enable Your Website
Ubuntu's Nginx package uses sites-available to hold configurations and sites-enabled to identify the configurations that should be active. Enable the new site by creating a symbolic link:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
If the link already exists, don't create it again. Before reloading Nginx, test the configuration:
sudo nginx -t
A successful test reports output similar to:
syntax is ok
test is successful
If the test reports an error, fix the indicated file or line before proceeding. Then reload Nginx to apply the configuration:
sudo systemctl reload nginx
A reload applies configuration changes without a full service restart, provided the configuration is valid.
What about the default site? Leave it enabled until your new website works as expected. If the default welcome page takes precedence during testing, inspect the enabled server blocks and their server_name and default_server settings. You can disable the default site later if you no longer need it.
Step 8: Test Your Website
If your domain's DNS records already point to the VPS, open:
http://example.com
You should see your custom heading: Hello from Nginx!
If DNS isn't configured yet, test the server block directly from the VPS:
curl -H 'Host: example.com' http://127.0.0.1/
You should see the HTML content from your new homepage. This verifies that Nginx can select the server block for the specified hostname and serve the page. Public access still requires working DNS records and any required provider-level firewall rules.
For a public website, create DNS records that point your domain to the VPS's public IP address. An A record maps a hostname to IPv4; an AAAA record maps it to IPv6. Only publish an AAAA record if your server is correctly reachable over IPv6.
Step 9: Prepare for HTTPS
HTTP sends website traffic without transport encryption. For a public website, configure HTTPS before collecting logins, personal information, or other sensitive data.
First, allow HTTPS through UFW:
sudo ufw allow 'Nginx HTTPS'
Check that HTTPS is permitted in the provider's firewall as well. For a domain that already resolves to your server, you can use Let's Encrypt with Certbot to obtain and configure a TLS certificate. The appropriate installation method depends on your Ubuntu environment and preferred package source.
Do not attempt certificate issuance until your DNS records are correct and the necessary validation traffic can reach your server. Ubuntu's Nginx configuration guide covers HTTPS server blocks and links to certificate guidance.
Troubleshooting Common Nginx Problems
The website is unreachable
Check whether Nginx is running:
sudo systemctl status nginx
Then check UFW:
sudo ufw status verbose
Verify the VPS public IP, provider firewall rules, and DNS records. A service can work locally while remaining unreachable from the internet.
Nginx reports a configuration error
Run:
sudo nginx -t
Read the reported filename and line number. Correct the configuration, test again, and reload only after the test succeeds.
You see a 403 Forbidden error
Check that the configured root directory and homepage exist:
ls -la /var/www/example.com/html
Nginx needs permission to traverse the parent directories and read the website files. Review the file ownership, permissions, and relevant Nginx logs before changing permissions broadly.
You still see the default welcome page
Inspect the enabled configurations:
ls -l /etc/nginx/sites-enabled/
Check whether the hostname matches your new server_name and whether another server block is handling the request. For more detail, inspect the Nginx logs:
sudo tail -n 50 /var/log/nginx/error.log
sudo tail -n 50 /var/log/nginx/access.log
Conclusion
You've now installed Nginx on Ubuntu, configured a website directory, created a server block, and tested the server's response. These fundamentals are useful whether you're hosting a static website, setting up a reverse proxy, or preparing a VPS for a web application.
Before putting a public site into production, confirm that DNS works, firewall rules are correct, the server is updated, and HTTPS is configured.


