Menjalankan aplikasi NodeJS terdapat 2 cara sesuaikan dengan web server yang anda gunakan. Namun untuk pengguna Cloudlinux OS, sudah terdapat fasilitas NodeJS Selector dimana disetiap akun cPanel, kamu sudah bisa memilih versi NodeJS dan deploy aplikasi NodeJS semakin mudah. Simak semua tutorial sederhana dibawah ini
web server litespeed
https://www.litespeedtech.com/support/wiki/doku.php/litespeed_wiki:other-ext-apps:nodejs-setup
- buatlah file .htaccess pada halaman public_html anda
Options +FollowSymLinks -Indexes
IndexIgnore *
DirectoryIndex
<IfModule mod_rewrite.c>
RewriteEngine on
# Simple URL redirect:
RewriteRule ^(.*)$ http://namadomain.com:65449/$1 [P]
</IfModule>
-
buatlah index.js dengan isi:
note: pastikan replace namadomain.com dengan domain anda sendiri.var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end("hello world!\n");
});
server.listen(65449);
-
start aplikasi node js anda dengan cara login ssh dan masuk ke directory public_html
ketikkan:nohup node index.js &
atau dengan membuat file cron.php yang berisikan:<?php
$host = 'namadomain.com';
$port = '65449';
$cron = true; // Set $cron = false to Stop cron
if(@$_SERVER['SERVER_PORT'] > 1){
die('Not accesible via the web !');
}
if($cron == true){
$checkconn = @fsockopen($host, $port, $errno, $errstr, 5);
if(empty($checkconn)){
exec('export HOME=/home/username; cd /home/username/public_html; npm start --production >> /home/username/nodejs.log 2>&1 &', $out, $ret);
echo "start app";
}else{
echo "already running";
}
}
?>
lalu masuk ke menu cron jobs pada cpanel menu anda:
4,23,34,48 * * * * php -d disable_functions="" /home/username/public_html/cron.php >> /home/username/cron.log 2>&1
pastikan jangan lupa merubah namadomain.com dan username dengan username anda sendiri dan port degan angka yang berbeda. seperti:65480 atau 65488, dll..
setelah semua selesai , maka seharusnya dapat berjalan lancar.
web server apache
- buatlah file .htaccess pada halaman public_html anda
# Start of .htaccess
# Define AppRoot Folder
PassengerAppRoot /home/username/public_html
# Tell Passenger that your app is a Node.js app
PassengerAppType node
# Define NodeJS StartupFile
PassengerStartupFile index.js
# End of .htaccess
note: pastikan replace username dengan username account cpanel kalian. - buatlah index.js dengan isi:
var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end("hello world!\n");
});
maka jika anda buka domain anda akan muncul tulisan hello world.
perlu diingat bahwa framework nodejs seperti meanJS, ExpressJS,dll juga dapat dirunning menggunakan cara diatas.
jika ada hal-hal yang kurang jelas dapat menanyakan kepada tim support kami
NodeJS Selector
saat ini terdapat fitur NodeJS selector pada paket developer kami, sehingga tutorial diatas dapat digantikan dengan tutorial dibawah ini dengan lebih mudah
https://www.natanetwork.com/portal/knowledgebase/103/NEW-Tutorial-menjalankan-NodeJS-menggunakan-NodeJS-Selector-di-CPanel.html