May 28 2023, 16:28#

Configuration Apache, PHP et Let's Encrypt sur FreeBSD

Petite note rapide sur la configuration d'un serveur httpd d'Apache avec PHP et Let's Encrypt pour chiffrer le flux (SSL/TLS).

Prérequis

pkg inst py39-certbot apache24 mod_php80 php80 php80-extensions

Certbot

Générer un certificat avec Certbot

  • example.com
  • www.example.com

Nécessite qu'un enregistrement DNS (A/alias) pointe vers le serveur pour chaque certificat

certbot certonly --standalone -d example.com,www.example.com

Les certificats sont enregistrés dans /usr/local/etc/letsencrypt/live

Configurer le renouvellement automatique des certificats

Le process de renouvellement ne renouvelle que lorsque c'est nécessaire. Donc il peut être exécuté une fois par jour sans problème.

Ajouter dans la crontab de root la ligne suivante pour lancer le process tous les jours à 02h50

50 2 * * * /usr/local/bin/certbot renew --pre-hook "service apache24 stop" --post-hook "service apache24 start"

Les options pre-hook et post-hook permettent de contrôler l'arrêt et le redémarrage du service Apache avant et après le process de renouvellement.

Apache

Création du répertoire des logs Apache

mkdir /var/log/apache
chmod 750 /var/log/apache

Edition de /usr/local/etc/apache24/httpd.conf

ServerName localhost:80
LoadModule rewrite_module libexec/apache24/mod_rewrite.so
LoadModule ssl_module libexec/apache24/mod_ssl.so
LoadModule socache_shmcb_module libexec/apache24/mod_socache_shmcb.so
Include etc/apache24/extra/httpd-vhosts.conf
ErrorLog "/var/log/apache/httpd-error.log"
CustomLog "/var/log/apache/httpd-access.log" common

Activation de PHP

Ajouter les lignes suivantes dans le fichier /usr/local/etc/apache24/modules.d/080_mod_php.conf

AddType application/x-httpd-php .php
DirectoryIndex index.php index.html

Copie du fichier de configuration php.ini de production

cp -a /usr/local/etc/php.ini-production /usr/local/etc/php.ini

Configuration SSL dans /usr/local/etc/apache24/modules.d/070_mod_ssl.conf

Listen 443
SSLCipherSuite HIGH:MEDIUM:!MD5:!RC4:!3DES
SSLProxyCipherSuite HIGH:MEDIUM:!MD5:!RC4:!3DES
SSLHonorCipherOrder on
SSLProtocol all -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
SSLProxyProtocol all -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
SSLPassPhraseDialog builtin
SSLSessionCache "shmcb:/var/run/ssl_scache(512000)"
SSLSessionCacheTimeout 300
SSLOptions +StrictRequire

Exemple de configuration d'un VirtualHost avec SSL dans /usr/local/etc/apache24/extra/httpd-vhosts.conf

# example.com
<VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com
        ServerAdmin postmaster@example.com

        ErrorLog "/var/log/apache/example-error_log"
        CustomLog "/var/log/apache/example-access_log" common

        RewriteEngine on
        RewriteCond %{SERVER_NAME} =www.example.com [OR]
        RewriteCond %{SERVER_NAME} =example.com
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

<IfModule mod_ssl.c>

# example.com
<VirtualHost *:443>
        ServerName example.com
        ServerAlias www.example.com
        ServerAdmin postmaster@example.com
        DocumentRoot "/var/www/example"

        # /
        <Directory "/var/www/example">
                Options Indexes FollowSymLinks
                AllowOverride All
                Require all granted
        </Directory>

        ErrorLog "/var/log/apache/example-error_log"
        CustomLog "/var/log/apache/example-access_log" common

        SSLEngine on
        SSLCertificateFile /usr/local/etc/letsencrypt/live/example.com/fullchain.pem
        SSLCertificateKeyFile /usr/local/etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>

</IfModule>

Activation du service

sysrc clear_tmp_enable=YES
sysrc apache24_enable=YES

La directive clear_tmp_enable permet de vider /tmp au reboot. Utile pour supprimer automatiquement les anciens fichiers de session PHP après un reboot.

Démarrage du service

service apache24 start

Test SSL : https://www.ssllabs.com/

Tags : unix freebsd ssl tls http web php