
Installazione e configurazione di Apache+ PHP+MariaDB in Debian
LAMP è uno stack per lo sviluppo web. Debian 13 "Trixie" include PHP 8.4 e MariaDB 11.8. Tutti i comandi vengono eseguiti come root o con sudo.
Preparazione Preliminare
Se hai un sistema pulito, puoi saltare questa sezione e procedere all'installazione di Apache.
Se hai già componenti web installati, esegui i seguenti comandi per sicurezza:
Creazione del backup delle configurazioni esistenti
<code>mkdir -p /backup; dirs="" [ -d /etc/apache2 ] && dirs+="/etc/apache2 " [ -d /etc/mysql ] && dirs+="/etc/mysql " [ -d /var/www ] && dirs+="/var/www " [ -n "$dirs" ] && tar -czf /backup/lamp_$(date +%F).tar.gz $dirs </code>
Aggiornamento del sistema e controllo porte
<code>apt update && apt full-upgrade -y ss -tuln | grep -E ':(80|443)\s' || echo "Le porte 80/443 sono libere" systemctl is-active nginx >/dev/null && { systemctl stop nginx; systemctl disable nginx; } </code>
Questi comandi:
- Creano un backup solo delle configurazioni esistenti
- Verificano la disponibilità delle porte 80/443
- Fermano Nginx se installato (per evitare conflitti con Apache)
Installazione di Apache
<code>apt install apache2 -y systemctl enable apache2 --now a2enmod rewrite ssl headers expires deflate systemctl reload apache2 </code>
Test: Apri l'IP del server nel browser:
<code>hostname -I | awk '{print $1}' </code>
Configurazione del Firewall
<code>apt install ufw -y SSH_PORT=$(ss -tuln | grep -w ssh | grep -oP '0.0.0.0:\K[0-9]+|:::\K[0-9]+' | head -1) ufw allow ${SSH_PORT:-22} ufw allow 80,443/tcp ufw --force enable </code>
Virtual Host
Creazione directory e pagina di test:
<code>mkdir -p /var/www/helloworld.net && chown -R www-data:www-data /var/www/helloworld.net && chmod -R 755 /var/www/helloworld.net cat > /var/www/helloworld.net/index.html << 'EOF' <!DOCTYPE html><html lang="it"><head><meta charset="UTF-8"><title>Ciao Mondo</title></head><body><h1>Ciao Mondo - Debian 13</h1></body></html> EOF </code>
Configurazione:
Creare /etc/apache2/sites-available/helloworld.net.conf
:
<code><VirtualHost *:80> ServerName helloworld.net ServerAlias www.helloworld.net DocumentRoot /var/www/helloworld.net ErrorLog ${APACHE_LOG_DIR}/helloworld.net.error.log CustomLog ${APACHE_LOG_DIR}/helloworld.net.access.log combined <Directory /var/www/helloworld.net> Options +FollowSymLinks -Indexes AllowOverride All Require all granted </Directory> Header set X-Content-Type-Options nosniff Header set X-Frame-Options DENY </VirtualHost> </code>
Attivazione:
<code>a2dissite 000-default.conf 2>/dev/null a2ensite helloworld.net.conf apache2ctl configtest && systemctl reload apache2 </code>
Test: Visita http://helloworld.net
Installazione di PHP 8.4
<code>apt install php8.4 php8.4-fpm php8.4-mysql php8.4-cli php8.4-common php8.4-curl php8.4-gd php8.4-mbstring php8.4-xml php8.4-zip -y a2enmod proxy_fcgi setenvif a2enconf php8.4-fpm systemctl enable php8.4-fpm --now systemctl reload apache2 </code>
Test PHP
Creare il file /var/www/helloworld.net/test.php
:
<code><?php echo "<h1>PHP ".phpversion()."</h1><p>Server: ".php_uname()."</p><p>Ora: ".date('Y-m-d H:i:s')."</p><h3>Estensioni:</h3><ul>"; foreach(['mysqli','curl','gd','mbstring','xml'] as $ext) echo "<li>$ext: ".(extension_loaded($ext)?"✅":"❌")."</li>"; echo "</ul>"; ?> </code>
Test: Visita http://helloworld.net/test.php
Installazione di MariaDB
<code>apt install mariadb-server mariadb-client -y systemctl enable mariadb --now mysql_secure_installation </code>
MariaDB 11.8 utilizza unix_socket per root di default. Risposte consigliate nella procedura guidata:
- N — non cambiare password di root
- Y — rimuovere utenti anonimi
- Y — proibire accesso remoto di root
- Y — rimuovere database test
- Y — ricaricare tabelle privilegi
Test di Connessione al Database
<code>WEBAPP_PASSWORD="secure_webapp_$(date +%s)_$(openssl rand -hex 6)" mysql -e "CREATE DATABASE test_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; CREATE USER 'webapp'@'localhost' IDENTIFIED BY '$WEBAPP_PASSWORD'; GRANT ALL ON test_db.* TO 'webapp'@'localhost'; FLUSH PRIVILEGES;" echo "$WEBAPP_PASSWORD" > /root/.webapp_password chmod 600 /root/.webapp_password </code>
Creare config.php
:
<code><?php $webapp_password = trim(file_get_contents('/root/.webapp_password')); $conn = new mysqli('localhost', 'webapp', $webapp_password, 'test_db'); ?> </code>
Creare db_test.php
per la verifica:
<code><?php require_once 'config.php'; if($conn->connect_error) die("Errore DB: {$conn->connect_error}"); echo "<h2>✅ MariaDB</h2><p>Versione: {$conn->server_info}</p>"; ?> </code>
Test: Visita http://helloworld.net/db_test.php
Installazione di phpMyAdmin
<code>apt install phpmyadmin -y </code>
Creare virtual host /etc/apache2/sites-available/phpmyadmin.conf
:
<code><VirtualHost *:80> ServerName phpmyadmin.local DocumentRoot /usr/share/phpmyadmin <Directory /usr/share/phpmyadmin> Options +FollowSymLinks -Indexes AllowOverride All Require all granted <Files "config.inc.php">Require all denied</Files> </Directory> </VirtualHost> </code>
<code>a2ensite phpmyadmin.conf systemctl reload apache2 </code>
Accesso: http://phpmyadmin.local
Ottimizzazione
Apache
<code>a2enmod cache cache_disk expires systemctl reload apache2 </code>
MariaDB
Aggiungere in /etc/mysql/mariadb.conf.d/50-server.cnf
:
<code>[mysqld] innodb_buffer_pool_size=128M innodb_log_file_size=32M max_connections=100 slow_query_log=1 long_query_time=2 </code>
HTTPS con Let's Encrypt
<code>apt install certbot python3-certbot-apache -y certbot --apache -d helloworld.net -d www.helloworld.net </code>
Monitoraggio e Log
Creare status.php
:
<code><?php header('Content-Type: application/json; charset=utf-8'); echo json_encode(['status'=>'ok','time'=>date('Y-m-d H:i:s')], JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE); ?> </code>
Sicurezza Aggiuntiva (per Produzione)
Per ambienti di sviluppo e test questa sezione può essere saltata — la protezione base UFW è sufficiente.
Per ambienti di produzione è consigliata protezione aggiuntiva:
Fail2Ban
<code>apt install fail2ban -y systemctl enable fail2ban --now </code>
Creare configurazione /etc/fail2ban/jail.local
:
<code>[DEFAULT] bantime = 3600 findtime = 600 maxretry = 3 [sshd] enabled = true [apache-auth] enabled = true port = http,https [apache-badbots] enabled = true port = http,https [apache-noscript] enabled = true port = http,https [apache-overflows] enabled = true port = http,https </code>
<code>systemctl restart fail2ban fail2ban-client status </code>
Diagnostica
Creare script /usr/local/bin/lamp-check
:
<code>#!/bin/bash echo "=== Stato LAMP ===" systemctl status apache2 mariadb php8.4-fpm echo "=== Stato Web ===" curl -s http://helloworld.net/status.php | jq . </code>
<code>chmod +x /usr/local/bin/lamp-check </code>
Aggiornamenti e Manutenzione
<code>apt install unattended-upgrades -y </code>
Creare job cron per manutenzione settimanale:
<code>echo "0 2 * * 0 apt update && apt upgrade -y && mysqlcheck --all-databases --optimize -u root" >> /etc/crontab </code>
Fatto!
LAMP su Debian 13 con PHP 8.4 e MariaDB 11.8 è installato, ottimizzato e messo in sicurezza!
Content
- Preparazione Preliminare
- Installazione di Apache
- Configurazione del Firewall
- Virtual Host
- Installazione di PHP 8.4
- Test PHP
- Installazione di MariaDB
- Test di Connessione al Database
- Installazione di phpMyAdmin
- Ottimizzazione
- HTTPS con Let's Encrypt
- Monitoraggio e Log
- Sicurezza Aggiuntiva (per Produzione)
- Diagnostica
- Aggiornamenti e Manutenzione
- Fatto!