Debian'da Apache+ PHP+MariaDB kurulumu ve yapılandırılması
Debian'da Apache+ PHP+MariaDB kurulumu ve yapılandırılması

Debian'da Apache+ PHP+MariaDB kurulumu ve yapılandırılması

Okumak 6 dakika

LAMP web geliştirme için bir yığındır. Debian 13 "Trixie" PHP 8.4 ve MariaDB 11.8 içerir. Tüm komutlar root olarak veya sudo ile çalıştırılır.

Ön Hazırlık

Temiz bir sisteminiz varsa, bu bölümü atlayabilir ve Apache kurulumuna geçebilirsiniz.

Zaten web bileşenleri kurulu ise, güvenlik için aşağıdaki komutları çalıştırın:

Mevcut konfigürasyonların yedeklerini oluşturma

<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>

Sistem güncellemesi ve port kontrolü

<code>apt update && apt full-upgrade -y
ss -tuln | grep -E ':(80|443)\s' || echo "80/443 portları boş"
systemctl is-active nginx >/dev/null && { systemctl stop nginx; systemctl disable nginx; }
</code>

Bu komutlar:

  • Sadece mevcut konfigürasyonların yedeklerini oluşturur
  • 80/443 portlarının kullanılabilirliğini kontrol eder
  • Kurulu ise Nginx'i durdurur (Apache ile çakışmayı önlemek için)

Apache Kurulumu

<code>apt install apache2 -y
systemctl enable apache2 --now
a2enmod rewrite ssl headers expires deflate
systemctl reload apache2
</code>

Test: Tarayıcıda sunucu IP'sini açın:

<code>hostname -I | awk '{print $1}'
</code>

Güvenlik Duvarı Konfigürasyonu

<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>

Sanal Host

Dizin ve test sayfası oluşturma:

<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="tr"><head><meta charset="UTF-8"><title>Merhaba Dünya</title></head><body><h1>Merhaba Dünya - Debian 13</h1></body></html>
EOF
</code>

Konfigürasyon:

/etc/apache2/sites-available/helloworld.net.conf oluşturun:

<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>

Aktivasyon:

<code>a2dissite 000-default.conf 2>/dev/null
a2ensite helloworld.net.conf
apache2ctl configtest && systemctl reload apache2
</code>

Test: http://helloworld.net  adresini ziyaret edin

PHP 8.4 Kurulumu

<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>

PHP Testi

/var/www/helloworld.net/test.php dosyasını oluşturun:

<code><?php
echo "<h1>PHP ".phpversion()."</h1><p>Sunucu: ".php_uname()."</p><p>Zaman: ".date('Y-m-d H:i:s')."</p><h3>Uzantılar:</h3><ul>";
foreach(['mysqli','curl','gd','mbstring','xml'] as $ext) echo "<li>$ext: ".(extension_loaded($ext)?"✅":"❌")."</li>";
echo "</ul>";
?>
</code>

Test: http://helloworld.net/test.php  adresini ziyaret edin

MariaDB Kurulumu

<code>apt install mariadb-server mariadb-client -y
systemctl enable mariadb --now
mysql_secure_installation
</code>

MariaDB 11.8 varsayılan olarak root için unix_socket kullanır. Sihirbazda önerilen cevaplar:

  • N — root parolasını değiştirme
  • Y — anonim kullanıcıları kaldır
  • Y — root'un uzaktan erişimini yasakla
  • Y — test veritabanını kaldır
  • Y — yetki tablolarını yeniden yükle

Veritabanı Bağlantı Testi

<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>

config.php oluşturun:

<code><?php
$webapp_password = trim(file_get_contents('/root/.webapp_password'));
$conn = new mysqli('localhost', 'webapp', $webapp_password, 'test_db');
?>
</code>

Doğrulama için db_test.php oluşturun:

<code><?php
require_once 'config.php';
if($conn->connect_error) die("DB hatası: {$conn->connect_error}");
echo "<h2>✅ MariaDB</h2><p>Sürüm: {$conn->server_info}</p>";
?>
</code>

Test: http://helloworld.net/db_test.php adresini ziyaret edin

phpMyAdmin Kurulumu

<code>apt install phpmyadmin -y
</code>

Sanal host /etc/apache2/sites-available/phpmyadmin.conf oluşturun:

<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>

Erişim: http://phpmyadmin.local

Optimizasyon

Apache

<code>a2enmod cache cache_disk expires
systemctl reload apache2
</code>

MariaDB

/etc/mysql/mariadb.conf.d/50-server.cnf dosyasına ekleyin:

<code>[mysqld]
innodb_buffer_pool_size=128M
innodb_log_file_size=32M
max_connections=100
slow_query_log=1
long_query_time=2
</code>

Let's Encrypt ile HTTPS

<code>apt install certbot python3-certbot-apache -y
certbot --apache -d helloworld.net -d www.helloworld.net
</code>

İzleme ve Loglar

status.php oluşturun:

<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>

Ek Güvenlik (Üretim İçin)

Geliştirme ve test ortamları için bu bölüm atlanabilir — temel UFW koruması yeterlidir.

Üretim ortamları için ek koruma önerilir:

Fail2Ban

<code>apt install fail2ban -y
systemctl enable fail2ban --now
</code>

/etc/fail2ban/jail.local konfigürasyonu oluşturun:

<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>

Tanımlama

/usr/local/bin/lamp-check betiği oluşturun:

<code>#!/bin/bash
echo "=== LAMP Durumu ==="
systemctl status apache2 mariadb php8.4-fpm
echo "=== Web Durumu ==="
curl -s http://helloworld.net/status.php | jq .
</code>
<code>chmod +x /usr/local/bin/lamp-check
</code>

Güncellemeler ve Bakım

<code>apt install unattended-upgrades -y
</code>

Haftalık bakım için cron görevi oluşturun:

<code>echo "0 2 * * 0 apt update && apt upgrade -y && mysqlcheck --all-databases --optimize -u root" >> /etc/crontab
</code>

Tamamlandı!

PHP 8.4 ve MariaDB 11.8 ile Debian 13 üzerinde LAMP kuruldu, optimize edildi ve güvenli hale getirildi!

GPUDedicIT1