Home / How To / How to install Node.js on Debian 10 and configure Nginx as a Frontend proxy server

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 was built on Chrome V8 JavaScript engine and can run JavaScript code outside of a browser. It can run on several operating systems including Linux, FreeBSD, Windows and macOS. Node.js also works on other web servers such as Apache and Nginx.

In this tutorial, we will teach you how to install and configure Node.js to manage backend requests and configure Nginx to handle front-end requests on Debian 1

0. [19659003] Prerequisites
  • A server that run Debian 10.
  • A root password is configured on your server.

Getting Started

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

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

After 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 repository. So you have 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 - 

When the installation is complete, you can install Node.js with the following command:

  apt-get install nodejs -y 

When the installation is complete, you can verify the Node .js version with the following command: [19659007] 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 would see the NPM version in the following output:

  6.13.4

Creating Node.js Application

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

  mkdir nodeeapp 

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

  cd nodeeapp 
npm install express

Next, create an example on Node.js application with express with nano editor:

  nano app.js 

Add following content:

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

Save and close the file when you are done.

Then run your application with the following command:

  node app.js 

Next, open another 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 the curl command shown below:

  curl http://127.0.0.1:8000 Chapter19659009 ?? If all is well, you should get the following output: 

  Nginx as Frontend Server for Node.js!

Installing and Configuring PM2

PM2 is a process manager for managing the Node.js application. YOu can easily start, stop and enable the Node.js application to start at PM2 startup.

Install the latest version of PM2 with NPM as below:

  npm install pm2-g 

Once installed, change the directory to the node app and your application with the following command: Ads

  cd node app 
pm2 start app.js [19659009] Once the application is 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 remove all programs:
$ 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 startup:
Starting at $ pm2

To proceed to checkout:
http://pm2.io/


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

[PM2] Playing PM2 demon with pm2_home = / root / .pm2
[PM2] PM2 was successfully dismantled
[PM2] Start /root/nodeapp/app.js in fork_mode (1 instance)
[PM2] Issued.
?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????
? id? name? namespace? version? bearing? pid? uptime? ? ? status? CPU? meme? user? watching ?
?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????
? 0? app? default? N / A? fork? 13202? 0s? 0? online    ? 0%? 27.2 MB? root? Disabled?
?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????

In the above output you can see very useful information such as, PID application, uptime, status, CPU and memory usage.

Then run the following command to create and configure a Node.js application startup script so that your Node.js application will start automatically at startup.

  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-error

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

[Install]
WantedBy = multi-user.target

Målväg
/etc/systemd/system/pm2-root.service
command List
[ 'systemctl enable pm2-root' ]
[PM2]   Writing init configuration in /etc/systemd/system/pm2-root.service
[PM2] Do script start at start ...
[PM2] [-]   Execution: 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 at restart via:
$ pm2 save

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

Then start the PM2 service with the following command:

  systemctl start pm2-root 

Next, verify the status of the 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 setting: enabled)
Active: active (running) since Thu 2020-03-26 06:04:28 UTC; 2s ago
Document: https://pm2.keymetrics.io/
Process: 13387 ExecStart = / usr / lib / node_modules / pm2 / bin / pm2 resume (code = terminated, status = 0 / SUCCESS)
Main PID: 13191 (PM2 v4.2.3: God)
Details: 0 (limit: 4915)
CGroup: /system.slice/pm2-root.service
? 13191 PM2 v4.2.3: God Daemon (/root/.pm2)

26 Mar 06:04:28 kolab.linuxbuz.com pm2 [13387]: [PM2] Recovery processes in /root/.pm2/dump.pm2.bak
Mar 26 06:04:28 kolab.linuxbuz.com pm2 [13387]: [PM2][ERROR]   Could not read dump file in /root/.pm2/dump.pm2.bak
26 Mar 06:04:28 kolab.linuxbuz.com pm2 [13387]: [PM2][ERROR]   No processes saved; DUMP file does not exist
26 Mar 06:04:28 kolab.linuxbuz.com pm2 [13387]:? PM2 + enabled | Instance Name: node.myserver.tld-109e | Track: https://app.pm2.io/#/r/7p66twg
26 Mar 06:04:28 kolab.linuxbuz.com pm2 [13387]: ????????????????????????????????? ?????????????????????????????????????????????????? ?????????????
26 Mar 06:04:28 kolab.linuxbuz.com pm2 [13387]:? id? name? namespace? version? bearing? pid? uptime? ? ? status? CPU
26 Mar 06:04:28 kolab.linuxbuz.com pm2 [13387]: ????????????????????????????????? ?????????????????????????????????????????????????? ?????????????
26 Mar 06:04:28 kolab.linuxbuz.com pm2 [13387]:? 0? app? default? N / A? fork? 13202? 8m? 0? online    ? 0%
26 Mar 06:04:28 kolab.linuxbuz.com pm2 [13387]: ????????????????????????????????? ?????????????????????????????????????????????????? ?????????????
26 Mar 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 runs and listens to localhost. Then you need to configure Nginx as a front-end proxy server for Node.js application so that all users can access your application over the Internet.

First install the Nginx web server with the following command:

  apt-get install nginx-y 

Create an Nginx virtual host configuration file when it is installed for Node.js application: Ads

[19659008] nano /etc/nginx/sites-enabled/myapp.confebrit19659009??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 done. Then restart the Nginx server to apply the changes:

  systemctl restart nginx 

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

  systemctl status nginx 

You should see the following output: [19659025]? nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor setting: enabled)
Active: active (running) since Tue 2020-03-25 12:42:49 UTC; 5 minutes ago
Document: man: nginx (8)
Process: 26180 ExecStartPre = / usr / sbin / nginx-t -q -g daemon on; master_process on; (code = terminated, status = 0 / SUCCESS)
Process: 26182 ExecStart = / usr / sbin / nginx -g daemon on; master_process on; (code = terminated, status = 0 / SUCCESS)
Main PID: 26183 (nginx)
Details: 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

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

When you're done, you can move on to the next step.

Access your Node.js application

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

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

 Node.js page

Conclusion

In the above article you learned how to set Node.js application on Debian 10. You also learned how to run the Node.js application as a demon 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.


Source link