GNU/Linux >> Tutoriels Linux >  >> Ubuntu

Installer le serveur web Nginx sur Ubuntu 20.04

Nginx est un serveur web et reverse proxy haute performance, utilisé par de nombreux sites en production. Léger, stable et efficace pour servir du contenu statique comme dynamique. Ce guide installe Nginx sur Ubuntu 20.04 LTS, configure le pare-feu et crée un bloc serveur pour un domaine.

Installer Nginx

Actualisez l'index des paquets puis installez Nginx.

sudo apt update
sudo apt install nginx
Installer le serveur web Nginx sur Ubuntu 20.04

Démarrez et vérifiez le service.

sudo systemctl start nginx
sudo systemctl status nginx
Installer le serveur web Nginx sur Ubuntu 20.04

Contrôlez la version installée.

dpkg -l nginx
Installer le serveur web Nginx sur Ubuntu 20.04

Configurer le pare-feu

Si UFW est actif, autorisez le profil Nginx adapté. Pour du trafic HTTP simple, utilisez « Nginx HTTP ».

sudo ufw allow 'Nginx HTTP'
sudo ufw reload
sudo ufw status
Installer le serveur web Nginx sur Ubuntu 20.04

Tester la page par défaut

Ouvrez l'IP ou le domaine de votre serveur.

https://server-ip

La page d'accueil par défaut de Nginx doit s'afficher.

Installer le serveur web Nginx sur Ubuntu 20.04

Gérer le service Nginx

Arrêter Nginx.

sudo systemctl stop nginx

Démarrer Nginx.

sudo systemctl start nginx

Activer Nginx au démarrage.

sudo systemctl enable nginx

Redémarrer après une modification de configuration.

sudo systemctl restart nginx

Recharger sans interrompre les connexions en cours.

sudo systemctl reload nginx

Créer un bloc serveur

Créez le répertoire du contenu du site.

sudo mkdir -p /var/www/crazytechgeek.info/html
sudo chown -R $USER:$USER /var/www/crazytechgeek.info/html
sudo chmod -R 755 /var/www/crazytechgeek.info

Créez un fichier d'index de test.

sudo vim /var/www/crazytechgeek.info/html/index.html

Ajoutez du HTML simple, puis créez la configuration du bloc serveur.

sudo vim /etc/nginx/sites-available/crazytechgeek.info

Utilisez une configuration de ce type.

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

    root /var/www/crazytechgeek.info/html;
    index index.html index.htm;

    server_name crazytechgeek.info www.crazytechgeek.info;

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

Activez le bloc serveur et testez la configuration.

sudo ln -s /etc/nginx/sites-available/crazytechgeek.info /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
Installer le serveur web Nginx sur Ubuntu 20.04

Visitez votre domaine pour confirmer que la page personnalisée est bien servie.

https://crazytechgeek.info
Installer le serveur web Nginx sur Ubuntu 20.04

Fichiers Nginx importants

Configuration principale : /etc/nginx/nginx.conf

Blocs serveur disponibles : /etc/nginx/sites-available/

Blocs serveur activés : /etc/nginx/sites-enabled/

Journal d'accès : /var/log/nginx/access.log

Journal d'erreurs : /var/log/nginx/error.log

Nginx est installé et prêt à servir un ou plusieurs sites sur Ubuntu 20.04. En production, ajoutez un certificat Let's Encrypt avec Certbot (sudo certbot --nginx) pour activer HTTPS et la redirection automatique.

Ubuntu
  1. Installer le serveur web Nginx sur Ubuntu 18.04

  2. Installer et configurer NGINX sur Ubuntu 20.04

  3. Installer Nginx sur Ubuntu 20.04

  4. Installer LEMP sur Ubuntu 16.04

  5. Nginx sur Ubuntu 18.04 : installation, blocs serveur et fichiers clés

Installer Nginx sur Ubuntu 15.04

Hébergement web sécurisé avec Hiawatha : guide complet d'installation pour Ubuntu 16.04

Installer Nginx sur Ubuntu 18.04 LTS

Installer Nginx sur Ubuntu 14.04

Installer Nginx sur Ubuntu 20.04

Comment installer et optimiser le serveur web Lighttpd sur Ubuntu 18.04