Introduction

The goal of this tutorial is to show how to setup an Ubuntu server to run Node.js apps. The steps will help you avoid security mistakes and also allow you to not run the app as root, use port 80 to run your app and make it restart when the server starts.

To follow this tutorial you might have basic knowledge on Linux. You can skip some steps if you do not need them, but they will give some advantages.

Install CURL

We are using CURL to install Node.js on the server. For this, issue the following command:

sudo apt-get install curl

sudo means that you will be running this command as root user. You will have to type your password and after you have done that the command will run.

Install Latest Node.JS

By the time I am writing this tutorial, the latest version of Node.js is 13.8.0. If there are any other new versions when you try to install, use them instead.

Type the following commands and see as your instance downloads, compiles and installs Node.js. P.s. Type one line at a time.

curl -sL https://deb.nodesource.com/setup_13.x | sudo -E bash -
sudo apt-get install -y nodejs

When the process is over, ensure all went well by typing:

node -v

If everything went well you should see the version of Node.js you installed. In the case of this tutorial, you might see something like this:

v13.8.0

Give Safe User Permission to Use Port 80

We are not running the applications as the root user, but there is one thing: the safe user does not have permission to use the default HTTP port (8). Remember that the objective is to be able to publish a website which visitors can access by an easy URL like http://sitename.com.

But unless you sign in as root, you will have to use a different URL.

The solution for this is to type the following commands:

sudo apt-get install libcap2-bin
sudo setcap cap_net_bind_service=+ep /usr/local/bin/node

Now you can easily use port 80 with Node application.

Use NPM to Install A Package Called PM2

NPM is a package we use to install frameworks and libraries to use with Node.js apps and it was installed with Node.js. PM2 is a tool that will keep your site up as it restarts the application if it crashes, and also help you by restarting the node application as a service when you restart the server.

To install PM2 you just have to issue the following command:

sudo npm install pm2 -g

Create a Node App

Now you should test if everything will be working as it should. We are using the IP address, but you should use a domain name when you test it.

First, issue the following command:

nano app.js

Then enter the following lines into the editor:

var http = require('http');
var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello World\n");
});
server.listen(80);
console.log("Server running at http://127.0.0.1:80/");

Press ctrl-X to exit and save.

Now you have a node based app called app.js which you can use to test your environment.

To run the app, just enter the command line:

node.js

But we are going to use PM2 to run the app instead of Node.

Run the app using PM2

To start, issue the following command:

pm2 start app.js 

You should see the following report

report

Running your app this way will ensure PM2 will automatically restart the app if it crashes. It will also keep a log of your unhandled exceptions and ensure that any app it manages restart when the server reboots.

Run the following command to run your app as a service by typing the following command:

sudo env PATH=$PATH:/usr/local/bin pm2 startup -u safeuser

You may not be using safeuser as the user name - use the name of your setup. You should see a report like this:

Adding system startup for /etc/init.d/pm2-init.sh ...
   /etc/rc0.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
   /etc/rc1.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
   /etc/rc6.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
   /etc/rc2.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
   /etc/rc3.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
   /etc/rc4.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
   /etc/rc5.d/S20pm2-init.sh -> ../init.d/pm2-init.sh

It’s done! Now you have your pm2 and node.js working together!

Read more about: Ubuntu