If you just bought your first VPS, learning to install Nginx on Ubuntu is one of the most useful first skills you can pick up. This guide shows you how to install Nginx on an Ubuntu VPS from start to finish. Nginx is fast, lightweight, and powers a huge share of the world’s busiest websites, which makes it an excellent choice for beginners and experienced users alike.
Table of Contents
Prerequisites
Before you begin, make sure you have the following:
- A VPS running Ubuntu (20.04, 22.04, or 24.04 all work fine with these steps)
- Root access or a user account with sudo privileges
- A terminal or SSH client to connect to your server (PuTTY on Windows, or the built-in Terminal on macOS and Linux)
If you have not connected to your VPS yet, open your terminal and run:
ssh root@your-server-ip
Replace your-server-ip with the actual IP address your hosting provider gave you. Accept the security prompt if this is your first connection.
Step 1: Update Your Package Lists
It is good practice to update your package lists before installing anything. This ensures you get the latest available version of Nginx from Ubuntu’s repositories.
sudo apt update
You can also upgrade already-installed packages while you are at it, though this is optional:
sudo apt upgrade -y
Step 2: Install Nginx
Installing Nginx on Ubuntu is a single command:
sudo apt install nginx -y
The -y flag tells the installer to answer “yes” to any confirmation prompts automatically. The installation usually takes less than a minute. Ubuntu will also start the Nginx service automatically once the installation finishes.
Step 3: Start and Enable the Nginx Service
Even though Nginx usually starts on its own, it is worth confirming it is running and making sure it starts automatically whenever your server reboots:
sudo systemctl start nginx
sudo systemctl enable nginx
Now check the service status:
sudo systemctl status nginx
You should see the word active (running) in green. Press q to exit the status view and return to your command prompt.
Step 4: Allow Web Traffic Through the Firewall
Ubuntu’s built-in firewall, UFW, blocks incoming connections by default on many VPS images. If your provider enabled UFW, you need to open the HTTP (port 80) and HTTPS (port 443) ports so visitors can reach your site.
First, check whether UFW is active:
sudo ufw status
If it says inactive, you can skip this step or enable the firewall later. If it is active, allow Nginx traffic with:
sudo ufw allow 'Nginx Full'
This single rule opens both port 80 and port 443. If you only want plain HTTP for now, use sudo ufw allow 'Nginx HTTP' instead. Then reload the firewall:
sudo ufw reload

Step 5: Verify Nginx Is Working
Open your web browser and visit your server’s IP address:
http://your-server-ip
You should see the default Nginx welcome page with the message “Welcome to nginx!”. If the page loads, congratulations — your web server is up and running.

You can also verify from the command line:
curl -I http://localhost
A response starting with HTTP/1.1 200 OK confirms Nginx is serving pages correctly.
Step 6: Learn the Basic Nginx Management Commands
You will use these commands constantly as you manage your server, so it is worth memorizing them:
- Check status:
sudo systemctl status nginx - Stop the server:
sudo systemctl stop nginx - Start the server:
sudo systemctl start nginx - Restart the server:
sudo systemctl restart nginx - Reload configuration without dropping connections:
sudo systemctl reload nginx - Test configuration files for syntax errors:
sudo nginx -t
The reload command is especially handy. Whenever you change a configuration file, run sudo nginx -t first to check for errors, then sudo systemctl reload nginx to apply the changes without any downtime.
Step 7 (Optional): Set Up a Simple Server Block
Nginx uses “server blocks” (similar to Apache’s virtual hosts) to host multiple websites on one server. Here is how to create one for a practice domain:
- Create a directory for your site’s files:
sudo mkdir -p /var/www/example.com/html
- Create a simple test page:
echo "<h1>It works!</h1>" | sudo tee /var/www/example.com/html/index.html
- Create a server block configuration file:
sudo nano /etc/nginx/sites-available/example.com
- Paste in this basic configuration:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
- Enable the site by linking it into the
sites-enableddirectory:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
- Test the configuration and reload Nginx:
sudo nginx -t
sudo systemctl reload nginx
Replace example.com with your real domain name and point your domain’s DNS records at your server’s IP address, and your site will go live.
Troubleshooting Common Problems
The welcome page does not load. Double-check that Nginx is running (sudo systemctl status nginx) and that your firewall allows HTTP traffic. Some providers also have an external firewall or security group in their control panel — make sure port 80 is open there too.
“403 Forbidden” error. This usually means a file permission problem. Your web files should be readable by the www-data user that Nginx runs as. A quick fix for a standard site directory is:
sudo chown -R www-data:www-data /var/www/example.com/html
“Address already in use” error. Another program (often Apache) may already be listening on port 80. Check with sudo ss -tlnp | grep :80 and stop the conflicting service before starting Nginx.
Conclusion: Install Nginx on Ubuntu in Minutes
You now know how to install Nginx on an Ubuntu VPS, open the firewall, verify the installation, and manage the service with basic commands. From here, the natural next steps are pointing a domain name at your server, setting up additional server blocks, and adding free HTTPS encryption with a certificate. Nginx is a solid foundation — take your time exploring its configuration, and always test with sudo nginx -t before reloading.
Frequently Asked Questions
Is Nginx better than Apache?
Neither is universally better — it depends on your needs. Nginx generally handles high numbers of simultaneous connections with lower memory usage, which is why it is popular for busy sites and as a reverse proxy. Apache is known for its flexibility and extensive module system. For a beginner hosting a typical website on a VPS, Nginx is an excellent and simple choice.
Do I need a domain name to use Nginx?
No. You can access your server directly through its IP address, which is perfect for learning and testing. You only need a domain name when you want visitors to reach your site through a memorable address instead of a raw IP.
Where are Nginx configuration files stored?
The main configuration file is /etc/nginx/nginx.conf. Individual site configurations live in /etc/nginx/sites-available/ and are activated by linking them into /etc/nginx/sites-enabled/. The default website files are served from /var/www/html/.
How do I add HTTPS to my Nginx site?
The most common free option is Let’s Encrypt, which you can set up with the Certbot tool. After installing Certbot’s Nginx plugin, a single command can obtain a certificate and configure Nginx to use it automatically. Always back up your working configuration before making HTTPS changes for the first time.


