Web Server: Nginx

01

Installation

bash
# Debian/Ubuntu
apt update && apt install nginx -y

# CentOS/AlmaLinux
dnf install nginx -y

Start and enable Nginx on boot

bash
systemctl start nginx
systemctl enable nginx
systemctl status nginx

Open ports in the firewall:

bash
ufw allow http
ufw allow https
02

Directory structure

PathDescription
/etc/nginx/nginx.confMain configuration
/etc/nginx/sites-available/Site configurations (Debian/Ubuntu)
/etc/nginx/sites-enabled/Active sites symlinks
/etc/nginx/conf.d/Additional configurations (CentOS)
/var/www/html/Default document root
/var/log/nginx/Access and error logs
03

Configure a Virtual Host

Create the configuration file for your domain:

bash
nano /etc/nginx/sites-available/example.com
nginx
server {
    listen 80;
    listen [::]:80;

    server_name example.com www.example.com;
    root /var/www/example.com;
    index index.html index.php;

    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;

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

Enable the site:

bash
ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Create the site directory:

bash
mkdir -p /var/www/example.com
echo "<h1>It works!</h1>" > /var/www/example.com/index.html
chown -R www-data:www-data /var/www/example.com
04

Verify and restart

bash
# Verify configuration syntax
nginx -t

# Reload without downtime
systemctl reload nginx

# Full restart
systemctl restart nginx
05

Useful commands

bash
# See error logs in real time
tail -f /var/log/nginx/error.log

# See access logs in real time
tail -f /var/log/nginx/access.log

# See all active sites
ls -la /etc/nginx/sites-enabled/
06

Configuration for PHP (PHP-FPM)

nginx
server {
    listen 80;
    server_name example.com;
    root /var/www/example.com;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

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

    location ~ /\.ht {
        deny all;
    }
}

See also the guide for SSL with Certbot.

DeluxHost, opgericht in 2023, biedt hoogwaardige hostingoplossingen voor diverse digitale behoeften. Wij bieden gedeelde hosting, VPS en dedicated servers met geavanceerde beveiliging en wereldwijde datacenters.

© DeluxHost, Alle rechten voorbehouden. | BTW-nummer: IT17734661006
Alle systemen operationeel