Passer au contenu principal

Configuration Bookstak avec Caddy

Configuration DebianMachine 10virtuelle Alpine Linux Edge

Prérequis : 

Bon à savoir !
C'est une pile LCMP (Linux Caddy Mysql Php )

  • DebianAlpine 10Linux Edge
  • Caddy 2
  • MariaDB
  • PHP 7.4

Configuration de la VM :

  • 2 Vcpu
  • 1 Go de ram
  • 16 Go de disque

Editer la liste des sources

Il est ou le fichier ?
Emplacement: /etc/apk/repositiories
Nom du fichier: repositiories

# Utilisation du canal edge pour etre en rolling-release
http://dl-cdn.alpinelinux.org/alpine/edge/main
http://dl-cdn.alpinelinux.org/alpine/edge/community
http://dl-cdn.alpinelinux.org/alpine/edge/testing
Installez les prérequis
aptapk update && apt full-upgrade -ya
aptapk installadd -ymariadb-client gitmariadb unzipphp7 php7-fpm php7-json php7-phar php7-zlib php7-simplexml php7-dom php7-fileinfo php7-xmlwriter php7-xml php7-pdo_mysql php7-session php7-ctype php7-mbstring php7-mysqlnd php7-tokenizer php7-curl mariadb-serverphp7-zip php7.4php7-mcrypt php7.4-cliphp7-openssl php7.4-fpmphp7-pdo php7.4-php7-tidy php7.4-curl php7.4-ldap php7.4-mcrypt php7.4-php7-gd php7.4-xml php7.4-zip php7.4-mysql php7.4-mbstring php7.4-common
Supprimer le démarrage d'apache et Nginx 

Question ?
Pourquoi apache est il installé avec apt install php-7.4 ?
Pourquoi nginx est il installé avec apt install php-7.4-fpm ?
Je m'attend que apt installe JUSTE php7.4 ...

Désactiver apache2 et nginx au démarrage du serveur OU les supprimer du serveur ( attention aux dépendances !)

systemctl disable --now apache2
systemctl disable --now nginx

apt remove --purge apache2
apt remove --purge nginx
Création base de donnée pour bookstack
mysql -u root
CREATE DATABASE bookstackDB;
GRANT ALL ON bookstackDB.* TO 'bookstack' IDENTIFIED BY 'TheBookStackADM'BookStack';
FLUSH PRIVILEGES;

Configuration PHP

BonIl àest savoirou !le fichier ?
Emplacement: /etc/php/7.4/fpm/pool.php7/php-fpm.d/www.conf
Nom du fichier: www.conf

Modifier les élément suivant sinon Caddy affichera l'erreur suivante
[ERROR 502 /info.php] dial unix /var/run/php7.4-fpm.sock: connect: permission denied

; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions. The owner
; and group can be specified either by name or by their numeric IDs.
; Default Values: user and group are set as the running user
;                 mode is set to 0660
listen.owner = caddy
listen.group = caddy

Configuration serveur web Caddy2

Création utilisateur caddy
groupadd --system caddy

useradd --system \
	--gid caddy \
	--create-home \
	--home-dir /var/lib/caddy \
	--shell /usr/sbin/nologin \
	--comment "Caddy web server" \
	caddy
Fichier de démarrage Systemd

Bon à savoir !
Emplacement: /etc/systemd/system/
Nom du fichier: caddy.service

# caddy.service
# For using Caddy with a config file.

[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target

[Service]
User=caddy
Group=caddy
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

Activer le fichier de démarrage et lancer le service

systemctl# daemon-reloadVia systemctlOpenRC
enablerc-update caddyadd systemctlphp-fpm7 default
rc-service php-fpm7 start caddy
Fichier Caddyfile

Bon à savoir !
Emplacement: /etc/caddy/
Nom du fichier: Caddyfile

{
        # Activation protocole http3
        experimental_http3
}

# Nom de domaine a servir en HTTPS
Nom.De.Domaine.fr

# Envoie les notifications du fonctionnement du certificat Let's Encrypt à l'administrateur système
tls Sysadmin@mail.com

# Bloc log
# Garde les logs pendant 1 an conformément à la loi française
log {
        output file /var/log/caddy/access.json {
                roll_size 1gib
                roll_keep 5
                roll_keep_for 8760h
        }
}


# Definition du chemin racine Web
root * /var/www/bookstack/public
# Compression
encode zstd gzip
# Backend PHP 7.4
php_fastcgi unix//run/php/php7.4-fpm.php-fpm7.sock
# Fichier web à servir
file_server


Appliquez les bon droits aux fichiers web pour caddy

chown -R www-data:www-datacaddy:caddy /var/www/bookstack

 

Configuration BookStack

Installation fichier bookstack

Se positionner dans /var/www/

cd /var/www

Cloner les fichier source du projet bookstack

git clone https://github.com/BookStackApp/BookStack.git --branch release --single-branch

Renommer le fichier .env.example en .env

mv /var/www/bookstack/.env.example /var/www/bookstack/.env

Modifier le fichier .env

Emplacement: /var/www/bookstack
Nom du fichier: .env

Bon à savoir !
Modifier les parties suivantes
- APP_URL
- DB_HOST
- DB_DATABASE
- DB_USERNAME
- DB_PASSWORD

# Application key
# Used for encryption where needed.
# Run `php artisan key:generate` to generate a valid key.
APP_KEY=

# Application URL
# Remove the hash below and set a URL if using BookStack behind
# a proxy, if using a third-party authentication option.
# This must be the root URL that you want to host BookStack on.
# All URL's in BookStack will be generated using this value.
APP_URL=https://serveur.domaine.fr

# Database details
DB_HOST=localhost
DB_DATABASE=bookstack
DB_USERNAME=bookstack
DB_PASSWORD=LeFameuxMotDePasse

# Only send cookies over a HTTPS connection.
# Ensure you have BookStack served over HTTPS before enabling.
# Defaults to 'false'
SESSION_SECURE_COOKIE=true

# A full list of options can be found in the '.env.example.complete' file.
# Sets application language to French
APP_LANG=fr
Installer composer
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'e5325b19b381bfd88ce90a5ddb7823406b2a38cff6bb704b0acc289a09c8128d4a8ce2bbafcd1fcbdc38666422fe2806') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
mv composer.phar /usr/local/bin/composer

Installer les dépendances via composer

Bon à savoir
Exécutez les commandes suivantes dans /var/www/bookstack

cd /var/www/bookstack
composer install --no-dev
php artisan key:generate
php artisan migrate

Appliquez les bon droits aux fichiers web pour caddy

chown -R www-data:www-datacaddy:caddy /var/www/bookstack

Gestion service et log

Service Caddy

Lancer, redémarrer et arreter le service

systemctlrc-service caddy start
caddy
systemctl reloadrc-service caddy systemctl stop
rc-service caddy reload

Voir les logs

systemctlrc-service caddy status caddy
journalctl -u caddy