How to Install Nginx on Ubuntu Server (and Host Your First Website)

Nginx (say "engine-x") is the web server behind a huge share of the internet. On a home server it does two jobs: serving your own pages, and — more often — sitting in front of all your other apps as a reverse proxy so they share one address and one HTTPS certificate. This tutorial covers the install and a first site; the next two add HTTPS and the proxy.
Quick reference
| Task | Command / location |
|---|---|
| Install | sudo apt install nginx -y |
| Open the firewall | sudo ufw allow 'Nginx Full' |
| Default website files | /var/www/html/ |
| Site configs | /etc/nginx/sites-available/ (enabled via links in sites-enabled/) |
| Check config for errors | sudo nginx -t |
| Apply changes | sudo systemctl reload nginx |
| Error log | /var/log/nginx/error.log |
1. Install and open the port
sudo apt install nginx -y
sudo ufw allow 'Nginx Full'
Nginx starts automatically. Browse to http://your-server-ip and you'll see the Welcome to nginx! page.
2. Put your own page there
sudo nano /var/www/html/index.html
Replace everything with:
<h1>Hello from my server</h1>
Refresh the browser. That's a website. Drop any HTML, CSS, images or a static site generator's output into /var/www/html and Nginx serves it.
To edit as yourself rather than with sudo every time:
sudo chown -R $USER:$USER /var/www/html
3. Add a site with its own name
One Nginx can serve many sites, each in a server block. Make a folder and a config:
sudo mkdir -p /var/www/example.com
sudo chown -R $USER:$USER /var/www/example.com
echo "<h1>example.com works</h1>" > /var/www/example.com/index.html
sudo nano /etc/nginx/sites-available/example.com
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Enable it by linking it into sites-enabled, test, reload:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Always run nginx -t before reloading. It catches typos so you don't take every site down with one bad file.
4. Point a name at it
On your home network, add a line to your computer's hosts file (C:\Windows\System32\drivers\etc\hosts on Windows, /etc/hosts on Mac/Linux):
192.168.1.50 example.com
For a real domain, create an A record at your domain registrar pointing to your public IP, and forward port 80 (and 443) on your router to the server. The next tutorial adds HTTPS, which you'll want before going public.
Useful bits
- Disable a site: sudo rm /etc/nginx/sites-enabled/example.com then reload. The config stays in sites-available.
- Password-protect a folder: sudo apt install apache2-utils, sudo htpasswd -c /etc/nginx/.htpasswd john, then in the location block add auth_basic "Private"; auth_basic_user_file /etc/nginx/.htpasswd;.
- Watch requests arrive: sudo tail -f /var/log/nginx/access.log.
Troubleshooting
Still the Welcome page. Hard-refresh the browser (Ctrl + F5), or the default site is winning: sudo rm /etc/nginx/sites-enabled/default and reload.
403 Forbidden. Nginx can't read the folder. sudo chmod -R 755 /var/www/example.com and make sure there's an index.html.
"nginx -t" fails with "unexpected end of file". A missing } or ;. The error gives the line number.
Port 80 already in use. Apache may be installed: sudo systemctl disable --now apache2.
Can't reach it from another device. Firewall: sudo ufw status should list Nginx. Then curl localhost on the server to prove Nginx itself is fine.
Wrapping up
Install, allow through the firewall, put files in /var/www, one server block per site, nginx -t then reload. From here it's a short step to a free HTTPS certificate and to putting Nginx in front of every other app on the server.
