Servers & VPS

How to Install and Configure Nginx on a Linux VPS

Step-by-step guide to installing Nginx on an Ubuntu or Debian VPS, setting up your first virtual host, and preparing it for production traffic.

Detailed view of a server rack with a focus on technology and data storage.

To install Nginx on an Ubuntu or Debian VPS, run sudo apt install nginx — the server starts immediately. This guide takes you from that first command to a fully configured virtual host ready for real production traffic.

Why Nginx Over Apache?

Nginx was designed to solve the C10K problem: handling thousands of simultaneous connections without spawning a new process for each one. Its event-driven architecture keeps memory usage low even under heavy load.

  • Performance: static files served at disk speed; reverse proxy with minimal overhead.
  • Memory: one Nginx worker uses ~5 MB, versus ~20-30 MB for an Apache prefork worker.
  • Versatility: works as a web server, reverse proxy, load balancer, and HTTP cache.
  • Adoption: the most widely used web server on high-traffic sites worldwide.

If your VPS has 1-2 GB of RAM and you expect traffic spikes, Nginx is the natural choice. To explore how to size your server properly, check out our VPS servers guide.

Prerequisites

  • VPS running Ubuntu 22.04 / 24.04 or Debian 11 / 12 (these steps apply to both).
  • SSH access with a sudo user (avoid working directly as root).
  • Ports 80 and 443 open in your provider's firewall and in UFW/iptables.
  • A domain name pointing to your VPS IP (needed for a named server block).

Installing Nginx Step by Step

1. Update repositories and install the package

sudo apt update
sudo apt install nginx -y

Once complete, Nginx starts automatically. Check its status:

sudo systemctl status nginx

You should see active (running) highlighted in green.

2. Open ports in UFW

If you use UFW (Ubuntu's default firewall), enable the Nginx profile:

sudo ufw allow 'Nginx Full'
sudo ufw status

Nginx Full opens both port 80 (HTTP) and port 443 (HTTPS). If you don't have a certificate yet, use Nginx HTTP temporarily.

3. Confirm the default page loads

Open http://<your-VPS-IP> in a browser. You should see Nginx's welcome page with the text "Welcome to nginx!".

Configuring a Virtual Host (Server Block)

Nginx organizes sites into server blocks stored in /etc/nginx/sites-available/. Each file describes one domain.

Create the site directory

sudo mkdir -p /var/www/yoursite.com/html
sudo chown -R $USER:$USER /var/www/yoursite.com/html

Write the server block

sudo nano /etc/nginx/sites-available/yoursite.com

Paste the following base block:

server {
    listen 80;
    listen [::]:80;

    server_name yoursite.com www.yoursite.com;
    root /var/www/yoursite.com/html;
    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ =404;
    }

    access_log /var/log/nginx/yoursite.com.access.log;
    error_log  /var/log/nginx/yoursite.com.error.log;
}

Enable the site and reload Nginx

sudo ln -s /etc/nginx/sites-available/yoursite.com /etc/nginx/sites-enabled/
sudo nginx -t          # validate the configuration
sudo systemctl reload nginx

The nginx -t command is critical: never reload without validating first.

Adding HTTPS with Let's Encrypt (Certbot)

A site without HTTPS should never go to production. Certbot automates obtaining and renewing free certificates.

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yoursite.com -d www.yoursite.com

Certbot will update your server block to redirect HTTP → HTTPS and add the certificate paths. Automatic renewal is already configured via a systemd timer.

Test renewal with:

sudo certbot renew --dry-run
Directive File Recommended value Effect
worker_processes nginx.conf auto One worker per CPU core
worker_connections nginx.conf 1024 Connections per worker
keepalive_timeout nginx.conf 65 TCP connection reuse
gzip on nginx.conf Enabled Compress text responses
server_tokens off nginx.conf off Hide Nginx version (security)

Edit /etc/nginx/nginx.conf inside the http { } block to apply these values, then run sudo nginx -t && sudo systemctl reload nginx.

Key Takeaways

  • Nginx installs with a single APT command and starts immediately on Ubuntu and Debian.
  • Server blocks in /etc/nginx/sites-available/ are the per-domain configuration unit.
  • Always validate with nginx -t before reloading to avoid outages.
  • Certbot + Let's Encrypt provides free HTTPS with automatic renewal.
  • Setting worker_processes auto and gzip on is enough for most mid-range VPS workloads.

Ready to take your VPS setup further? The team at elenlace.com can help you configure, optimize, and harden your server so your project scales without downtime.

FAQ

Does Nginx replace Apache, or can they coexist?

They can coexist on different ports, but only one can listen on port 80/443 at a time. On a VPS it's most common to pick one as your primary server. Using Nginx as a reverse proxy in front of Apache is valid but adds unnecessary complexity if you're starting from scratch.

How do I restart Nginx without dropping traffic?

Use sudo systemctl reload nginx instead of restart. A reload applies the new configuration without closing existing connections, avoiding any downtime.

Can I run PHP with Nginx?

Yes, but Nginx doesn't process PHP on its own — you need PHP-FPM. Install php-fpm and add a location ~ \.php$ block in your server block that passes requests to FPM's Unix socket. This is the most efficient combination for WordPress and PHP applications on a VPS.

Where are Nginx error logs?

The global error log lives at /var/log/nginx/error.log. If you define error_log in your server block, you get a per-site log, which makes troubleshooting much easier when you host multiple domains on the same VPS.

Prefer it done for you? El Enlace handles hosting and professional web development.

Further reading

Other providers and guides worth comparing:

← All