Install Web Server - Apache2 di Linux

Apache HTTP Server (Apache2) adalah salah satu web server tertua dan paling banyak digunakan di dunia.
Apache dikenal fleksibel, stabil, dan memiliki banyak modul untuk menambah fitur (SSL, rewrite, proxy, dll).
Dokumen ini membahas instalasi, konfigurasi dasar, best practice, security hardening, serta setup SSL dengan Let's Encrypt.


⚙️ Instalasi Apache2

Debian/Ubuntu

sudo apt update
sudo apt install apache2 -y

RHEL/CentOS/Rocky

sudo yum install httpd -y
sudo systemctl enable httpd
sudo systemctl start httpd

Cek Status

systemctl status apache2   # Debian/Ubuntu
systemctl status httpd     # RHEL/CentOS

🛠️ Konfigurasi Dasar

File konfigurasi utama:

  • /etc/apache2/apache2.conf (Debian/Ubuntu)
  • /etc/httpd/conf/httpd.conf (RHEL/CentOS)
  • Virtual host: /etc/apache2/sites-available/ dan /etc/apache2/sites-enabled/

Contoh konfigurasi virtual host sederhana:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com

    DocumentRoot /var/www/example.com/html

    ErrorLog ${APACHE_LOG_DIR}/example_error.log
    CustomLog ${APACHE_LOG_DIR}/example_access.log combined
</VirtualHost>

Aktifkan konfigurasi (Debian/Ubuntu):

sudo a2ensite example.com.conf
sudo systemctl reload apache2

🔐 Best Security Configuration

  1. Nonaktifkan informasi versi Apache Edit /etc/apache2/conf-available/security.conf:

    ServerTokens Prod
    ServerSignature Off
  2. Batasi metode HTTP

    <LimitExcept GET POST HEAD>
    Deny from all
    </LimitExcept>
  3. Proteksi dari klikjacking & XSS

    Header always set X-Frame-Options "SAMEORIGIN"
    Header set X-Content-Type-Options nosniff
    Header set X-XSS-Protection "1; mode=block"
  4. Batasi ukuran upload

    LimitRequestBody 10485760   # 10 MB
  5. Nonaktifkan modul yang tidak diperlukan

    sudo a2dismod autoindex
    sudo a2dismod status
  6. Gunakan user non-root Pastikan Apache berjalan dengan user www-data (Debian/Ubuntu) atau apache (RHEL).


🔒 SSL & HTTPS Setup dengan Let's Encrypt

Instal Certbot

Debian/Ubuntu:

sudo apt install certbot python3-certbot-apache -y

RHEL/CentOS:

sudo yum install certbot python3-certbot-apache -y

Generate & Install SSL Certificate

sudo certbot --apache -d example.com -d www.example.com

Certbot akan otomatis mengedit konfigurasi Apache dan menambahkan SSL.
Cek konfigurasi:

sudo apachectl configtest
sudo systemctl reload apache2   # Debian/Ubuntu
sudo systemctl reload httpd     # RHEL/CentOS

Auto Renew SSL

Certbot membuat cron job otomatis, bisa diuji dengan:

sudo certbot renew --dry-run

🚀 Best Practices untuk Apache2 di Production

✅ Gunakan ServerTokens Prod & ServerSignature Off untuk menyembunyikan versi Apache.
✅ Pisahkan konfigurasi virtual host per domain.
✅ Gunakan ErrorLog dan CustomLog untuk monitoring.
✅ Nonaktifkan modul yang tidak digunakan.
✅ Gunakan firewall (UFW, iptables, atau security group) hanya untuk port 80 & 443.
✅ Aktifkan SSL (Let's Encrypt) dengan auto-renew.
✅ Gunakan mod_security dan mod_evasive untuk proteksi tambahan (WAF & DoS mitigation).
✅ Selalu update Apache2 untuk patch keamanan terbaru.
✅ Lakukan benchmark (ab, wrk) untuk tuning MaxRequestWorkers, KeepAliveTimeout, dll.


📌 Kesimpulan

  • Apache2 adalah web server stabil & fleksibel dengan banyak modul tambahan.
  • Konfigurasi security sangat penting untuk mengurangi risiko serangan.
  • SSL dengan Let's Encrypt memberikan enkripsi gratis dan auto-renew.
  • Best practice: minimalkan modul aktif, gunakan SSL, tuning parameter sesuai workload, dan gunakan modul keamanan tambahan.