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
Related
I have a node.js script in my directory on my raspberry pi /home/pi/my/app/here/app.js
When I navigate using cd my/app/here and run node app.js my script executes without any problems. However, when I try to execute this script from a service in systemd I get an code=exited, status=203/EXEC error.
Here is my service file:
[Unit]
Description=App
After=network.target
[Service]
Type=simple
User=pi
ExecStart=/user/bin/node /home/pi/my/app/here/app.js
WorkingDirectory=/home/pi/my/app/here
Restart=on-failure
[Install]
WantedBy=multi-user.target
This is really my first time using systemd and most of what I have found has been through research online and other SO posts. So I am sure I am missing something pretty silly, but any help on getting my service to run would be much appreciated.
What am I missing here?
Put a hash bang in the file
#!/usr/bin/env node
And just ensure the x bit is set.
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
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.
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
I used systemd (ubuntu 16.0.4 LTS) to start MyServer at service (if reboot or crash ocurred, restart automatically by systemd).
It's work fine but it's never generate logs any more!
My server generate logs at file "MyServerLogs" and if this file not exist, create new one (in same running directory).
Also MyServer watch a directory (that create by itself in same running directory) for file creation. This does not work either!
When MyServer run by systemd service, It can not create file, write to file and watch directory for file creation any more. But why?.
I develop MyServer with c++ and beginner of systemd and unit files.
I put MyServer at:
/usr/bin/MyServer
and my unit file
[Unit]
Description=Virtual Distributed Ethernet
After=syslog.target
After=network-online.target
After=network.target
[Service]
ExecStart=/usr/bin/MyServer
Restart=always
StandardOutput=syslog
[Install]
WantedBy=multi-user.target
Thanks for any help.