Getting Nginx Running on Your Ubuntu Server
If you’re setting up a web server on Ubuntu, Nginx is one of the best choices out there. It’s lightweight, handles traffic efficiently, and doesn’t eat up your resources. In this guide, We’ll walk you through how to install Nginx on Ubuntu, verify it’s running, and apply basic configuration.
What You’ll Need
Before we dive in, make sure you have:
- A VPS, cloud instance, or dedicated server
- An Ubuntu server (20.04, 22.04, 24.04 or newer)
- Internet connectivity
- SSH access with a sudo-capable user
Step-by-Step: Install Nginx on Ubuntu
1. Update the Package Index
Always start fresh. This ensures you’re getting the latest version:
sudo apt update
sudo apt upgrade -y
The -y flag automatically confirms the installation, saves you a keystroke.
2. Install Nginx
The Ubuntu repositories include Nginx, so installation is simple:
sudo apt install nginx -y
This method is stable and recommended for most production servers.
3. Start and Enable Nginx
Ensure Nginx starts now and on boot:
sudo systemctl start nginx
sudo systemctl enable nginx
4. Verify the Installation
Check if Nginx is running:
sudo systemctl status nginx
You should see “active (running)” in green.
Or navigate to your server’s IP address in a browser:
http://your_server_ip
You should see the default Nginx welcome page. If you do, congratulations, you’ve successfully installed Nginx on Ubuntu.
Quick Configuration Tips
Your main configuration files live in /etc/nginx/. The default server block is at /etc/nginx/sites-available/default. For most setups, you’ll want to:
- Create separate server blocks for each domain
- Store your site files in /var/www/your_domain
Key locations:
/etc/nginx/nginx.conf– main config/etc/nginx/sites-available/– virtual host configs/etc/nginx/sites-enabled/– active sites
After any config change, always test before reloading:
sudo nginx -t
sudo systemctl reload nginx
Best Practices and Common Mistakes
- If UFW is enabled on your system, allow HTTP and HTTPS traffic:
sudo ufw allow 'Nginx Full'
- Avoid editing
nginx.confdirectly for site configs, use server blocks instead. - For production, disable default sites.
FAQ
Q: How do I restart Nginx after making configuration changes?
A: Use sudo systemctl restart nginx or sudo systemctl reload nginx if you just want to reload configs without dropping connections.
Q: Where are Nginx log files located?
A: Access logs are in /var/log/nginx/access.log and error logs in /var/log/nginx/error.log. These are invaluable for troubleshooting.
Q: Can I run Nginx alongside Apache?
A: Yes, but they can’t both listen on port 80. You’ll need to configure them on different ports or use Nginx as a reverse proxy for Apache.
Wrapping Up
That’s it, you’ve successfully learned how to install Nginx on Ubuntu and apply basic configuration safely. With Nginx installed, you’re ready to host websites, reverse proxies, or APIs with confidence. The next step is configuring your server blocks and pointing your domain to the server.