On this page
- What do you need before installing Nginx?
- How do you install Nginx on each distribution?
- How do you open the firewall for HTTP and HTTPS?
- Where does Nginx keep its configuration?
- How do you create a server block for your domain?
- How do you add HTTPS with Certbot?
- Which settings are worth tuning?
- How do you read Nginx logs and fix common errors?
- How do you keep Nginx updated?
- Questions
What do you need before installing Nginx?
You need a running server, a sudo user and a domain whose A and AAAA records point at the server. Log in over SSH as described in your first SSH session, and create the DNS records shown in the DNS records tutorial so that example.net resolves to 198.51.100.24 and 2001:db8:4f2a::24. Certificates can only be issued once the name resolves, so set DNS first and let it settle while you install.
Nginx itself is light. A static site or a small proxy runs comfortably on 1 GB of memory, and the process count grows with the vCPUs you give it. Bring the package index up to date before you add anything:
# Ubuntu and Debian
sudo apt update && sudo apt upgrade -y
# Rocky Linux and AlmaLinux
sudo dnf upgrade -yHow do you install Nginx on each distribution?
The distribution package is the simplest route and gets security fixes through normal updates. On Ubuntu 24.04, 22.04 and Debian 12 or 13 the package is called nginx:
sudo apt install -y nginx
sudo systemctl enable --now nginx
nginx -vRocky Linux 9 and AlmaLinux 9 ship Nginx in AppStream. The default stream is older, and newer branches are offered as module streams, so list them and pick the newest before installing:
sudo dnf module list nginx
sudo dnf module enable -y nginx:1.24
sudo dnf install -y nginx
sudo systemctl enable --now nginx
nginx -vIf you need the very latest mainline release, the Nginx project publishes its own repositories for all four systems at nginx.org. Most servers do not need that; the packaged versions receive backported fixes.
How do you open the firewall for HTTP and HTTPS?
Allow ports 80 and 443 inside the operating system, because the firewall on a BitcoinVPS server lives in the OS rather than in the console. On Ubuntu and Debian with UFW, the Nginx package registers an application profile:
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo ufw statusOn Rocky and Alma, firewalld is active by default:
sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-servicesNow load http://198.51.100.24/ in a browser or run curl -I http://198.51.100.24/. A 200 OK with a Server: nginx header proves the service is listening. More detail on both tools is in the UFW guide and the firewalld guide.
Where does Nginx keep its configuration?
The main file is /etc/nginx/nginx.conf on every system, but site files live in different places. Debian and Ubuntu use /etc/nginx/sites-available/ for definitions and symlinks in /etc/nginx/sites-enabled/ to switch them on. Rocky and Alma read every *.conf file in /etc/nginx/conf.d/, so a site is active as soon as its file exists there.
/etc/nginx/nginx.conf: global settings such as workers, logging and includes./etc/nginx/snippets/(Debian family): small reusable pieces./var/log/nginx/:access.loganderror.log./usr/share/nginx/htmlor/var/www/html: the default welcome page.
Keep one file per site and name it after the domain. That makes it obvious which file to edit later and lets you disable one site without touching the others.
How do you create a server block for your domain?
Make a document root owned by your deploy user, drop in a test page, then write a server block that names the domain. Start with the files:
sudo mkdir -p /var/www/example.net/public
sudo chown -R deploy:deploy /var/www/example.net
echo '<h1>example.net works</h1>' > /var/www/example.net/public/index.htmlCreate /etc/nginx/sites-available/example.net on Debian or Ubuntu, or /etc/nginx/conf.d/example.net.conf on Rocky and Alma:
server {
listen 80;
listen [::]:80;
server_name example.net www.example.net;
root /var/www/example.net/public;
index index.html;
access_log /var/log/nginx/example.net.access.log;
error_log /var/log/nginx/example.net.error.log;
location / {
try_files $uri $uri/ =404;
}
}On the Debian family, link it and remove the default site so it does not catch requests for your name:
sudo ln -s /etc/nginx/sites-available/example.net /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo nginx -t && sudo systemctl reload nginxOn Rocky and Alma, SELinux expects web content under /var/www to carry the right label. Run sudo restorecon -Rv /var/www/example.net after creating files, and read the SELinux basics guide if you serve from another path.
How do you add HTTPS with Certbot?
Add Certbot and its Nginx plugin, then let the plugin rewrite the server block. The plugin finds the block by server_name, requests a certificate over HTTP and adds the TLS directives plus a redirect.
# Ubuntu and Debian
sudo apt install -y certbot python3-certbot-nginx
# Rocky and Alma (Certbot comes from EPEL)
sudo dnf install -y epel-release
sudo dnf install -y certbot python3-certbot-nginx
sudo certbot --nginx -d example.net -d www.example.netCertbot installs a systemd timer that renews certificates before they expire. Prove that renewal works without waiting:
sudo certbot renew --dry-run
systemctl list-timers | grep -i certbotConfirm it with curl -I https://example.net/. The Certbot guide covers wildcard names and DNS challenges, and HTTPS and www redirects explains how to pick one canonical host name.
Which settings are worth tuning?
A handful of directives in nginx.conf cover most of the gains. Leave worker_processes auto; so Nginx starts one worker per vCPU, and raise connections per worker if you expect many idle keep-alive clients:
worker_processes auto;
events {
worker_connections 2048;
}
http {
server_tokens off;
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_max_body_size 20m;
open_file_cache max=2000 inactive=30s;
}server_tokens off hides the exact version in headers and error pages. client_max_body_size limits uploads; raise it for apps that accept large files. Compression is a separate topic covered in gzip and Brotli. Every change should be followed by sudo nginx -t and a reload, never a blind restart, so a typo never takes the site down.
How do you read Nginx logs and fix common errors?
Start with the error log for the site, then the global one. sudo tail -f /var/log/nginx/example.net.error.log streams new lines while you reload the page, and sudo journalctl -u nginx --since "10 min ago" shows failures to start.
- Address already in use: another program holds port 80 or 443. Find it with
sudo ss -ltnp | grep -E ':80|:443'. - 403 Forbidden: the worker user (
www-dataon Debian,nginxon Rocky) cannot read the files, or there is no index file. On Rocky and Alma also check SELinux labels. - 404 for every path: the request matched a different server block. Check
server_nameand remove leftover default sites. - 502 Bad Gateway: a proxied app or PHP-FPM is down. See the reverse proxy guide.
Log files are rotated daily by logrotate, which the package configures for you.
How do you keep Nginx updated?
Nginx updates with the rest of the system, so regular sudo apt upgrade or sudo dnf upgrade runs are enough. The package reloads workers gracefully, keeping open connections alive. After any upgrade, confirm with nginx -v and systemctl status nginx. If you outgrow the plan, deploy a larger one and move the files and configuration across, as explained in moving to a larger plan.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Install and configure Nginx
Should I use the distro package or the nginx.org repository?
The distro package is fine for most sites and gets security fixes through normal updates. Use the nginx.org repository only if you need a feature from a newer release.
How do I host several domains on one server?
Create one server block per domain, each with its own server_name and document root. Nginx picks the block whose name matches the Host header of the request.
Why does nginx -t fail after my edit?
It prints the file and line number of the problem. Missing semicolons and unmatched braces are the usual causes. Fix the line and run the test again before reloading.
Does Certbot renew certificates on its own?
Yes. The package adds a systemd timer that checks twice a day and renews certificates that are close to expiry. sudo certbot renew --dry-run confirms it will work.
How much memory does Nginx need?
Very little for static files: a few megabytes per worker. The app or database behind it usually decides the plan size, not Nginx.
Can I run Nginx and Apache together?
Only if they listen on different ports, for example Nginx on 80 and 443 proxying to Apache on 127.0.0.1:8080. Two servers cannot bind the same port.
More in Web servers and proxies
- Install and configure Apache
- OpenLiteSpeed web server
- HAProxy load balancer and TLS proxy
- Load balancing with Nginx upstreams
- Nginx as a reverse proxy for an app
- Apache as a reverse proxy
- Compression with gzip and Brotli
- HTTPS and www redirects
- Turn off Apache directory listings
- Nginx Proxy Manager with Docker
- Nginx UI web interface
- Traefik for Docker containers
- Apache Tomcat for Java apps
- Encrypted FTP with vsftpd (FTPS)
- Varnish cache in front of a site