Nginx (pronounced "engine-ex") is one of the most popular, high-performance web servers in the world. Engineered with an event-driven, asynchronous architecture, Nginx powers millions of high-traffic websites, web applications, and APIs by serving static content with ultra-low memory consumption and functioning as a robust reverse proxy, load balancer, and HTTP cache.
Whether you are running Ubuntu 26.04 LTS, Ubuntu 24.04 LTS, Ubuntu 22.04 LTS, or Ubuntu 20.04 LTS on an Aveshost Linux VPS, setting up Nginx is straightforward. In this step-by-step tutorial, you will learn how to install Nginx, configure the UFW firewall, set up domain server blocks (virtual hosts), test configuration syntax, and master essential service management commands.
Prerequisites
- An active Linux Virtual Private Server (VPS) running Ubuntu (20.04, 22.04, 24.04, or 26.04 LTS).
- SSH root access or a non-root user with
sudoprivileges. - A registered domain name pointing its DNS A Record to your VPS server’s public IP address (see How to Point Your Domain to a VPS IP Address).
- UFW (Uncomplicated Firewall) installed on your server.
Step-by-Step Guide: How to Set Up Nginx on Ubuntu Server
Step 1: Connect to Your VPS and Update Packages
- Open your terminal (macOS/Linux) or PowerShell/Command Prompt (Windows) and connect to your VPS via SSH:
ssh username@YOUR_SERVER_IP # OR connect as root ssh root@YOUR_SERVER_IP - Update your local package index and upgrade existing packages to ensure your system is up-to-date:
sudo apt update && sudo apt upgrade -y
Step 2: Install Nginx via APT
- Install Nginx directly from the official Ubuntu repositories:
sudo apt install nginx -y - Once installed, start and enable the Nginx service so that it automatically boots with the server:
sudo systemctl start nginx sudo systemctl enable nginx - Verify that Nginx is running properly:
sudo systemctl status nginxYou should see an active status displaying Active: active (running).
Step 3: Adjust the UFW Firewall
Before testing Nginx in a web browser, ensure your firewall allows web traffic on HTTP (port 80) and HTTPS (port 443):
- Check available UFW application profiles:
sudo ufw app list - Allow both OpenSSH and full Nginx traffic (HTTP + HTTPS):
sudo ufw allow OpenSSH sudo ufw allow 'Nginx Full' - Enable the firewall (if not already enabled) and verify status:
sudo ufw enable sudo ufw status
Step 4: Verify Nginx in Your Web Browser
- Open your favorite web browser and visit your server’s public IP address:
http://YOUR_SERVER_IP - You should see the default "Welcome to nginx!" landing page confirming that your web server is functioning correctly.
Step 5: Setting Up Domain Server Blocks (Virtual Hosts)
Nginx uses Server Blocks (similar to Virtual Hosts in Apache) to encapsulate configuration details and host multiple websites or domain names from a single VPS server.
1. Create the Document Root Directory
Create a dedicated directory for your domain (replace domain.com with your actual domain name):
sudo mkdir -p /var/www/domain.com/html
2. Assign Ownership and Permissions
sudo chown -R $USER:$USER /var/www/domain.com/html
sudo chmod -R 755 /var/www/domain.com
3. Create a Sample HTML Page
Create a test index.html file using nano:
nano /var/www/domain.com/html/index.html
Paste the following sample HTML content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Success! Welcome to domain.com</title>
<style>
body { font-family: Arial, sans-serif; text-align: center; padding: 60px; background: #f8fafc; }
h1 { color: #0284c7; }
</style>
</head>
<body>
<h1>Success! Nginx Server Block is Working on Ubuntu!</h1>
<p>Your domain.com website is hosted on your Aveshost VPS.</p>
</body>
</html>
Save and exit by pressing CTRL + O, ENTER, and then CTRL + X.
4. Create the Nginx Server Block Configuration
Create a new server block file in /etc/nginx/sites-available/:
sudo nano /etc/nginx/sites-available/domain.com
Paste the following configuration:
server {
listen 80;
listen [::]:80;
server_name domain.com www.domain.com;
root /var/www/domain.com/html;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ =404;
}
# Logging settings
access_log /var/log/nginx/domain.com.access.log;
error_log /var/log/nginx/domain.com.error.log;
}
Save and exit (CTRL + O, ENTER, CTRL + X).
5. Enable the Server Block & Test Syntax
- Enable the configuration by creating a symbolic link to the
sites-enableddirectory:sudo ln -s /etc/nginx/sites-available/domain.com /etc/nginx/sites-enabled/ - Test your Nginx configuration syntax to ensure there are no formatting or typographical errors:
sudo nginx -tYou should see confirmation: syntax is ok & test is successful.
- Restart or reload Nginx to apply the changes:
sudo systemctl reload nginx - Visit
http://domain.comin your browser to view your live server block page!
Essential Nginx Management Commands Reference
| Action | Command | Purpose |
|---|---|---|
| Check Status | sudo systemctl status nginx |
Checks if Nginx is running and displays recent logs. |
| Test Syntax | sudo nginx -t |
Validates configuration files before reloading. |
| Graceful Reload | sudo systemctl reload nginx |
Applies configuration updates without dropping connections. |
| Restart Server | sudo systemctl restart nginx |
Completely stops and restarts the Nginx process. |
| Stop Server | sudo systemctl stop nginx |
Stops the Nginx web server. |
| Start Server | sudo systemctl start nginx |
Starts the Nginx web server. |
Next Step: Secure Your Website with SSL
Now that Nginx is running, the next crucial step is securing your domain with a free, automated Let’s Encrypt SSL certificate using Certbot to enable HTTPS encryption.
Need Further Assistance?
If you encounter any issues configuring Nginx server blocks, firewall rules, or DNS settings on your Ubuntu VPS, our technical support team is available 24/7. Feel free to submit a support ticket through your client area for expert assistance.