Beginner 12 min

Deploy a site with Nginx

Install Nginx, configure a vhost and upload your application or static site.

#Nginx#Web#Deploy

Ready to start your service? Check offers and order online.

View offers and order

Overview

Nginx is a powerful web server, ideal for hosting static sites, PHP-FPM applications, reverse proxy or APIs. This guide installs Nginx on Debian/Ubuntu and configures a demo site.

Installation

apt update apt install nginx -y systemctl enable nginx systemctl start nginx

Make sure Nginx answers: Open http://ADRESSE_IP in your browser. The default "Welcome to nginx" page must be displayed.

Firewall If UFW is active, allow HTTP/HTTPS: ufw allow 'Nginx Full'. See guide Configure a UFW firewall.

File structure

  • /var/www/, root of websites
  • /etc/nginx/sites-available/, configurations available
  • /etc/nginx/sites-enabled/, activated sites (symbolic links)
  • /var/log/nginx/, access and error logs

Create a static site

  1. Create site directory
    mkdir -p /var/www/monsite.fr/html chown -R www-data:www-data /var/www/monsite.fr
  2. Add Home Page
    echo '<!DOCTYPE html><html><head><title>Mon site</title></head> <body><h1>Bienvenue sur monsite.fr</h1></body></html>' \ > /var/www/monsite.fr/html/index.html
  3. Create the virtual host

    Create /etc/nginx/sites-available/monsite.fr :

server { listen 80; listen [::]:80; server_name monsite.fr www.monsite.fr; root /var/www/monsite.fr/html; index index.html index.htm; location / { try_files $uri $uri/ =404; } access_log /var/log/nginx/monsite.fr.access.log; error_log /var/log/nginx/monsite.fr.error.log; }
  1. Enable site
    ln -s /etc/nginx/sites-available/monsite.fr /etc/nginx/sites-enabled/ nginx -t systemctl reload nginx

    nginx -t validate the syntax before reloading. Never reload if the test fails.

Point your domain name

In your registry, create a A record pointing to the IPv4 of your VPS. DNS Propagation: a few minutes at 24 h. Test with:

dig +short monsite.fr

Enable HTTPS with Let's Encrypt

Install Certbot:

apt install certbot python3-certbot-nginx -y certbot --nginx -d monsite.fr -d www.monsite.fr

Follow the wizard (e-mail, conditions, HTTP redirect → HTTPS recommended). The certificate is automatically renewed via a timer systemd.

Renewal test:

certbot renew --dry-run

Hosting a PHP application (optional)

apt install php-fpm -y

Add in your block server :

location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.2-fpm.sock; }

Adapt PHP (php8.1-fpm, php8.2-fpm...) according to ls /run/php/.

Troubleshooting

  • 502 Bad Gateway, PHP-FPM stopped or bad socket in config.
  • 403 Forbidden, Incorrect Rights on /var/www/ or missing index.
  • Default site displayed, Check server_name and the link in sites-enabled.

Check the logs: tail -f /var/log/nginx/error.log

Next steps