Exemple d’utilisation de notre tout nouveau Raspi

Nous allons installer un serveur Web, apache.

Ce n’est qu’un exemple : choisissez le logiciel que vous voulez, mais gardez à l’esprit la sécurisation.

Installation du programme

Installation

pi@piras:~ $ sudo apt install apache2
Lecture des listes de paquets... Fait
Construction de l'arbre des dépendances       
Lecture des informations d'état... Fait
Les paquets supplémentaires suivants seront installés : 
apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libbrotli1 liblua5.2-0
Paquets suggérés :
apache2-doc apache2-suexec-pristine | apache2-suexec-custom
Les NOUVEAUX paquets suivants seront installés :
apache2 apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libbrotli1
liblua5.2-0
0 mis à jour, 10 nouvellement installés, 0 à enlever et 0 non mis à jour.
Il est nécessaire de prendre 2 309 ko dans les archives.
Après cette opération, 7 253 ko d'espace disque supplémentaires seront utilisés.
Souhaitez-vous continuer ? [O/n] o
...
Enabling site 000-default.
Created symlink /etc/systemd/system/multi-user.target.wants/apache2.service → /lib/systemd/system/apache2.service.
Created symlink /etc/systemd/system/multi-user.target.wants/apache-htcacheclean.service → /lib/systemd/system/apache-htcacheclean.
service.
Traitement des actions différées (« triggers ») pour systemd (241-7~deb10u6+rpi1) ...
Traitement des actions différées (« triggers ») pour man-db (2.8.5-2) ...
Traitement des actions différées (« triggers ») pour libc-bin (2.28-10+rpi1) ...
pi@piras:~ $ 

Ouverture des ports

Il nous faut ajouter les services http et https.

  1. Règles en cours

     pi@piras:~ $ sudo firewall-cmd --state
     running
     pi@piras:~ $ sudo firewall-cmd --get-default-zone
     public
     pi@piras:~ $ sudo firewall-cmd --get-active-zones
     public
     interfaces: eth0
     pi@piras:~ $ sudo firewall-cmd --list-all
     public (active)
     target: default
     icmp-block-inversion: no
     interfaces: eth0
     sources: 
     services: dhcpv6-client smtp ssh
     ports: 
     protocols: 
     masquerade: no
     forward-ports: 
     source-ports: 
     icmp-blocks: 
     rich rules: 
  2. Ajout des services

     pi@piras:~ $ sudo firewall-cmd --permanent --zone=public --add-service={http,https}
     success
     pi@piras:~ $
  3. Test de nos services actifs

    J’ai relancé le raspi car j’avais des erreurs de zones.

    Après redémarrage, tout est bon :

     pi@piras:~ $ sudo firewall-cmd --list-all
     public (active)
     target: default
     icmp-block-inversion: no
     interfaces: eth0
     sources: 
     services: dhcpv6-client http https smtp ssh
     ports: 
     protocols: 
     masquerade: no
     forward-ports: 
     source-ports: 
     icmp-blocks: 
     rich rules: 
    
     pi@piras:~ $ 

    Nos services http et https ont bien été pris en compte.

Test

Nous allons faire un test de connexion :

Page d’accueil de Debian

Tout fonctionne comme il faut.

Création de la page d’accueil

La page d’accueil

Nous allons créer un petit fichier .html pour personnaliser notre page d’acceuil. Le fichier s’appellera index.html et est situé dans /var/www/html/.

pi@piras:~ $ cd /var/www/html/
pi@piras:/var/www/html $ ls
index.html
pi@piras:/var/www/html $ 

Avec votre éditeur favori, supprimez le fichier :

pi@piras:/var/www/html $ sudo rm index.html 
pi@piras:/var/www/html $

et créez un nouveau fichier dont le contenu est le suivant :

<!DOCTYPE html>
<html>
    <head>
        <title>Page d'accueil du Piras</title>
        <meta charset="UTF-8">
    </head>
    <body>
        <h1>Page d'acceuil du Piras</h1>
        <p>Vous voilà sur la page principale de notre tout nouveau serveur Web.</p>
        <p>Merci de votre visite!</p>
    </body>
</html>

pi@piras:/var/www/html $ sudo vim index.html
pi@piras:/var/www/html $

Changez le propriétaire du fichier :

pi@piras:/var/www/html $ sudo chown www-data: index.html
pi@piras:/var/www/html $

Création du virtualhost correspondant

pi@piras:/etc/apache2/sites-available $ cat piras.conf
<VirtualHost *:80>

    ServerName piras.yojik.net
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
            
</VirtualHost>
pi@piras:/etc/apache2/sites-available $

On désactive les anciens sites par défaut, et on active le nouveau :

pi@piras:/etc/apache2/sites-available $ sudo a2dissite default-ssl
Site default-ssl disabled.
To activate the new configuration, you need to run:
systemctl reload apache2
pi@piras:/etc/apache2/sites-available $ sudo a2dissite 000-default
Site 000-default disabled.
To activate the new configuration, you need to run:
systemctl reload apache2
pi@piras:/etc/apache2/sites-available $ 

pi@piras:/etc/apache2/sites-available $ sudo a2ensite piras
Enabling site piras.
To activate the new configuration, you need to run:
systemctl reload apache2
pi@piras:/etc/apache2/sites-available $

On recharge apache2 pour qu’il prenne en compte nos modifications :

pi@piras:/etc/apache2/sites-available $ sudo systemctl reload apache2
pi@piras:/etc/apache2/sites-available $

Test de notre page d’accueil

Page d’accueil du Piras

Et voilà! :D

Sécurisation de notre serveur

État des lieux

Rendez-vous sur la page suivante : Test Mozilla. Nos résultats ne sont pas terribles …

Résultat 1
Résultat 2
Résultat 3

Nous allons suivre les recommandations de la fondation Mozilla en ce qui concerne les algorithmes utilisés pour le chiffrement.

Page de configuration d’un serveur Apache par la fondation Mozilla : Configurateur

Nous alons donc éditer notre fichier piras.conf situé dans /etc/apache2/sites-available/ ainsi que le fichier général de configuration d’apache, situé dans pi@piras:/etc/apache2, apache2.conf :

  1. Sauvegarde des fichiers existants

    pi@piras:/etc/apache2 $ sudo cp apache2.conf apache2.conf.orig pi@piras:/etc/apache2 $

  2. Création de certificats let’encrypt

    Il existe plusieurs clients letsencrypt pour la gestion des certificats. Nous utiliserons certbot, le client officiel de letsencrypt.

     pi@piras:~ $ sudo apt install certbot python-certbot-apache
     ...
     pi@piras:~ $

    Nous stoppons le service apache

     pi@piras:~ $ sudo systemctl stop apache2
     pi@piras:~ $

    Lançons certbot

     pi@piras:~ $ sudo certbot
     Saving debug log to /var/log/letsencrypt/letsencrypt.log
     Plugins selected: Authenticator apache, Installer apache
     Enter email address (used for urgent renewal and security notices) (Enter 'c' to
     cancel): eric@yojik.eu
    
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     Please read the Terms of Service at
     https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
     agree in order to register with the ACME server at
     https://acme-v02.api.letsencrypt.org/directory
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     (A)gree/(C)ancel: A
    
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     Would you be willing to share your email address with the Electronic Frontier
     Foundation, a founding partner of the Let's Encrypt project and the non-profit
     organization that develops Certbot? We'd like to send you email about our work
     encrypting the web, EFF news, campaigns, and ways to support digital freedom.
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     (Y)es/(N)o: Y
    
     Which names would you like to activate HTTPS for?
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     1: piras.yojik.net
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     Select the appropriate numbers separated by commas and/or spaces, or leave input
     blank to select all options shown (Enter 'c' to cancel): 1
     Obtaining a new certificate
     Performing the following challenges:
     http-01 challenge for piras.yojik.net
     Waiting for verification...
     Cleaning up challenges
     Created an SSL vhost at /etc/apache2/sites-available/piras-le-ssl.conf
     Deploying Certificate to VirtualHost /etc/apache2/sites-available/piras-le-ssl.conf
     Enabling available site: /etc/apache2/sites-available/piras-le-ssl.conf
    
     Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     1: No redirect - Make no further changes to the webserver configuration.
     2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
     new sites, or if you're confident your site works on HTTPS. You can undo this
     change by editing your web server's configuration.
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
     Redirecting vhost in /etc/apache2/sites-enabled/piras.conf to ssl vhost in /etc/apache2/sites-available/piras-le-ssl.conf
    
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     Congratulations! You have successfully enabled https://piras.yojik.net
    
     You should test your configuration at:
     https://www.ssllabs.com/ssltest/analyze.html?d=piras.yojik.net
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    
     IMPORTANT NOTES:
     - Congratulations! Your certificate and chain have been saved at:
     /etc/letsencrypt/live/piras.yojik.net/fullchain.pem
     Your key file has been saved at:
     /etc/letsencrypt/live/piras.yojik.net/privkey.pem
     Your cert will expire on 2021-06-05. To obtain a new or tweaked
     version of this certificate in the future, simply run certbot again
     with the "certonly" option. To non-interactively renew *all* of
     your certificates, run "certbot renew"
     - Your account credentials have been saved in your Certbot
     configuration directory at /etc/letsencrypt. You should make a
     secure backup of this folder now. This configuration directory will
     also contain certificates and private keys obtained by Certbot so
     making regular backups of this folder is ideal.
     - If you like Certbot, please consider supporting our work by:
    
     Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
     Donating to EFF:                    https://eff.org/donate-le
    
     pi@piras:~ $    

    Relançons apache

     pi@piras:~ $ sudo systemctl start apache2
     pi@piras:~ $
  3. Premier test

    Test avec SSL

    C’est bon.

  4. Sécurisation du serveur apache

    Voilà le contenu du fichier après modifications :

     pi@piras:~ $ cat /etc/apache2/sites-available/piras-le-ssl.conf
    
     <IfModule mod_ssl.c>
     <VirtualHost *:443>
         Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
         Header set Content-Security-Policy "default-src 'self';"
    
         <IfModule mod_headers.c>
                 Header set X-Content-Type-Options "nosniff"
         </IfModule> 
    
         Header set X-Frame-Options: "SAMEORIGIN"
    
         ServerName piras.yojik.net
         ServerAdmin webmaster@localhost
         DocumentRoot /var/www/html
    
         ErrorLog ${APACHE_LOG_DIR}/error.log
             CustomLog ${APACHE_LOG_DIR}/access.log combined
    
    
     SSLCertificateFile /etc/letsencrypt/live/piras.yojik.net/fullchain.pem
     SSLCertificateKeyFile /etc/letsencrypt/live/piras.yojik.net/privkey.pem
     Include /etc/letsencrypt/options-ssl-apache.conf
     </VirtualHost>
     </IfModule>
    
     pi@piras:~ $

Test de notre nouvelle configuration

Cette fois-ci, nous obtenons la note maximale A+ :

Test avec SSL
Test avec SSL
Test avec SSL

Nous sommes passés d’une note F à A+!

Petites améliorations supplémentaires

Voilà pour quelques améliorations de la sécurité de notre serveur Web.