Server Hardening Basics — Security

Server Hardening untuk Web Developer Server hardening adalah proses mengamankan server dengan mengurangi attack surface — menonaktifkan yang tidak perlu dan men

Server Hardening untuk Web Developer

Server hardening adalah proses mengamankan server dengan mengurangi attack surface — menonaktifkan yang tidak perlu dan mengonfigurasi yang ada dengan aman. Meskipun biasanya ini tugas DevOps, web developer perlu memahami dasarnya.

Prinsip Dasar

SSH Security

# /etc/ssh/sshd_config

# Gunakan SSH key, nonaktifkan password login
PasswordAuthentication no
PubkeyAuthentication yes

# Nonaktifkan root login
PermitRootLogin no

# Batasi user yang boleh SSH
AllowUsers deploy

# Ganti port default (opsional, security through obscurity)
Port 2222

# Timeout untuk idle connections
ClientAliveInterval 300
ClientAliveCountMax 2

Firewall (UFW)

# Deny semua incoming, allow semua outgoing
sudo ufw default deny incoming
sudo ufw default allow outgoing

# Hanya buka port yang diperlukan
sudo ufw allow 22/tcp    # SSH (atau port custom)
sudo ufw allow 80/tcp    # HTTP
sudo ufw allow 443/tcp   # HTTPS

# Aktifkan firewall
sudo ufw enable

# Cek status
sudo ufw status verbose

Automatic Security Updates

# Ubuntu/Debian: aktifkan automatic security updates
sudo apt install unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades

# Atau manual
sudo apt update && sudo apt upgrade -y

Web Server Hardening (Nginx)

server {
    # Sembunyikan versi Nginx
    server_tokens off;

    # Security headers
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header Strict-Transport-Security "max-age=31536000" always;

    # Blokir akses ke file sensitif
    location ~ /\.(?!well-known) {
        deny all;  # Blokir .env, .git, dll.
    }

    # Batasi methods
    if ($request_method !~ ^(GET|POST|PUT|DELETE|OPTIONS)$) {
        return 405;
    }
}

Database Hardening

# MySQL/MariaDB
# 1. Jalankan security script
mysql_secure_installation

# 2. Buat user khusus untuk aplikasi (bukan root)
CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'strong_password';
GRANT SELECT, INSERT, UPDATE, DELETE ON mydb.* TO 'app_user'@'localhost';
# Jangan berikan DROP, ALTER, atau GRANT privileges!

Checklist Server Hardening

Yang akan kamu pelajari