Getting Started with Nginx: Install, Configure a Site, and Everyday Commands
Install Nginx on your server with a few commands, set up your first site, and learn the day-to-day workflow of testing, reloading, and reading logs.
Nginx is one of the most widely used web servers and reverse proxies out there: lightweight, stable, and great under concurrency. This guide walks you through serving a static site from scratch on your own server or VPS.
Installing Nginx
On Ubuntu / Debian, use apt:
sudo apt update
sudo apt install -y nginx
On CentOS / Rocky, use dnf:
sudo dnf install -y nginx
Then start Nginx, enable it at boot, and confirm it's running:
sudo systemctl enable --now nginx
systemctl status nginx
Now open your server's public IP in a browser and you should see the default Nginx welcome page. If it doesn't load, check that your firewall and security group allow port 80 (add 443 later for HTTPS).
Configuring a Site
On Ubuntu / Debian, site configs live in /etc/nginx/sites-available/ and are symlinked into sites-enabled/ to take effect; the main nginx.conf pulls them in via include. Create /etc/nginx/sites-available/example:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
The key directives:
- listen — the port to listen on, usually 80 for HTTP.
- servername — the domain names to match; list several, or use as a catch-all.
- root — the site's root directory; request paths are appended to it.
- index — the default file served when none is named in the URL.
- location — rules matched by URL prefix.
Create the content directory and enable the site:
sudo mkdir -p /var/www/example
echo '<h1>Hello Nginx</h1>' | sudo tee /var/www/example/index.html
sudo ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/
CentOS / Rocky has no sites- layout — just drop the server block into /etc/nginx/conf.d/example.conf instead.
Testing and Reloading
Always test before you apply. Make this a habit:
sudo nginx -t
You want to see syntax is ok and test is successful. Then reload to apply the new config gracefully, without dropping existing connections:
sudo systemctl reload nginx
By contrast, restart bounces the process and briefly interrupts service, so prefer reload for routine config changes.
Reading the Logs
Logs default to /var/log/nginx/: access.log records incoming requests, error.log records failures. When something breaks, follow it live:
sudo tail -f /var/log/nginx/error.log
Summary
Install (apt install nginx) → write a server block with root / index / servername → test with nginx -t → apply with systemctl reload nginx. Once that path clicks, you've got the core Nginx workflow down. And when something goes wrong, check error.log first — the answer is usually right there.