---
title: Serveur Web Nginx
source: https://synapx.fr/blog/serveur-web-nginx/
date: 2026-06-26
category: Serveur Web
site: SynapxLab
---

# Serveur Web Nginx

```
+------------------+
|  Utilisateurs    |
+---------+--------+
          |
          v                            
+------------------+
|  Nginx + PHP     |
+---------+--------+
```

## Installation

```bash
sudo apt install nginx -y
sudo apt install php8.2 php8.2-fpm php8.2-mysql php8.2-xml php8.2-mbstring php8.2-curl -y

sudo nginx -t
sudo systemctl status nginx
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl stop nginx
sudo systemctl restart nginx

tail -f /var/log/nginx/error.log
tail -f /var/log/nginx/access.log
```

Installer le module Nginx dans Webmin :

Allez dans « Webmin Configuration / Webmin Modules / Installer un module tiers » puis indiquez l'URL du module.

## Configurer son nom de domaine

```bash
sudo mkdir -p /var/www/nomdedomaine.fr
sudo chown -R www-data:www-data /var/www/nomdedomaine.fr
sudo chmod -R 755 /var/www/nomdedomaine.fr
```

Créer le fichier de configuration du site :

```bash
sudo nano /etc/nginx/sites-available/nomdedomaine.fr
```

```nginx
server {
    listen 80;
    server_name nomdedomaine.fr www.nomdedomaine.fr;

    server_tokens off;

    root /var/www/nomdedomaine.fr;
    index index.html index.htm index.php;

    access_log /var/log/nginx/nomdedomaine.fr.access.log;
    error_log /var/log/nginx/nomdedomaine.fr.error.log;


    add_header Cache-Control "public";
    add_header X-Frame-Options "SAMEORIGIN";    # Autorise l'affichage uniquement depuis le même domaine


    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;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

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

Activer le site et tester :

```bash
sudo ln -s /etc/nginx/sites-available/nomdedomaine.fr /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

echo "<?php echo('Bonjour nomdedomaine.fr');?>" | sudo tee /var/www/nomdedomaine.fr/index.php
echo "<?php phpinfo();?>" | sudo tee /var/www/nomdedomaine.fr/info.php

curl -I http://www.nomdedomaine.fr/
curl -I http://nomdedomaine.fr/info.php
```

## Certificat SSL avec Certbot

```bash
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d nomdedomaine.fr -d www.nomdedomaine.fr
```

Certbot installe le certificat et met à jour le fichier :

```bash
cat /etc/nginx/sites-available/nomdedomaine.fr
```

```nginx
server {
    server_name nomdedomaine.fr www.nomdedomaine.fr;

    root /var/www/nomdedomaine.fr;
    index index.html index.htm index.php;

    access_log /var/log/nginx/nomdedomaine.fr.access.log;
    error_log /var/log/nginx/nomdedomaine.fr.error.log;

    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;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/nomdedomaine.fr/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/nomdedomaine.fr/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
    if ($host = www.nomdedomaine.fr) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = nomdedomaine.fr) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    server_name nomdedomaine.fr www.nomdedomaine.fr;
    return 404; # managed by Certbot
}
```

Quelques commandes utiles de gestion des certificats :

```bash
sudo certbot certonly --nginx -d nomdedomaine.fr -d www.nomdedomaine.fr
sudo tail -f /var/log/letsencrypt/letsencrypt.log
sudo certbot certificates
sudo certbot delete --cert-name nomdedomaine.fr
sudo certbot renew
sudo certbot install --cert-name nomdedomaine.fr -d nomdedomaine.fr -d www.nomdedomaine.fr
```

## Suppression de Nginx

```bash
sudo systemctl stop nginx
sudo apt-get purge nginx nginx-common nginx-full -y
sudo rm -rf /etc/nginx
sudo rm -rf /var/log/nginx
sudo rm -rf /var/www/html
sudo apt-get autoremove -y
sudo apt-get autoclean
sudo systemctl status nginx
```
