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
sudo apt update sudo apt upgrade -y
Langkah 2 — Install Apache2
sudo apt install apache2 -y
Langkah 3 — Enable & start service
sudo systemctl enable apache2 sudo systemctl start apache2
Langkah 4 — Cek status
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:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
Langkah 2 — Install Node.js
sudo apt install -y nodejs
Langkah 3 — Cek versi
node -v npm -v
Opsional: Menjalankan Node.js di belakang layar (PM2)
sudo npm install -g pm2 pm2 startup pm2 save
Opsional: Gunakan Apache2 sebagai reverse proxy ke Node.js
Install module proxy:
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
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:
nano /home/debian/app.js
Lalu jalankan:
pm2 start app.js
1. Aktifkan modul proxy di Apache
Jalankan:
sudo a2enmod proxy proxy_http sudo systemctl restart apache2
2. Buat VirtualHost Apache untuk proxy ke Node.js
Buat file config baru:
sudo nano /etc/apache2/sites-available/nodeapp.conf
<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
</VirtualHost>
3. Enable site & reload Apache
sudo a2ensite nodeapp.conf sudo systemctl reload apache2
4. Test
http://IP-SERVER
5. Disable default site
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:
cd /home/debian nano app2.js
Isi contoh:
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
pm2 start app2.js --name app4000
Cek:
pm2 ls
3. Tambahkan konfigurasi Apache untuk aplikasi kedua
tambahkan
sudo nano /etc/apache2/sites-available/nodeapp.conf
isi
<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:
sudo systemctl reload apache2
Lalu akses:
http://IP/app1 http://IP/app2
