How to Install Node.js on Debian 10 and configure Nginx as a Frontend Proxy Server

Node.js is a free and open-source JavaScript platform that can be used to serve dynamic and responsive server-side content. It built on Chrome’s V8 JavaScript engine and able to executes JavaScript code outside of a web browser. It can be run on several operating systems including, Linux, FreeBSD, Windows, and macOS. Node.js also works on other web servers like Apache and Nginx.

In this tutorial, we will learn how to install and configure Node.js to handle backend requests and configure Nginx to handle front-end requests on Debian 10.

Prerequisites

  • A server running Debian 10.
  • A root password is configured on your server.

Getting Started

First, it is recommended to update your system to the latest version. You can do it with the following command:

apt-get update -y
apt-get upgrade -y

Once your server is updated, restart it to apply the changes.

Install Node.js

By default, the latest version of Node.js is not available in the Debian 10 default repository. So you will need to install it from the script.

First, install curl with the following command:

apt-get install curl -y

Next, download and install the Node.js installation script with the following command:

curl -sL https://deb.nodesource.com/setup_12.x | bash -

Once the installation has been completed, you can install the Node.js with the following command:

apt-get install nodejs -y

Once the installation has been completed successfully, you can verify the Node.js version with the following command:

nodejs --version

You should see the Node.js version in the following output:

v12.16.1

You can also check the NPM version by running the following command:

npm --version

You should see the NPM version in the  following output:

6.13.4

Create Node.js Application

First, you will need to create a directory for your application. You can create it by running the following command:

mkdir nodeapp

Next, change the directory to nodeapp and install express with the following command:

cd nodeapp
npm install express

Next, create a sample Node.js application with express using the nano editor:

nano app.js

Add the following contents:

var express = require('express');
var app = express();
app.get('/', function(req, res){
   res.send("Nginx as Frontend Server for Node.js!");
});
app.listen(8000, '127.0.0.1');

Save and close the file when you are finished.

Next, run your application with the following command:

node app.js

Next, open other terminal and verify the application with the following command:

netstat -antup | grep 8000

You should see that your Node.js application is listening on port 8000:

tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN      26193/node

You can also verify the application using curl command as shown below:

curl http://127.0.0.1:8000

If everything is fine, you should get the following output:

Nginx as Frontend Server for Node.js!

Install and Configure PM2

PM2 is a process manager to manage Node.js application. YOu can easily start, stop and enable Node.js application to start at boot using PM2.

First, install the latest version of PM2 using NPM as shown below:

npm install pm2 -g

Once installed, change the directory to nodeapp and your application with the following command:

cd nodeapp
pm2 start app.js

Once the application has been started, you should get the following output:

                        -------------

__/\\\\\\\\\\\\\____/\\\\____________/\\\\____/\\\\\\\\\_____
 _\/\\\/////////\\\_\/\\\\\\________/\\\\\\__/\\\///////\\\___
  _\/\\\_______\/\\\_\/\\\//\\\____/\\\//\\\_\///______\//\\\__
   _\/\\\\\\\\\\\\\/__\/\\\\///\\\/\\\/_\/\\\___________/\\\/___
    _\/\\\/////////____\/\\\__\///\\\/___\/\\\________/\\\//_____
     _\/\\\_____________\/\\\____\///_____\/\\\_____/\\\//________
      _\/\\\_____________\/\\\_____________\/\\\___/\\\/___________
       _\/\\\_____________\/\\\_____________\/\\\__/\\\\\\\\\\\\\\\_
        _\///______________\///______________\///__\///////////////__


                          Runtime Edition

        PM2 is a Production Process Manager for Node.js applications
                     with a built-in Load Balancer.

                Start and Daemonize any application:
                $ pm2 start app.js

                Load Balance 4 instances of api.js:
                $ pm2 start api.js -i 4

                Monitor in production:
                $ pm2 monitor

                Make pm2 auto-boot at server restart:
                $ pm2 startup

                To go further checkout:
                http://pm2.io/


                        -------------

[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /root/nodeapp/app.js in fork_mode (1 instance)
[PM2] Done.
?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
? id  ? name   ? namespace   ? version ? mode    ? pid      ? uptime ? ?    ? status    ? cpu      ? mem      ? user     ? watching ?
?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
? 0   ? app    ? default     ? N/A     ? fork    ? 13202    ? 0s     ? 0    ? online    ? 0%       ? 27.2mb   ? root     ? disabled ?
?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

In the above output, you can see very useful information like, application PID, uptime, status, CPU and Memory usage.

Next, run the following command to create and configures a startup script for Node.js application so that your Node.js application will start automatically at boot.

pm2 startup

You should get the following output:

[PM2] Init System found: systemd
Platform systemd
Template
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target

[Service]
Type=forking
User=root
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/root/.pm2
PIDFile=/root/.pm2/pm2.pid
Restart=on-failure

ExecStart=/usr/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/lib/node_modules/pm2/bin/pm2 kill

[Install]
WantedBy=multi-user.target

Target path
/etc/systemd/system/pm2-root.service
Command list
[ 'systemctl enable pm2-root' ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-root.service
[PM2] Making script booting at startup...
[PM2] [-] Executing: systemctl enable pm2-root...
Created symlink /etc/systemd/system/multi-user.target.wants/pm2-root.service → /etc/systemd/system/pm2-root.service.
[PM2] [v] Command successfully executed.
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save

[PM2] Remove init script via:
$ pm2 unstartup systemd

Next, start the PM2 service with the following command:

systemctl start pm2-root

Next, verify the status of PM2 service with the following command:

systemctl status pm2-root

You should get the following output:

? pm2-root.service - PM2 process manager
   Loaded: loaded (/etc/systemd/system/pm2-root.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2020-03-26 06:04:28 UTC; 2s ago
     Docs: https://pm2.keymetrics.io/
  Process: 13387 ExecStart=/usr/lib/node_modules/pm2/bin/pm2 resurrect (code=exited, status=0/SUCCESS)
 Main PID: 13191 (PM2 v4.2.3: God)
    Tasks: 0 (limit: 4915)
   CGroup: /system.slice/pm2-root.service
           ? 13191 PM2 v4.2.3: God Daemon (/root/.pm2)

Mar 26 06:04:28 kolab.linuxbuz.com pm2[13387]: [PM2] Restoring processes located in /root/.pm2/dump.pm2.bak
Mar 26 06:04:28 kolab.linuxbuz.com pm2[13387]: [PM2][ERROR] Failed to read dump file in /root/.pm2/dump.pm2.bak
Mar 26 06:04:28 kolab.linuxbuz.com pm2[13387]: [PM2][ERROR] No processes saved; DUMP file doesn't exist
Mar 26 06:04:28 kolab.linuxbuz.com pm2[13387]: ? PM2+ activated | Instance Name: node.myserver.tld-109e | Dash: https://app.pm2.io/#/r/7p66twg
Mar 26 06:04:28 kolab.linuxbuz.com pm2[13387]: ????????????????????????????????????????????????????????????????????????????????????????????????
Mar 26 06:04:28 kolab.linuxbuz.com pm2[13387]: ? id  ? name   ? namespace   ? version ? mode    ? pid      ? uptime ? ?    ? status    ? cpu   
Mar 26 06:04:28 kolab.linuxbuz.com pm2[13387]: ????????????????????????????????????????????????????????????????????????????????????????????????
Mar 26 06:04:28 kolab.linuxbuz.com pm2[13387]: ? 0   ? app    ? default     ? N/A     ? fork    ? 13202    ? 8m     ? 0    ? online    ? 0%    
Mar 26 06:04:28 kolab.linuxbuz.com pm2[13387]: ????????????????????????????????????????????????????????????????????????????????????????????????
Mar 26 06:04:28 kolab.linuxbuz.com systemd[1]: Started PM2 process manager.

Configure Nginx as a Reverse Proxy for Node.js

At this point, your Node.js application is running and listening on the localhost. Next, you will need to configure Nginx as a front-end proxy server for Node.js application so that any user can access your application over the internet.

First, install the Nginx web server with the following command:

apt-get install nginx -y

Once installed, create an Nginx virtual host configuration file for Node.js application:

nano /etc/nginx/sites-enabled/myapp.conf

Add the following content:

server {
    listen 80;
    server_name node.myserver.tld;
    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
     }
}

Save and close the file when you are finished. Then, restart the Nginx server to apply the changes:

systemctl restart nginx

You can verify the status of Nginx service with the following command:

systemctl status nginx

You should see the following output:

? nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2020-03-25 12:42:49 UTC; 5min ago
     Docs: man:nginx(8)
  Process: 26180 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 26182 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 26183 (nginx)
    Tasks: 3 (limit: 2359)
   Memory: 5.5M
   CGroup: /system.slice/nginx.service
           ??26183 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           ??26184 nginx: worker process
           ??26185 nginx: worker process

Mar 25 12:42:49 debian10 systemd[1]: Starting A high performance web server and a reverse proxy server...
Mar 25 12:42:49 debian10 systemd[1]: Started A high performance web server and a reverse proxy server.

Once you are finished, you can proceed to the next step.

Access Your Node.js Application

At this point, Nginx is configured to serve your Node.js application.

Now, open your web browser and type the URL http://node.myserver.tld. You should see your Node.js application in the following screen:

Node.js page

Conclusion

In the above article, you learned how to set up Node.js application on Debian 10. You also learned how to run Node.js application as a daemon and configure Nginx as a reverse proxy to serve the application. I hope you can now easily host your Node.js application in a production environment.

Share this page:

Suggested articles

0 Comment(s)

Add comment