Node.js adalah lingkungan runtime JavaScript (JS) sumber terbuka untuk mengembangkan aplikasi web. Node.js ringan dan efisien berkat model I/O yang berbasis event dan non-blocking, serta dukungan lintas platform.
Ada beberapa cara untuk menginstal Node.js di Debian. Pendekatan terbaik bergantung pada versi Node.js yang ingin digunakan.
Install Apache2 di Debian 12
Langkah 1 — Update repository
|
1 2 |
sudo apt update && sudo apt upgrade -y |
Langkah 2 — Install Apache2
|
1 2 |
sudo apt install apache2 -y |
Langkah 3 — Enable & start service
|
1 2 3 |
sudo systemctl enable apache2 sudo systemctl start apache2 |
Langkah 4 — Cek status
|
1 2 |
systemctl status apache2 |
Install Node.js di Debian 12
A. Install Node.js via NodeSource (disarankan: versi stabil terbaru)
Langkah 1 — Tambahkan NodeSource repository
Misal ingin Node.js v20 LTS:
|
1 2 |
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - |
Langkah 2 — Install Node.js
|
1 2 |
sudo apt install -y nodejs |
Langkah 3 — Cek versi
|
1 2 3 |
node -v npm -v |
Opsional: Menjalankan Node.js di belakang layar (PM2)
|
1 2 3 |
sudo npm install -g pm2 pm2 startup pm2 save |
Opsional: Gunakan Apache2 sebagai reverse proxy ke Node.js
Install module proxy:
|
1 2 3 |
sudo a2enmod proxy proxy_http sudo systemctl restart apache2 |
Pastikan kamu berada di folder project Node.js-mu
saya buat folder procjetnya di /home/debian/app.js Contoh sederhana
|
1 2 3 4 5 6 7 8 9 10 11 |
const http = require('http'); const server = http.createServer((req, res) => { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Node.js berjalan!\n'); }); server.listen(3000, () => { console.log('Server running on port 3000'); }); |
Simpan:
|
1 2 |
nano /home/debian/app.js |
Lalu jalankan:
|
1 2 |
pm2 start app.js |
1. Aktifkan modul proxy di Apache
Jalankan:
|
1 2 3 |
sudo a2enmod proxy proxy_http sudo systemctl restart apache2 |
2. Buat VirtualHost Apache untuk proxy ke Node.js
Buat file config baru:
|
1 2 |
sudo nano /etc/apache2/sites-available/nodeapp.conf |
|
1 2 3 4 5 6 |
<VirtualHost *:80> ProxyPreserveHost On ProxyPass / http://127.0.0.1:3000/ ProxyPassReverse / http://127.0.0.1:3000/ </VirtualHost> |
3. Enable site & reload Apache
|
1 2 3 |
sudo a2ensite nodeapp.conf sudo systemctl reload apache2 |
4. Test
|
1 2 |
http://IP-SERVER |
5. Disable default site
|
1 2 3 |
sudo a2dissite 000-default.conf sudo systemctl reload apache2 |
Baik! Kamu bisa menambahkan aplikasi Node.js kedua yang berjalan di port 4000, dan Apache akan mengarahkan domain/IP tertentu ke aplikasi tersebut.
Berikut panduan lengkapnya.
1. Buat aplikasi Node.js kedua (port 4000)
Misalnya kamu buat file baru:
|
1 2 3 |
cd /home/debian nano app2.js |
Isi contoh:
|
1 |
|
1 2 3 4 5 6 7 8 9 10 11 |
const http = require('http'); const server = http.createServer((req, res) => { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Node.js kedua berjalan di port 4000\n'); }); server.listen(4000, () => { console.log('Server running on port 4000'); }); |
2. Jalankan aplikasi kedua dengan PM2
|
1 |
|
1 2 |
pm2 start app2.js --name app4000 |
Cek:
|
1 2 |
pm2 ls |
3. Tambahkan konfigurasi Apache untuk aplikasi kedua
tambahkan
|
1 2 |
sudo nano /etc/apache2/sites-available/nodeapp.conf |
isi
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<VirtualHost *:80> ServerName 172.81.180.166 # Aplikasi 1 ProxyPreserveHost On ProxyPass /app1 http://127.0.0.1:3000/ ProxyPassReverse /app1 http://127.0.0.1:3000/ # Aplikasi 2 ProxyPass /app2 http://127.0.0.1:4000/ ProxyPassReverse /app2 http://127.0.0.1:4000/ </VirtualHost> |
Reload:
|
1 |
sudo systemctl reload apache2<br> |
Lalu akses:
|
1 2 3 |
http://IP/app1 http://IP/app2 |
