Professional IT Partner
Digital Knowledge Base

Petunjuk Teknis: SystemD vs PM2 untuk Aplikasi Node.js

I
IT Musafir
24 Feb 2026, 08:22
62 Views
3 Menit Baca
563 Kata
Petunjuk Teknis: SystemD vs PM2 untuk Aplikasi Node.js
Node.js, Linux, DevOps
3 Menit
563 Kata
## Setup SystemD ### 1. Membuat Service File Buat file service di `/etc/systemd/system/your-app.service`: ```ini [Unit] Description=Your Node.js Application After=network.target [Service] Type=simple User=www-data WorkingDirectory=/var/www/your-app ExecStart=/usr/bin/node /var/www/your-app/server.js Restart=always RestartSec=10 Environment=NODE_ENV=production Environment=PORT=3000 # Logging StandardOutput=syslog StandardError=syslog SyslogIdentifier=your-app # Security NoNewPrivileges=true PrivateTmp=true ProtectSystem=strict ProtectHome=true ReadWritePaths=/var/www/your-app [Install] WantedBy=multi-user.target ``` ### 2. Perintah Dasar ```bash # Reload daemon sudo systemctl daemon-reload # Start service sudo systemctl start your-app # Enable auto-start saat boot sudo systemctl enable your-app # Cek status sudo systemctl status your-app # Lihat logs sudo journalctl -u your-app -f # Restart service sudo systemctl restart your-app # Stop service sudo systemctl stop your-app ``` ### 3. Variabel Lingkungan (Environment Variables) ```bash # Metode 1: Langsung di file service Environment=NODE_ENV=production Environment=DATABASE_URL=postgresql://user:pass@localhost/db # Metode 2: File env terpisah EnvironmentFile=/var/www/your-app/.env.production ``` --- ## Setup PM2 ### 1. Instalasi ```bash # Instalasi global npm install -g pm2 # Atau menggunakan yarn yarn global add pm2 ``` ### 2. Penggunaan Dasar ```bash # Start aplikasi pm2 start server.js --name "my-app" # Start dengan variabel lingkungan pm2 start server.js --name "my-app" --env production # Start dengan file konfigurasi pm2 start ecosystem.config.js # Daftar proses yang berjalan pm2 list # Monitor pm2 monit # Lihat logs pm2 logs my-app # Restart pm2 restart my-app # Stop pm2 stop my-app # Delete pm2 delete my-app ``` ### 3. File Konfigurasi Ecosystem Buat `ecosystem.config.js`: ```javascript module.exports = { apps: [{ name: 'my-app', script: 'server.js', instances: 'max', // Use all CPU cores exec_mode: 'cluster', env: { NODE_ENV: 'development', PORT: 3000 }, env_production: { NODE_ENV: 'production', PORT: 3000 }, // Konfigurasi auto-restart watch: false, max_memory_restart: '1G', // Logging log_file: '/var/log/pm2/combined.log', out_file: '/var/log/pm2/out.log', error_file: '/var/log/pm2/error.log', log_date_format: 'YYYY-MM-DD HH:mm:ss Z', // Opsi lanjutan kill_timeout: 5000, wait_ready: true, listen_timeout: 10000 }] }; ``` ### 4. Perintah PM2 ```bash # Start dengan konfigurasi ecosystem pm2 start ecosystem.config.js --env production # Simpan daftar proses saat ini pm2 save # Setup startup script pm2 startup # Generate startup script (manual) pm2 startup systemd -u www-data --hp /var/www # Reload dengan zero downtime pm2 reload my-app # Scale instances pm2 scale my-app +2 pm2 scale my-app 4 # Monitor aplikasi spesifik pm2 monit my-app # Dapatkan info detail pm2 show my-app ``` --- ## Perbandingan Fitur | Fitur | SystemD | PM2 | |-------|---------|-----| | **Instalasi** | Built-in Linux | npm install -g pm2 | | **Konfigurasi** | .service file | ecosystem.config.js | | **Auto-restart** | ✅ Built-in | ✅ Built-in | | **Zero-downtime reload** | ❌ | ✅ | | **Clustering** | ❌ Manual | ✅ Built-in | | **Monitoring** | journalctl, systemctl | pm2 monit, pm2 logs | | **Manajemen memory** | Dasar | Lanjutan (max_memory_restart) | | **Manajemen proses** | Dasar | Lanjutan | | **Fitur development** | ❌ | ✅ (watch mode, dll.) | | **Penggunaan resource** | Minimal | Overhead tambahan | | **Kesiapan production** | ✅ Sangat Baik | ✅ Baik | | **Learning curve** | Sedang | Mudah | --- ## Rekomendasi Implementasi ### Skenario 1: Lingkungan Production (Direkomendasikan: SystemD) ```bash # 1. Setup user sudo adduser --system --group --home /var/www/your-app app-user # 2. Deploy aplikasi sudo -u app-user git clone /var/www/your-app cd /var/www/your-app sudo -u app-user npm ci --production # 3. Buat systemd service sudo nano /etc/systemd/system/your-app.service # (paste konfigurasi systemd dari atas) # 4. Setup permissions sudo chown -R app-user:app-user /var/www/your-app # 5. Start service sudo systemctl daemon-reload sudo systemctl enable your-app sudo systemctl start your-app ``` ### Skenario 2: Lingkungan Development (Direkomendasikan: PM2) ```bash # 1. Install PM2 npm install -g pm2 # 2. Buat konfigurasi ecosystem cat > ecosystem.config.js << 'EOF' module.exports = { apps: [{ name: 'dev-app', script: 'server.js', watch: ['src', 'config'], ignore_watch: ['node_modules', 'logs'], env: { NODE_ENV: 'development', PORT: 3000 } }] }; EOF # 3. Start development server pm2 start ecosystem.config.js pm2 save pm2 startup ``` ### Skenario 3: Pendekatan Hybrid (Terbaik dari Keduanya) **Development**: Gunakan PM2 untuk development yang mudah **Production**: Gunakan SystemD dengan PM2 sebagai process manager ```bash # Production systemd service yang menjalankan PM2 [Unit] Description=Aplikasi Anda via PM2 After=network.target [Service] Type=forking User=www-data WorkingDirectory=/var/www/your-app ExecStart=/usr/bin/pm2 start ecosystem.config.js --env production ExecReload=/usr/bin/pm2 reload all ExecStop=/usr/bin/pm2 stop all PIDFile=/home/www-data/.pm2/pm2.pid Restart=always [Install] WantedBy=multi-user.target ``` --- ## Troubleshooting ### Masalah SystemD ```bash # Cek status service sudo systemctl status your-app # Lihat logs detail sudo journalctl -u your-app -n 50 # Cek error syntax sudo systemd-analyze verify your-app.service # Test konfigurasi sudo systemd-run --unit=test-your-app /usr/bin/node /var/www/your-app/server.js ``` ### Masalah PM2 ```bash # Cek logs PM2 pm2 logs --lines 50 # Cek detail proses pm2 show your-app # Delete dan restart pm2 delete all pm2 start ecosystem.config.js # Cek instalasi PM2 pm2 --version which pm2 ``` ### Masalah Umum 1. **Permission Denied**: Cek kepemilikan file dan permissions 2. **Port Already in Use**: Gunakan `netstat -tlnp | grep :3000` 3. **Memory Issues**: Monitor dengan `free -h` dan sesuaikan batas memory 4. **Environment Variables**: Pastikan sudah diatur dengan benar --- ## Best Practices ### Keamanan - Jalankan sebagai non-root user - Gunakan file environment untuk data sensitif - Batasi akses file system - Update keamanan secara berkala ### Monitoring - Setup rotasi log - Monitor penggunaan resource - Setup alert untuk kegagalan - Health check berkala ### Backup - Backup file konfigurasi - Version control script deployment - Dokumentasikan konfigurasi kustom - Test prosedur recovery --- ## Matriks Keputusan Cepat | Use Case | Tool Direkomendasikan | Alasan | |----------|----------------------|---------| | Aplikasi production sederhana | SystemD | Built-in, reliable, overhead minimal | | Aplikasi kompleks dengan clustering | PM2 | Built-in clustering, zero-downtime reload | | Workflow development | PM2 | Watch mode, restart mudah, monitoring | | Production enterprise | SystemD + Nginx | Paling robust, integrasi terbaik | | Tim dengan pengetahuan Linux terbatas | PM2 | Learning curve lebih mudah | | Lingkungan dengan resource terbatas | SystemD | Memory overhead minimal | --- *Terakhir diperbarui: Desember 2025*

Review Pembaca

Beri penilaian dan komentar untuk artikel ini.

4.8 (10 review)
Budi Santoso
25 Feb 2026

Insight-nya relevan dengan kebutuhan kerja saya.

Siti Rahma
25 Feb 2026

Artikelnya sangat membantu dan mudah dipahami.

Ilham Maulana
25 Feb 2026

Bahasanya ringan tapi tetap mendalam.

Siti Rahma
25 Feb 2026

Tips yang diberikan benar-benar kepakai.

Nabila Putri
25 Feb 2026

Tips yang diberikan benar-benar kepakai.