Conteneur LXC : Installation de Let's Encrypt

1/ Modifier la zone DNS du nom de domaine 'hacklab.run' pour créer le sous-domaine 'nekrofage.hacklab.run'.

Ajouter l'enregistrement DNS :

nekrofage                 IN A      146.59.154.144

2/ Connexion au serveur OVH.

util01@station66:~$ ssh ubuntu@146.59.154.144
ubuntu@146.59.154.144's password: 
Welcome to Ubuntu 21.10 (GNU/Linux 5.13.0-28-generic x86_64)
...
Last login: Fri Feb 18 19:39:04 2022 from 90.6.103.190
ubuntu@vps-c49a29aa:~$ s

3/ Création du conteneur 'container4' qui va héberger un site web.

ubuntu@vps-c49a29aa:~$ sudo lxc launch ubuntu:20.04 container4
Creating container4
Starting container4

4/ Liste des conteneurs.

ubuntu@vps-c49a29aa:~$ sudo lxc list
+------------+---------+-----------------------+-----------------------------------------------+-----------+-----------+
|    NAME    |  STATE  |         IPV4          |                     IPV6                      |   TYPE    | SNAPSHOTS |
+------------+---------+-----------------------+-----------------------------------------------+-----------+-----------+
| container1 | RUNNING | 10.168.240.200 (eth0) | fd42:9eda:b555:ed18:216:3eff:fe3c:6b14 (eth0) | CONTAINER | 0         |
+------------+---------+-----------------------+-----------------------------------------------+-----------+-----------+
| container2 | RUNNING | 10.168.240.198 (eth0) | fd42:9eda:b555:ed18:216:3eff:feff:cd0a (eth0) | CONTAINER | 0         |
+------------+---------+-----------------------+-----------------------------------------------+-----------+-----------+
| container3 | RUNNING | 10.168.240.36 (eth0)  | fd42:9eda:b555:ed18:216:3eff:feaf:37a (eth0)  | CONTAINER | 0         |
+------------+---------+-----------------------+-----------------------------------------------+-----------+-----------+
| container4 | RUNNING | 10.168.240.86 (eth0)  | fd42:9eda:b555:ed18:216:3eff:fefa:6e7a (eth0) | CONTAINER | 0         |
+------------+---------+-----------------------+-----------------------------------------------+-----------+-----------+
| proxy      | RUNNING | 10.168.240.247 (eth0) | fd42:9eda:b555:ed18:216:3eff:fe43:c198 (eth0) | CONTAINER | 0         |
+------------+---------+-----------------------+-----------------------------------------------+-----------+-----------+

5/ Connexion au conteneur 'container4'.

ubuntu@vps-c49a29aa:~$ lxc exec container4 -- sudo --user ubuntu --login
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

ubuntu@container4:~$ 

6/ Installation des paquets de base.

ubuntu@container4:~$ sudo apt install -y vim screen htop links

7/ Installation de Apache2.

ubuntu@container4:~$ sudo apt install -y apache2

8/ Configuration de Apache2.

Ouvrir :

/etc/apache2/conf-available/remoteip.conf

Ajouter :

RemoteIPHeader X-Real-IP
RemoteIPTrustedProxy 10.168.240.247 fd42:9eda:b555:ed18:216:3eff:fe43:c198

10.168.240.247 fd42:9eda:b555:ed18:216:3eff:fe43:c198 : Correspond à l'adresse ipv4 et ipv6 du conteneur proxy reverse

9/ Installation du module 'remoteip'.

ubuntu@container4:~$ sudo a2enmod remoteip
Enabling module remoteip.
To activate the new configuration, you need to run:
  systemctl restart apache2

10/ Activation la configuration.

ubuntu@container4:~$ sudo a2enconf remoteip
Enabling conf remoteip.
To activate the new configuration, you need to run:
  systemctl reload apache2

11/ Modification de la page par défaut :

Ouvrir :

/var/www/html/index.html

Chercher :

        <div class="section_header section_header_red">
          <div id="about"></div>
          It works!
        </div>

Remplacer par :

        <div class="section_header section_header_red">
          <div id="about"></div>
          Nekrofage
        </div>

12/ Rechargement de Apache :

ubuntu@container4:~$ sudo systemctl reload apache2

13/ Sortir du conteneur

ubuntu@container4:~$ exit
logout
ubuntu@vps-c49a29aa:~$ 

14/ Configuration de Nginx comme proxy reverse du conteneur 'proxy'.

ubuntu@vps-c49a29aa:~$ lxc exec proxy -- sudo --user ubuntu --login
ubuntu@proxy:~$ 

15/ Ajout de la configuration pour le nom de domaine 'nekrofage.hacklab.run' vers conteneur 'container4'.

Ouvrir :

/etc/nginx/sites-available/nekrofage.hacklab.run

Ajouter :

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

        server_name nekrofage.hacklab.run;

        location / {
                include /etc/nginx/proxy_params;

                proxy_pass http://container4.lxd;
        }

        real_ip_header proxy_protocol;
        set_real_ip_from 127.0.0.1;
}

16/ Activation de la configuration.

ubuntu@vps-c49a29aa:~$ sudo ln -s /etc/nginx/sites-available/nekrofage.hacklab.run /etc/nginx/sites-enabled/

17/ Rechargement de Nginx.

ubuntu@proxy:~$ sudo systemctl reload nginx

18/ Sortir du conteneur 'proxy'.

ubuntu@proxy:~$ logout
ubuntu@vps-c49a29aa:~$ 

19/ Test l'adresse internet :

http://nekrofage.hacklab.run

20/ Connexion au conteneur 'proxy'.

ubuntu@vps-c49a29aa:~$ lxc exec proxy -- sudo --user ubuntu --login
ubuntu@proxy:~$ 

21/ Installation de 'certbot'.

ubuntu@proxy:~$ sudo apt-get install python3-certbot-nginx

22/ Configuration.

ubuntu@proxy:~$ sudo certbot --nginx
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): lesanglierdesardennes@gmail.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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: N

Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: container1.hacklab.run
2: container2.hacklab.run
3: nekrofage.hacklab.run
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 3
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for nekrofage.hacklab.run
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/nekrofage.hacklab.run

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 all traffic on port 80 to ssl in /etc/nginx/sites-enabled/nekrofage.hacklab.run

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://nekrofage.hacklab.run

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=nekrofage.hacklab.run
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/nekrofage.hacklab.run/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/nekrofage.hacklab.run/privkey.pem
   Your cert will expire on 2022-05-20. 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

23/ Vérification du renouvellement automatique.

ubuntu@proxy:~$ sudo certbot renew --dry-run
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/nekrofage.hacklab.run.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator nginx, Installer nginx
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for nekrofage.hacklab.run
Waiting for verification...
Cleaning up challenges

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed with reload of nginx server; fullchain is
/etc/letsencrypt/live/nekrofage.hacklab.run/fullchain.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates below have not been saved.)

Congratulations, all renewals succeeded. The following certs have been renewed:
  /etc/letsencrypt/live/nekrofage.hacklab.run/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates above have not been saved.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - 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.

24/ Configuration pour le proxy.

Ouvrir :

/etc/nginx/sites-enabled/nekrofage.hacklab.run 

Chercher :

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot

Remplacer par :

    listen [::]:443 ssl proxy_protocol ipv6only=on; # managed by Certbot
    listen 443 ssl proxy_protocol; # managed by Certbot

25/ Redémarrer Nginx.

ubuntu@proxy:~$ sudo systemctl restart nginx

26/ Test :

https://nekrofage.hacklab.run/