The Angular session is killed after the putty session timeout - node.js

I just want to run the Angular to run forever until I kill it manual So I just used the below command to run it as service in linux box
nohup ng serve --host {xyz.com} &
It will make the application Up and running and created nohup.out file but the session is gone as soon as the putty is time out.
Can anyone lead me to achieve this?

You shouldn't be using ng serve in production, what you need to do is build your Angular App and use something like proxy_pass in a real server (like nginx or apache) to tell it to serve your Angular static app files (index.html + js bundles)
You are taking a big risk by running the app with ng serve on a server as the http server behind it is not secure !

It can be good idea to use production ready tools like systemd to run your nodejs applicaiton - this manual can help:
https://nodesource.com/blog/running-your-node-js-app-with-systemd-part-1/
In your case, if your ng app is saved in /opt/app directory, unit file will be something like this
[Unit]
Description=hello_env.js - making your environment variables rad
Documentation=https://example.com
After=network.target
[Service]
Environment=NODE_PORT=3001
Type=simple
User=ubuntu
Workdir=/opt/app
ExecStart=/usr/bin/node /opt/app/.bin/ng serve --host {xyz.com}
Restart=on-failure
[Install]
WantedBy=multi-user.target
in 2nd part (https://nodesource.com/blog/running-your-node-js-app-with-systemd-part-2/), they explained how to start few instances of application and use nginx in front of them as load balancer and reverse proxy with HTTPS support

Related

NextJS Deployment - How can I simply deploy NextJS like NodeJS on Ubuntu server?

To deploy NodeJS, I use systemd and configure /lib/systemd/system/[service_name].service file
========================================
[Unit]
Description=Inventory Management System
Documentation=PLM Inventory Management Documents
After=network.target
[Service]
Environment=NODE_PORT=3000
Type=simple
User=superuser
WorkingDirectory=/home/superuser/inventory
ExecStart=/home/superuser/.npm-global/bin/nodemon --exec babel-node /home/superuser/inventory/server/server.js
Restart=on-failure
[Install]
WantedBy=multi-user.target
========================================
How can I deploy NextJS by systemd on ubuntu server?
Thanks in advance.
I am running it with the following unit file:
[Unit]
Description=NodeJS server, NextJS public frontend
After=network.target
[Service]
Type=simple
User=someuser
Group=someuser
Restart=on-failure
RestartSec=10
WorkingDirectory=/some/dir/
ExecStartPre=/usr/bin/npm install
ExecStartPre=/usr/bin/npm run build
ExecStart=/usr/bin/npm run start
[Install]
WantedBy=multi-user.target
The WorkingDirectory should point to the directory, where your NextJS project is.
npm install installs the dependencies in node_modules (duh).
npm run build compiles some stuff, that ends up as static/
npm run start starts the NodeJS server.
A lot of people suggest using pm2, but i am not a fan of it.
If you are using CDI, you can get the npm run build and npm install into the build process, and then extract the artifacts (the file/folders mentioned below).
Considering the way NextJS works, you should probably only need the following files and folders: node_modules/ static/ package.json package-lock.json next.config.js.
Although, this too depends on your project.
Ofcourse, this is not nearly enough for production servers.
Especially, since you will need to setup an webserver like Nginx or Apache, in order to do the location Proxy Pass to your NextJS application.
This is because, the NextJS application is running under Localhost:PORT.
Source: painful experience with this.

Is there a node forever equivalent for yarn?

Right now I'm using screen in order to run my command yarn run dev-server.
I'm looking for an equivalent to something like:
forever start app.js
Except for yarn run dev-server.
dev-server in package.json is = "dev-server": "webpack-dev-server"
As everyone knows if the program crashes in screen it doesn't automatically restart.
Side note, it's a react web framework.
On an Ubuntu server, I would recommend to use systemd for running your NodeJs application.
This article has some examples how you can write a systemd unit file. If the process dies, systemd will detect it and automatically restart it.
Untested (but to sketch the idea):
[Unit]
Description=Example server
[Service]
WorkingDirectory=/path/to/my/server
ExecStart=/usr/bin/yarn run dev-server
[Install]
WantedBy=multi-user.target
Although running it through yarn is a bit weird. In a production setup, I have not seen it. Normally, you execute the server by directly running it with node (e.g., node server.js).
You can use PM2
PM2 is a daemon process manager that will help you manage and keep your application online 24/7
And for the use with yarn
pm2 start yarn --interpreter bash --name api -- start

What is the best way to run a Node.js script as service in Ubuntu?

I have a Node.js script that keeps my MongoDB database and the CRM database synced in real-time.
I want to run this script as a background task on my Ubuntu server. I found this solution, but it doesn't work for me. Is there another approach to reach this?
If you just want to start your application, you could use Forever or PM2 for running and auto-restarting on crash. However, this is not a background task.
For a background task that starts on server reboot, the post you linked is the right way to go. If it didn't work, maybe this article will help you. This is from official Express site: Process managers for Express apps
Basically, you create
[Unit]
Description="My Express App"
[Service]
ExecStart=/usr/bin/node server.js
WorkingDirectory=/project/absolute/path
Restart=always
RestartSec=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=MyApp
Environment=NODE_ENV=production PORT=8080
[Install]
WantedBy=multi-user.target
Into a /etc/systemd/system/my-app.service file and then use systemctl to start it:
systemctl enable my-app.service
systemctl start my-app.service
Now this assumes your Linux distribution works with systemctl. If your Linux distribution works with upstart or something else, then you need to google up the instruction for that process manager.

Node.js on Plesk not starting

I have installed the Node.js Extension and I can't get my app running. I have a Symfony 4 application and I am using socket.io for socket communication. Now I need to startup socket.js to startup my server. This file is located in the resources/js folder.
So I have setup Node.js as below:
Document Root: /application.domain.com/public
Application Mode: production
Application URL: This site is under development
Application Root: /application.domain.com
Application Startup File: resources/js/socket.js
Custom environment variables:
But when I start the application, it is not working. When I run the socket.js file from CLI (SSH connection) like this
/opt/plesk/node/8/bin/node socket.js
It is working fine. But now I need to keep my SSH connection alive and there is no check if socket.js is still running. I suppose the Node.js extension also takes care for that.
How can I use this Node.js extension to startup up my socket.js? Or how could I do this in another way?
OK, I fixed this another way with Systemd.
Found this on https://www.axllent.org/docs/view/nodejs-service-with-systemd/, so credits to Ralph Slooten.
Create the service file
/etc/systemd/system/nodeserver.service
Content
[Unit]
Description=Node.js Example Server
#Requires=After=mysql.service # Requires the mysql service to run first
[Service]
ExecStart=/usr/local/bin/node /opt/nodeserver/server.js
# Required on some systems
#WorkingDirectory=/opt/nodeserver
Restart=always
# Restart service after 10 seconds if node service crashes
RestartSec=10
# Output to syslog
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodejs-example
#User=<alternate user>
#Group=<alternate group>
Environment=NODE_ENV=production PORT=1337
[Install]
WantedBy=multi-user.target
Enable the service
systemctl enable nodeserver.service
Start the service
systemctl start nodeserver.service

Sails.js with a systemd script

I have managed to get a Sails js application working on a server, currently just running with nohup to keep the service running when the SSH session is ended.
Obviously, this is not a very robust solution. What happens if the app crashes or the server is reset etc? I am using Fedora so I am using systemd.
Here is what I have so far.
ExecStart=/usr/bin/node /home/dashboard-app/app.js
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=dashboard-app
User=***
Group=***
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
The service starts okay but the script does not know about the config files so will default to Sails port 1337. Going to that port on the server will not work either.
I have also got nginx set up to with the port set in the sails config file which works fine but I don't think this will make a difference.
You need to set WorkingDirectory= to the top-level directory of your Node app, wherever that is.
For example:
[Service]
WorkingDirectory=/home/dashboard-app
or for global-installed apps,
[Service]
WorkingDirectory=/usr/lib/node_modules/dashboard-app

Resources