Nginx + PHP (dernière version) + Certbot

Installe Nginx, la dernière version de PHP via le dépôt sury.org et Certbot pour les certificats SSL Let's Encrypt.

nginxphpcertbotsslweb
$ curl scripts.ysavary.fr/nginx-php-certbot | bash

Nginx + PHP + Certbot

Stack web complète : Nginx en reverse proxy/serveur web, PHP-FPM via le dépôt sury.org (toujours la version la plus récente), et Certbot pour Let's Encrypt.

Ce que fait le script :

bash
#!/bin/bash
set -euo pipefail

RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'
info()  { echo -e "${GREEN}[INFO]${NC}  $*"; }
warn()  { echo -e "${YELLOW}[WARN]${NC}  $*"; }
error() { echo -e "${RED}[ERR]${NC}   $*"; exit 1; }

[[ $EUID -ne 0 ]] && error "Ce script doit être exécuté en root."

export DEBIAN_FRONTEND=noninteractive
OS=$(grep -oP '(?<=^VERSION_CODENAME=).+' /etc/os-release 2>/dev/null || echo "bookworm")

# ── Mise à jour ───────────────────────────────────────────────────────────────
info "=== Étape 1/5 — Mise à jour du système ==="
apt-get update -y && apt-get upgrade -y

# ── Nginx ────────────────────────────────────────────────────────────────────
info "=== Étape 2/5 — Installation de Nginx ==="
apt-get install -y nginx
systemctl enable nginx

# ── Dépôt PHP sury.org ───────────────────────────────────────────────────────
info "=== Étape 3/5 — Ajout du dépôt PHP (sury.org) ==="
apt-get install -y apt-transport-https ca-certificates curl gnupg2

curl -fsSL https://packages.sury.org/php/apt.gpg \
    | gpg --dearmor -o /usr/share/keyrings/sury-php.gpg

echo "deb [signed-by=/usr/share/keyrings/sury-php.gpg] https://packages.sury.org/php/ $OS main" \
    > /etc/apt/sources.list.d/sury-php.list

apt-get update -y

PHP_VER=$(apt-cache search '^php[0-9]\.[0-9]-fpm$' \
    | awk '{print $1}' | grep -oP '[0-9]+\.[0-9]+' | sort -V | tail -1)

[[ -z "$PHP_VER" ]] && error "Impossible de détecter la version PHP disponible."
info "Dernière version PHP détectée : $PHP_VER"

# ── PHP + extensions ─────────────────────────────────────────────────────────
info "=== Étape 4/5 — Installation de PHP $PHP_VER ==="
apt-get install -y \
    php${PHP_VER} \
    php${PHP_VER}-fpm \
    php${PHP_VER}-cli \
    php${PHP_VER}-common \
    php${PHP_VER}-mysql \
    php${PHP_VER}-pgsql \
    php${PHP_VER}-curl \
    php${PHP_VER}-mbstring \
    php${PHP_VER}-xml \
    php${PHP_VER}-zip \
    php${PHP_VER}-gd \
    php${PHP_VER}-intl \
    php${PHP_VER}-bcmath

# Certbot
apt-get install -y certbot python3-certbot-nginx

# ── Config PHP ───────────────────────────────────────────────────────────────
PHP_INI="/etc/php/${PHP_VER}/fpm/php.ini"
sed -i 's/^upload_max_filesize.*/upload_max_filesize = 64M/'  "$PHP_INI"
sed -i 's/^post_max_size.*/post_max_size = 64M/'              "$PHP_INI"
sed -i 's/^memory_limit.*/memory_limit = 256M/'               "$PHP_INI"
sed -i 's/^max_execution_time.*/max_execution_time = 60/'     "$PHP_INI"

# ── Config Nginx default ──────────────────────────────────────────────────────
info "=== Étape 5/5 — Configuration Nginx ==="
cat > /etc/nginx/sites-available/default <<EOF
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    root /var/www/html;
    index index.php index.html;

    access_log /var/log/nginx/access.log;
    error_log  /var/log/nginx/error.log;

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt  { log_not_found off; access_log off; }
    location ~ /\.          { deny all; }

    location / {
        try_files \$uri \$uri/ /index.php?\$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php${PHP_VER}-fpm.sock;
        fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
        include fastcgi_params;
    }
}
EOF

nginx -t
systemctl restart nginx
systemctl restart php${PHP_VER}-fpm

echo ""
info "=== Installation terminée ==="
echo ""
printf "  %-12s %s\n" "Nginx :"   "$(nginx -v 2>&1)"
printf "  %-12s %s\n" "PHP :"     "$(php -v | head -1)"
printf "  %-12s %s\n" "Certbot :" "$(certbot --version 2>&1)"
echo ""
echo "  Config PHP    : /etc/php/$PHP_VER/fpm/php.ini"
echo "  Sites Nginx   : /etc/nginx/sites-available/"
echo "  Web root      : /var/www/html/"
echo ""
echo "  ── Obtenir un certificat SSL ──────────────────────────"
echo "  certbot --nginx -d mondomaine.fr"
echo "  certbot --nginx -d mondomaine.fr -d www.mondomaine.fr"
echo ""