Description de l'infrastructure

  • La FreeBox (192.168.1.1) dispose d'une adresse IP publique fixe.
  • Votre site personnel "foo.mondomaine.fr" dispose d'un enregistrement DNS valide.
  • Le reverse-proxy haproxy est installé sur une Alpine Linux 3.14 (192.168.1.20)
  • Le site web "foo.mondomaine.fr" est hébergé sur un serveur Apache sur une autre machine (192.168.1.25)

Installation sur la machine Alpine Linux

Haproxy

  • apk update
  • apk add haproxy
  • Il faut créer un dossier pour les futurs certificats ssl : mkdir /etc/haproxy/certs
  • Voici le fichier /etc/haproxy/haproxy.conf que j'utilise :
global
  log 127.0.0.1 local2
  chroot /var/lib/haproxy
  pidfile /var/run/haproxy.pid
  maxconn 4000
  user haproxy
  group haproxy
  daemon
  stats socket /var/lib/haproxy/stats
  # SSL
  ca-base /etc/ssl/certs
  crt-base /etc/ssl/private
  tune.ssl.default-dh-param 2048
  ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:ECDH+AES128:DH+AES:RSA+AES:!aNULL:!MD5:!DSS
  ssl-default-bind-options no-sslv3

defaults
  mode http
  log global
  option dontlognull
  option http-server-close
  option redispatch
  retries 3
  timeout http-request 10s
  timeout queue 1m
  timeout connect 10s
  timeout client 1m
  timeout server 1m
  timeout http-keep-alive 10s
  timeout check 10s
  maxconn 3000

#---------------------------------------------------------------------
# HTTP et HTTPS
#---------------------------------------------------------------------

frontend http
  mode http
  option httplog
  option forwardfor except 127.0.0.0/8
  bind *:80
  # LETSENCRYPT
  acl acl_letsencrypt path -i -m beg /.well-known/
  http-request redirect scheme https unless { ssl_fc }
  use_backend http_letsencrypt if acl_letsencrypt

frontend https
  mode http
  option httplog
  option forwardfor except 127.0.0.0/8
  bind *:443 ssl crt /etc/haproxy/certs/
  acl acl_foo hdr(host) -i foo.mondomaine.fr
  use_backend http_foo if acl_foo

backend http_foo
  mode http
  server s21 192.168.1.25:80

backend http_letsencrypt
  mode http
  server s01 127.0.0.1:8888

#---------------------------------------------------------------------
# SSH
#---------------------------------------------------------------------

frontend ssh_2222
  mode tcp
  bind *:2222
  default_backend ssh_foo

backend ssh_foo
  mode tcp
  server s21 192.168.1.25:22
  • service haproxy restart
  • Pour relancer haproxy au reboot : rc-update add haproxy

Lighttpd

  • PRINCIPE :
    • J'utilise un serveur  web pour que letsencrypt puisse valider la création du certificat SSL (il existe d'autres méthodes, mais elle sont moins simples à utiliser)
    • Dans haproxy, la ligne "acl_letsencrypt" recherche si l'URL contient par ".well-known". Dans ce cas, il y a redirection vers le serveur lighttpd, sur le port 8888.
  • Dans le fichier /etc/apk/repositories, activez tous les dépots.
  • apk update
  • apk add lighttpd
  • Dans /etc/lighttpd/lighttppd.conf, activez la ligne "server.port = 8888"
  • Il faut créer un dossier pour le controle par letsencrypt : mkdir /var/www/localhost/htdocs/.well-known
  • service lighttpd restart
  • rc-update add lighttpd

Freebox

  • Ouvrir les ports :
    • 80 ==> 192.168.1.20:80
    • 443 ==> 192.168.1.20:80
    • 2222 ==> 192.168.1.25:22
  • INFORMATION SUR LE SERVICE SSH :
    • A partir du web, le service SSH est disponible avec l'URL foo.mondomaine.net sur le port 2222 (accès direct sans passer par haproxy)
    • En local foo.mondomaine.net pointe sur le serveur Alpine (192.168.1.20). En utilisant le port 2222, haproxy redirige vers le serveur 192.168.1.25

Certbot

  • apk install certbot
  • Pour créer le certificat :
    certbot certonly --webroot --webroot-path /var/www/localhost/htdocs/ --domain foo.mondomaine.net --email prenom.nom@gmail.com --non-interactive
  • Assembler et déplacer le fichier pem :
    cat /etc/letsencrypt/live/foo.mondomaine.net/fullchain.pem /etc/letsencrypt/live/foo.mondomaine.net/privkey.pem|tee /etc/haproxy/certs/foo.mondomaine.pem
  • Dans une tâche cron, réaliser la mise à jour du certificat :
    certbot renew