Migrasi dari Apache ke Nginx sering dilakukan untuk meningkatkan performa, efisiensi memori, dan skalabilitas aplikasi. NodeJS, yang biasanya berjalan di belakang Apache dengan proxy, akan lebih optimal jika dipadukan dengan Nginx.
Berikut panduan lengkap, mulai dari persiapan hingga optimasi pasca-migrasi.
Contents
- 1 1. Persiapan Migrasi
- 2 2. Install dan Konfigurasi Dasar Nginx
- 3 3. Memahami Perbedaan Apache vs Nginx
- 4 4. Migrasi VirtualHost → Server Block Nginx
- 5 5. Migrasi Rewrite Rules
- 6 6. Reverse Proxy untuk NodeJS
- 7 7. Konfigurasi SSL / HTTPS
- 8 8. Testing dan Validasi Konfigurasi
- 9 9. Optimasi Nginx untuk NodeJS
- 10 10. Disable Apache dan Clean-up
- 11 11. Monitoring dan Logging
- 12 12. Troubleshooting Umum
- 13 13. Praktik Terbaik Migrasi
- 14 Kesimpulan
- 15 Related Posts
1. Persiapan Migrasi
a. Backup Sistem
Sebelum memulai, backup semua data penting:
# Backup konfigurasi Apache
sudo cp -r /etc/apache2 /etc/apache2-backup
# Backup website dan NodeJS app
tar -czvf website-backup.tar.gz /var/www/html
tar -czvf nodejs-backup.tar.gz /path/to/nodejs-app
# Backup database
mysqldump -u dbuser -p dbname > db-backup.sql
Catatan: Backup memastikan Anda bisa rollback jika terjadi masalah.
b. Inventarisasi Konfigurasi Apache
- VirtualHost
- RewriteRules (.htaccess)
- SSL/TLS (certificates)
- Modul aktif (mod_rewrite, mod_proxy, mod_headers, mod_ssl)
- Directory permissions
2. Install dan Konfigurasi Dasar Nginx
a. Install Nginx
Ubuntu/Debian:
sudo apt update
sudo apt install nginx
CentOS/RHEL:
sudo yum install epel-release
sudo yum install nginx
b. Verifikasi Nginx
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx
Jika berjalan, default page Nginx bisa diakses via browser.
3. Memahami Perbedaan Apache vs Nginx
| Fitur | Apache | Nginx |
|---|---|---|
| Architecture | Process/thread-based | Event-driven, asynchronous |
| Handling Concurrent | Thread per request | Non-blocking, single-threaded |
| Rewrite (.htaccess) | Didukung | Tidak didukung, konfigurasi di nginx.conf |
| Reverse Proxy | Mod_proxy | Built-in, performa tinggi |
| SSL/TLS | Mod_ssl | Native support, Let’s Encrypt mudah |
Penting: Semua
.htaccessharus diterjemahkan ke Nginx server block.
4. Migrasi VirtualHost → Server Block Nginx
Apache VirtualHost contoh:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html/example
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Nginx Server Block ekivalen:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/html/example;
index index.html index.htm;
access_log /var/log/nginx/example.access.log;
error_log /var/log/nginx/example.error.log;
location / {
try_files $uri $uri/ =404;
}
}
Gunakan direktori
/etc/nginx/sites-available/dan link ke/etc/nginx/sites-enabled/untuk manajemen server block yang rapi.
5. Migrasi Rewrite Rules
Apache mod_rewrite contoh:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
Nginx ekivalen:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
Redirect HTTP → HTTPS:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
Semua aturan redirect atau rewrite Apache harus diterjemahkan. Gunakan
rewriteatautry_filesdi Nginx.
6. Reverse Proxy untuk NodeJS
Jika NodeJS berjalan di port 3000:
server {
listen 80;
server_name example.com www.example.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Catatan penting:
proxy_set_header Upgrade $http_upgrade→ untuk WebSocketproxy_set_header Host $host→ agar host asli diteruskan ke NodeJSproxy_cache_bypass→ mencegah cache mengganggu WebSocket atau data real-time
7. Konfigurasi SSL / HTTPS
Gunakan Let’s Encrypt:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
- Certbot akan otomatis menambahkan konfigurasi SSL di server block.
- Hasil konfigurasi Nginx akan menambahkan:
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
8. Testing dan Validasi Konfigurasi
- Syntax check:
sudo nginx -t
- Reload Nginx:
sudo systemctl reload nginx
- Cek log untuk error:
tail -f /var/log/nginx/error.log
tail -f /var/log/nginx/access.log
- Cek semua route, API NodeJS, dan WebSocket berjalan normal.
9. Optimasi Nginx untuk NodeJS
a. Worker Processes & Connections
worker_processes auto;
events {
worker_connections 2048;
}
b. Gzip Compression
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
c. Caching Static Content
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
d. Timeout & Buffer
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
client_max_body_size 10M;
10. Disable Apache dan Clean-up
Setelah Nginx berjalan normal:
sudo systemctl stop apache2
sudo systemctl disable apache2
sudo systemctl status apache2
Pastikan tidak ada konflik port 80/443 sebelum menonaktifkan Apache.
11. Monitoring dan Logging
- Access & Error Logs:
/var/log/nginx/access.log,/var/log/nginx/error.log - NodeJS logs: Gunakan PM2 atau systemd untuk restart otomatis:
pm2 start app.js --name myapp
pm2 startup
pm2 save
- Monitoring Tools: Grafana + Prometheus atau ELK stack untuk performa NodeJS + Nginx.
12. Troubleshooting Umum
| Masalah | Solusi |
|---|---|
| 502 Bad Gateway | Pastikan NodeJS berjalan, port sesuai proxy |
| Rewrite tidak bekerja | Cek try_files di Nginx, terjemahkan .htaccess dengan benar |
| SSL error | Periksa sertifikat, jalankan sudo certbot renew --dry-run |
| WebSocket tidak berjalan | Pastikan proxy_set_header Upgrade $http_upgrade dan Connection "upgrade" |
13. Praktik Terbaik Migrasi
- Lakukan migrasi di staging server sebelum production.
- Backup semua konfigurasi Apache dan aplikasi.
- Migrasi bertahap:
- Step 1: Install Nginx
- Step 2: Konfigurasi server block
- Step 3: Reverse proxy NodeJS
- Step 4: Testing semua route dan API
- Step 5: Optimasi Nginx
- Gunakan monitoring & alerting setelah go-live.
- Dokumentasikan konfigurasi Nginx untuk tim lain.
Kesimpulan
Migrasi dari Apache ke Nginx untuk NodeJS memberikan keuntungan signifikan:
- Performa lebih baik untuk traffic tinggi
- Penggunaan memori lebih rendah
- Reverse proxy built-in lebih efisien
- Optimasi caching & compression lebih fleksibel
- Skalabilitas lebih mudah dengan container atau auto-scaling
Proses migrasi harus dilakukan secara bertahap, diawali dengan backup, testing di staging, validasi rewrite & SSL, hingga disable Apache. Dengan pendekatan ini, downtime dapat diminimalkan dan performa NodeJS meningkat.
