NginxWeb ServerHTTP7 min read

Install and configure Nginx

Install and configure Nginx as a web server with server blocks to host sites on your VPS.


Nginx is the most popular web server for modern applications. Ideal for serving static sites, as a reverse proxy or load balancer.

Step 1 — Install Nginx

bash
sudo apt update
sudo apt install nginx -y

Step 2 — Verify it's running

bash
sudo systemctl status nginx

Open your browser and visit http://YOUR_VPS_IP. You should see the Nginx welcome page.

Step 3 — Allow HTTP/HTTPS traffic in the firewall

bash
sudo ufw allow 'Nginx Full'

Step 4 — Create a server block (virtual host)

Create the directory for your site:

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

Create a test page:

bash
echo '<h1>Site active on Baires Host</h1>' > /var/www/mydomain.com/html/index.html

Create the server block configuration:

bash
sudo nano /etc/nginx/sites-available/mydomain.com

Content:

nginx
server {
    listen 80;
    listen [::]:80;
    server_name mydomain.com www.mydomain.com;
    root /var/www/mydomain.com/html;
    index index.html;

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

Step 5 — Enable the site

bash
sudo ln -s /etc/nginx/sites-available/mydomain.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Step 6 — Management commands

bash
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx   # Reload config without downtime

Next steps

Configure SSL with Let's Encrypt to enable HTTPS. See the guide "Configure SSL with Let's Encrypt".


Was this guide helpful?