Formation Evolix

Services HTTP

Apache

Serveur HTTP le plus utilisé, depuis 1996

Installation


# apt install apache2-mpm-itk \
              libapache2-mod-evasive apachetop libwww-perl
    

Fichiers de configuration

/etc/apache2/
├── apache2.conf
├── conf-available
│   └── *.conf
├── conf-enabled
│   └── *.conf -> ../conf-available/*.conf
├── envvars
├── magic
├── mods-available
│   ├── *.conf
│   └── *.load
├── mods-enabled
│   ├── *.conf -> ../mods-available/*.conf
│   └── *.load -> ../mods-available/*.load
├── ports.conf
├── sites-available
│   └── *.conf
└── sites-enabled
    └── *.conf -> ../sites-available/*.conf
    

Des modules à la carte


      # a2enmod rewrite expires headers rewrite cgi
    

Optimisations "Evolix"


      ServerTokens Prod
      Timeout 10
      KeepAliveTimeout 2
      MaxKeepAliveRequests 10
      ServerLimit 250
      #MaxClients 250
      MaxRequestWorkers 250
      StartServers 50
      MinSpareServers 20
      MaxSpareServers 30
      MaxRequestsPerChild 100
      
          AllowOverride None
          Require all granted
      
      
      SSLProtocol all -SSLv2 -SSLv3
      SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5:!RC4
      
    

Forcer le umask

# grep umask /etc/apache2/envvars
umask 007

VirtualHost

  • Permet de séparer de plusieurs sites sur un même serveur HTTP
  • Séparation par nom de domaine ou par adresses IP

VirtualHost basé sur un nom de domaine


<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com

    DocumentRoot /home/example/www/
    <Directory /home/example/www/>
        Options SymLinksIfOwnerMatch
        AllowOverride AuthConfig Limit FileInfo Indexes
    </Directory>

    AssignUserID www-example example
    MaxClientsVHost 150

    CustomLog /var/log/apache2/access.log vhost_combined
    CustomLog /home/example/log/access.log combined
    ErrorLog  /home/example/log/error.log

    RewriteEngine On
    UseCanonicalName On
    RewriteCond %{HTTP_HOST} !^www.example.com$
    RewriteRule ^/(.*) http://%{SERVER_NAME}/$1 [L,R]
</VirtualHost>
    

Activation du site


# adduser example
# adduser --ingroup example www-example
# mkdir /home/example/{www,log,awstats}
# chown example: /home/example/{www,log,awstats}
# a2ensite example
    

Gestion des droits avec Apache-ITK

  • user/group distincts pour chaque VirtualHost
  • séparation stricte des droits
  • … y compris la lecture
  • … y compris avec PHP

# chmod 750 /home/example
      

Restriction en écriture pour Apache

  • Lecture seule pour Apache, sauf certains répertoires (uploads/, cache/)
  • utilisateur dédié pour l'écriture par Apache : www-example
  • … et pour FTP/SSH/SFTP/SCP/RSYNC : example
  • groupe commun pour tous et droits accordés au groupe
  • pas besoin de chmod, Apache a son umask à 007

# echo "umask 027" >> /etc/profile
# find /home/example -type f -user www-example -exec chmod 660 {} \;
# find /home/example -type d -user www-example -exec chmod 770 {} \;
      

Attention !

Ne jamais forcer les droits
récursivement sur toute l’arborescence.

Si la restriction en écriture pour Apache est impossible :

  • plus d’utilisateur distinct
  • "AssignUserID example example" élimine 100% des soucis de droits
  • … mais réduction de la sécurité

HTTPS ‑ TLS/SSL

Activation du module SSL pour Apache


# a2enmod ssl
  

Création d'un certificat


# openssl req -newkey rsa:2048 -sha256 -nodes \
    -keyout private.key -out demande.csr
# openssl x509 -req -days 3650 -sha256 -in demande.csr \
    -signkey private.key -out certificate.crt
# mv private.key /etc/ssl/private/
# chown root:ssl-cert /etc/ssl/private/private.key
# chmod 640 /etc/ssl/private/private.key
# mv certificate.crt /etc/ssl/certs
# chown root:root /etc/ssl/certs/certificate.crt
# chmod 644 /etc/ssl/certs/certificate.crt
  

Modification du VirtualHost


<VirtualHost *:80 *:443>
    ServerName secure.example.com
    ServerAlias www.example.com example.com

    SSLEngine on
    SSLProtocol all -SSLv2 -SSLv3
    SSLCertificateKeyFile /etc/ssl/private/private.key
    SSLCertificateFile /etc/ssl/certs/certificate.crt
    # SSLCertificateChainFile /etc/ssl/certs/certificates_chain.pem

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [L,R=permanent]
</VirtualHost>
    

logs

Apache propose plusieurs formats de logs

CustomLog log/global_access.log vhost_combined
CustomLog log/access.log combined

SetEnvIf User-Agent "Foo" dontlog
CustomLog log/access.log combined env=!dontlog

ErrorLog  log/error.log

Authentification "HTTP Basic"

Le module mod_auth_basic est disponible par défaut.

    AuthType Basic
    AuthName "Restricted"
    AuthUserFile /foo/.htpasswd
    AuthGroupFile /dev/null
    require valid-user
    

Règles de ré-écriture


RedirectPermanent / http://new.example.com

RedirectMatch ^/(.*)$ http://new.example.com/$1

# GET / --> /sub/
RedirectMatch ^/$ /sub/

RewriteRule ^/(.*) http://new.example.com/$1 [L,R=permanent]

RewriteCond %{REQUEST_URI} !^/foo.txt
RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [L,R]

# le drapeau NC pour ne pas tenir compte de la casse
RewriteRule ^/FoO.tXt /sub/ [L,R,NC]

# empêcher des requêtes POST sur une URL particulière
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^/foo.txt [L,F]

mod_evasive

Limite les accès, notamment les dénis de service


<IfModule mod_evasive20.c>
    DOSHashTableSize    3097
    DOSPageCount        5
    DOSSiteCount        30
    DOSPageInterval     3
    DOSSiteInterval     1
    DOSBlockingPeriod   60
    DOSEmailNotify      security@example.com
</IfModule>

Fail2ban

  • recherche de comportements anormaux dans les logs
  • blocage temporaire ou permanent, via firewall

Apachetop

Affiche en direct des stats sur le traffic, d'après les logs.

  $ apachetop -f access.log -T 3600 -q
  

mod_status

Génère une page web résumant l'état d'Apache.

<IfModule mod_status.c>
    ExtendedStatus On
    <Location /server-status-XXXX>
        SetHandler server-status
        Deny from all
        Include ipaddr_whitelist.conf
        Allow from 192.0.2.43
        Allow from 127.0.0.1
    </Location>
</IfModule>
    

Nginx

Serveur web, alternative à Apache

Installation


  # aptitude install nginx
  # nginx -v
  
nginx version: nginx/1.10.3
  
https://wiki.evolix.org/HowtoNginx

HAProxy

Proxy et load-balancer TCP/HTTP/HTTPS

Installation


  # apt install haproxy
  # varnishd -v
  
HA-Proxy version 1.7.5-2 2017/05/17
Copyright 2000-2017 Willy Tarreau <willy@haproxy.org>
  

Configuration minimale


global
      log 127.0.0.1 local5 debug

defaults
      mode     http

listen www
      bind *:80
      balance roundrobin
      option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www.example.com
      stats uri /haproxy-stats
      stats auth foo:bar
      server www00 192.0.2.1:80 maxconn 50 check inter 10s
      server www01 192.0.2.2:80 maxconn 50 check inter 10s
      

Configuration avancée

  • gestion fine des performances
  • multiples IP/domaines écoutés
  • différents algorithmes de load-balancing

TLS/SSL

  • terminaison SSL ou transmission transparente
  • multi certificats et SNI
  • agrafage OCSP facilitée

mode TCP


listen memcached 127.0.0.1:11211
    option tcp-check
    server nosql00 192.0.2.3:11211 check
    server nosql01 192.0.2.4:11211 check backup
    

mode MySQL (simple)


listen mysql 127.0.0.1:3306
    mode tcp
    option mysql-check user haproxy_check
    server sql00 192.0.2.1:3306 check
    

mode MySQL (avancé)

Si le test de connexion à MySQL ne suffit pas,
on indique un programme pour un test personnalisé
qui indiquera à HAProxy si le backend va bien.

Dashboard

Une interface web permet de suivre l'état du proxy.

Varnish

Accélérateur web : cache et reverse-proxy

Installation


# apt install varnish
# varnishd -V

varnishd (varnish-4.0.2 revision bfe7cd1)
Copyright (c) 2006 Verdens Gang AS
Copyright (c) 2006-2014 Varnish Software AS

Configuration minimale

Varnish relaie les requêtes vers le port 8080 local.


backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

Logs

Par défaut les logs sont gardés en mémoire (taille fixe) pour les performances.

# varnishstat
# varnishtop -i ReqURL
# varnishlog
# varnishnsca
Filtres possibles

# varnishlog -q 'TxHeader eq MISS' -q "ReqHeader \
             ~ '^Host: example\.com$'" | grep RxURL
# varnishncsa -q "ReqHeader eq 'X-Cache: MISS'"

Syntaxe VCL

PHP

Langage de programmation très adapté au web.

https://wiki.evolix.org/HowtoLAMP/PHP

Let's Encrypt

  • Autorité de certification
  • propose des certificats X-509
  • DV – Domain Validation

Principes de base

  • gratuit
  • automatique
  • sécurisé
  • transparent
  • ouvert
  • cooperatif

Composants

  • Protocole ACME (standard IETF en janvier 2018)
  • Boulder : serveur ACME
  • Certbot : client ACME pour gérer des certificats
  • des dizaines d'autres clients tiers

Installation de certbot


  # apt install certbot
  
  • création du compte Let's Encrypt
  • création de certificats
  • auto-configuration du serveur web
  • renouvellement/révocation de certificats

3 Challenges pour la validation

  • HTTP – ressource signée
  • DNS – enregistrement DNS signé
  • TLS-SNI – certificat TLS contenant une signature

Particularités

  • Durée de vie de 90 jours
  • Entièrement automatisé
  • … donc seulement DV et pas OV ou EV