How to setup a private dedicated Node.js Ubuntu Server?

12,071

Solution 1

what do i need to install to properly run node.js apps on my network on a dedicated ubuntu server?

You just need to install nodejs. nodejs can run on any port, so you don't need Apache or anything else.

how do i install the latest nodejs,npm,and the init files on ubuntu server

Try to follow the steps outlined in this guide: http://howtonode.org/how-to-install-nodejs . Use the instructions for Ubuntu.

when i reboot the app starts automatically every time

One way to do this is to write a small script that will run on boot. The script would contain the instruction:

nodejs /path/to/app/app.js

Check out this SO answer on how to run a script on boot: https://stackoverflow.com/questions/3036/files-and-scripts-that-execute-on-boot

Solution 2

By your question, you sound about as lazy and impatient as I am, therefore use PPAs instead of building from the source. Just follow the node.js ubuntu directions.

In-fact I'm so lazy, I refuse to type in port numbers, hence I proxy all my node.js applications with nginx. (This is also the best way, and only way I can tell to have multiple servers "listening" on port 80). [Nginx's install guide.] Once you get nginx up, follow Chris Lea's guide.(http://wiki.nginx.org/Install) for the proxy.

BTW if you installed apache, make sure you purge it sudo apt-get purge apache*. This will most likely break your php apps, but that's why you're running node right? Just google how to run php with nignx.

Now for upstart & monit. Just follow this guide. NOTE: The guide has a typo so read the comments carefully.

As for samaba, you're on your own there.

TL;DR

Answer A: guide

Answer B: sudo cp my-node-app.conf /etc/init; sudo service my-node-app start

Edit 1

Upstart is Ubuntu's native utiltiy for starting background processes. Read all about it here.

#!upstart
description "node-app"
author      "me"

env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

respawn
start on runlevel [23]

script
    #set enviroment vars here
    export NODE_ENV=production
    #Uncommit if you need a pid file for monit
    #echo $$ > /var/run/node-app.pid
    exec /usr/bin/node /path/to/app.js 2>&1 >> /path/to/log/file/app.log
end script

#Logs start and stop time timestamps to the file
pre-start script
  echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /path/to/log/file/app.log
end script

pre-stop script
  #Uncomment if you need a pid file for monit
  #rm /var/run/yourprogram.pid
  echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /path/to/log/file/app.log
end script

Now start and stop your process by using service:

sudo service node-app start

Switch start with stop or status if needed.

If you are not using monit, just remove the pid lines. I really recommend using monit because you can configure it to give you email alert if your process dies or an error occurs in the log file.

Share:
12,071
cocco
Author by

cocco

http://stackoverflow.com/a/21353032/2450730 Nodejs,canvas,js,websockets,css33d fun! https://www.youtube.com/playlist?list=PLL9y8ZNSfDQxqg37X4-2A9T-r98RcAOAs http://jsfiddle.net/Mjagk/1/ - http://jsfiddle.net/Mjagk/4/ http://jsfiddle.net/LxX34/11/ http://jsfiddle.net/hP8Me/4/ - http://jsfiddle.net/qZm4e/ - http://jsfiddle.net/qZm4e/5/ http://jsfiddle.net/znT2H/ http://jsfiddle.net/trjsJ/ http://jsfiddle.net/pc76H/2/ http://jsfiddle.net/DAbh8/ - http://jsfiddle.net/DAbh8/4/ http://jsfiddle.net/RAu8Q/5/ golf http://jsfiddle.net/CA6GM/2/ nojs http://jsfiddle.net/KLXMw/ http://jsfiddle.net/ufcqc/ fastclick http://jsfiddle.net/UK4Es/ http://jsfiddle.net/98NHn/ http://jsfiddle.net/qzm6X/ http://jsperf.com/dgfgghfghfghghgfhgfhfghfhgfh http://jsfiddle.net/brm0kuda/ http://jsfiddle.net/brm0kuda/1/ http://jsfiddle.net/8qp5cvjs/ http://jsfiddle.net/7Nedw/5/ http://jsfiddle.net/0c10oujz/ http://jsfiddle.net/LxX34/18/ http://jsperf.com/bidirectionalindexof float bounceOut(float k){ return k<1/2.75?7.5625*kk:k<2/2.75?7.5625(k-=1.5/2.75)k+.75:k<2.5/2.75?7.5625(k-=2.25/2.75)k+.9375:7.5625(k-=2.625/2.75)*k+.984375; } http://jsfiddle.net/dgws3usa/ http://jsfiddle.net/dgws3usa/2/ plot https://jsfiddle.net/feceadjx/3/ id3tags 2.4.0 https://jsfiddle.net/2awq6pz7/ https://jsfiddle.net/Lxwf9nga/1/ https://jsfiddle.net/w3af98L1/

Updated on July 24, 2022

Comments

  • cocco
    cocco almost 2 years

    I found an old PC and i want to use it as a dedicated Node.js test machine.

    Basically i wanna write my apps on a win machine then copy them over samba to the node folder and launch them via ssh. Later, I would add an upstart script and copy it with samba to the server so that when i reboot the app starts automatically every time.

    1. What do I need to install in order to properly run Node.js apps on my network on a dedicated Ubuntu server? Here is the list I came up with, please correct me if I'm wrong. Is there anything else?
      • ssh
      • samba (ftp or sftp should be the way to go but as it's a closed internal network and i have to access it from various os's samba is the simplest way to share files not considering security issues..most of the time i use a simple text editor)
      • "basic ubuntu server" files?
      • "LAMP" (?)
      • node.js
      • node package manager.
    2. how do i install the latest Node.js, npm, and the init files on Ubuntu server. I saw that there was no simple sudo apt-get install nodejs npm.
    3. What kind of script do I need to launch my apps and where do i put them (prefer native scripts)?

    EDIT

    After some testing i'm at a good point now, and here is what i did:

    1. I installed ubuntu from a minimal CD
    2. when it comes to choose the packages i selected ONLY ssh & samba
    3. update the system
    4. install the dependencies that u need to run node.js
    5. install latest node from git
    6. setup samba in my case i created the folder /var/nodejs for the scripts
    7. put your testApp.js in the nodejs folder
    8. start your testApp.js from ssh. *it won't work

    3-update the system

    sudo apt-get update && sudo apt-get upgrade
    

    4-dependancies

    sudo apt-get install g++ curl libssl-dev apache2-utils git-core make
    

    5-install node

    git clone git://github.com/ry/node.git
    cd node
    ./configure
    make
    sudo make install
    

    6-setup samba sudo nano /etc/samba/smb.conf

    [nodejs]
    comment = nodejs
    workgroup = WG
    security = USER
    path = /var/nodejs
    server string =Node JS
    browsable = yes
    read only = no
    writeable = yes
    create mask = 0777
    

    7-testApp.js

    var http = require('http');
    http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello Node.js\n');
    }).listen(80, "192.168.0.1");
    console.log('Server running at http://192.168.0.1:80/');
    

    8-Now everything should run...but:

    You can run nodejs only as administrator appending "sudo" in front of the launch command else as a normal user u don't have access to most of the ports under 1000.

    A. How can i lauch my app on port 80 without using sudo?

    And obviously if u launch you app with the command sudo node /var/nodejs/testApp.js if u close the terminal the app will stop.

    For that we use a init script.

    After some reading i found that upstart is natively installed in ubuntu server and it's probably the best way to launch your apps.

    B. I know u need to put the script into /etc/init/ with your appname and .conf extension.but how does that work?

  • leeway
    leeway almost 11 years
    So you don't really need to purge apache, but if you're green to this who web-server stuff, it's a ton easier. Once you get comfortable, then bring in apache. You can also proxy with apache, but I don't know how to do that.
  • cocco
    cocco almost 11 years
    i don't want to install apache. nodejs is my server.i don't want to install svn.i use samba because i'm developing on every type of OS using the native text app and the most common visual/open/simple way is SAMBA (the system is in a closed network.). i read about nodemon and i think 'upstart' is more systemrelative.but thanks for the port 80 tip
  • cocco
    cocco almost 11 years
    PPA is a better solution as mine(as mine don't update right?).Even if the nginx is very fast and the proxy thing is very intresting i want that node.js is the only server on the system.I already read that upstart guide.. but i don't want to install monit & what is the program.pid and program.sys. to make it short i'm happy if i get ONLY 'upstart' working even if i need to append sudo. ps.:What means TL;DR?
  • cocco
    cocco almost 11 years
    regarding port 80 that is redirect... i'm trying to keep node as fast as possibble.
  • cocco
    cocco almost 11 years
    all i try to do is to keep node standalone and as fast as possible.most of the time i will use ssh. samba is just simple way to edit my files over the private network.and even if its a security problem launching my node.js apps with sudo now it does not make any difference.so i would be very happy if i get to know a little more just about the native upstart.
  • cocco
    cocco almost 11 years
    what is the program.pid and program.sysin the init script?
  • cocco
    cocco almost 11 years
    i would be very happy if you extend your answer and add me a proper /etc/init/yourprogram.conf for my configurtion... without monit.
  • leeway
    leeway almost 11 years
    Yes, PPA will stay updated if you update them (apt-get update & apt-get upgrade). Nginx is faster for service static content then node.js and it allows two node servers to listen on the same port. The pid is the process id. It changes when your process changes. Monit is watching that file for changes and sending you alerts when it does.
  • kleopatra
    kleopatra over 10 years
    tried to format your code (not overly successful ;-) - please edit and improve