Wednesday, July 26, 2017

How to setup a NodeJS app on Ubuntu

Untitled Document.md

Make All The Services

Ubuntu uses something called Upstart for services so you can either use services already on your box or make new ones. Your app uses nodejs so lets review how to install that and then make a service out of it:

Install NodeJS

This installs it to /usr/bin/nodejs:

sudo apt-get update
sudo apt-get install nodejs

Make the Web User and the web hosting directory

adduser www
mkdir /home/www/run
chown -R www:www /home/www/run && chmod -R 770 /home/www/run
mkdir -P /home/www/MY_APP/log
chown -R www:www /home/www/MY_APP && chmod -R 770 /home/www/MY_APP

Push All The Codes

Now that you are done writing your code, go ahead and push the code to:

/home/www/MY_APP/

Run this command again after to give the right permissions on your newly placed files:

chown -R www:www /home/www/MY_APP && chmod -R 770 /home/www/MY_APP

Your directory structure should look something like this because NodeJS:

node_modules
package.json
server.js
public/
routes/
view/

What we’re concerned with is the server.js file. That isNode.JS entry point to the world. In that file you will have something like the following symbolizing that your app runs on port 3000:


var app = express();

app.set('port', process.env.PORT || 3000);

Make the Upstart Service

Making a service is as easy as adding a .conf file to the /etc/init directory:

/etc/init/MY_APP.conf
description "MY_APP"
author "MY_NAME"


env NODE=/usr/bin/nodejs
env SCRIPT=/home/www/MY_APP/server.js
env LOG_FILE=/home/www/MY_APP/log/stdout.log
env PID_FILE=/home/www/run/MY_APP.pid
env USER=www


start on filesystem or runlevel [2345]
stop on shutdown


respawn
respawn limit 10 5


script
    exec start-stop-daemon --start --chuid $USER --make-pidfile --pidfile $PID_FILE --exec $NODE $SCRIPT >> $LOG_FILE 2>&1
end script


pre-start script
    touch $LOG_FILE
    chown root:$USER $LOG_FILE
    chmod 770 $LOG_FILE
    echo "[`date`] MY_APP starting" >> $LOG_FILE
end script


pre-stop script
    rm -f $PID_FILE
    echo "[`date`] MY_APP stopping" >> $LOG_FILE
end script

Check to make sure the new Service is ok:

 init-checkconf /etc/init/MY_APP.conf

Check the status of the new Service

service MY_APP status

Start the new Service

service MY_APP start
  1. Check the status of the new Service again
service MY_APP status

Why all this logging crap? Logs are important, when your NodeJS app starts behaving strangely you will want to know why on your production server so follow the paths of the log files, and verify they are there and correct.

I owe it to https://ubuverse.com/run-your-node-js-app-as-an-upstart-service/ who originally wrote a general version of the Upstart user creation and script

Aren’t Reverse Proxies Fun?

Your app is running but you have to hit port:3000 in order to view it, what do we do now?

This guide is how to setup Apache to handle your reverse proxy because Apache and because exposing NodeJS to port:80 is bad.

Enable proxies in apache

 sudo a2enmod proxy_http

Go to your /etc/apache2/sites-available/000-default.conf or whatever config file you are using to tell apache what to do:

<VirtualHost *:80>
       DocumentRoot /home/www/MY_APP
       ServerName www.awesome.com
       ServerAlias awesome.com

       ProxyRequests on
       ProxyPass / http://localhost:3000/
</VirtualHost>

Reload Apache

sudo service apache2 reload