Cara install aplikasi Node.JS di hosting Cpanel

Halo,

untuk running aplikasi node js terdapat 2 cara sesuaikan dengan web server yang anda gunakan.

1. 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>

 


note: pastikan replace namadomain.com dengan domain anda sendiri.

2.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");
});
server.listen(65449);



3. 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

1. 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.


2. 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.

  • 1 Users Found This Useful
Was this answer helpful?

Powered by WHMCompleteSolution