Running mongodb in production - node.js

I am currently building a web app using express.js and mongodb. My problem is I do not know how to run mongodb itself in production. Because in the development, I used mongod command, but it need the terminal to be open so mongodb will run. How can I do this in the real server? In my express app, I just use pm2 to run it.

Since you are not an expert on managing this, I would suggest you to use some mongodb-as-a-service, so that they manage that for you and you just simply access it. for example: https://mongolab.com/
Or you can use https://www.mongodb.com/cloud so that you can just lunch a few commands in your server and mongodb+backup will be installed in your server and you can update, or make clusters simply using their interface.
Or if you want to install it in your server, guessing you are using linux, and probably an ubuntu based distro you can run mongod like #aiobe suggested: sudo service mongod start

Related

How to properly run NodeJs on Windows Server in production

I am having difficult times, trying to make my NodeJS scripts run on windows server 2012. Or more precisely, to make it robust.
I have installed PM2, whic his great, also added service for windows startup which works fine, but now I found biggest issue I can't solve.
When windows server user start pm2 start, directly on server or through ssh, when logging out, all pm2 scripts are gone.
I've tried to look into pm2-windows-service but that seems inconsistent, when I restart service, it works fine, but sometimes I need to manually reload o restart only 1 script and then whole list of pm2 scripts gets somehow detached or attached to user, so when I log out from server it's all gone again.
I can't find solution to have watcher/autorestart on scripts, and make them run as a service regardless of user being logged in/out.
There must be solution for running multiple nodejs scripts on windows ?
Once you launch your NodeJS processes, you should run pm2 save command. That way, your stated scripts are saved.
Your attached/dettached problems may be due a relative path configuration in the service env variables. Try moving to an absolute path.
Note: PM2 recommendation is to launch commands with an admin-privileged user; otherwise inconsistences may appear.
Note (2): Bare in mind that PM2 on Windows has some issues. For example, if you restart your server, most of the times processes dissapear from pm2 ls (they noted that Windows does not have the feature to restore saved instances). If you saved them, pm2 resurrect will restore them.

How to run React Web Application forever

I have created a web application using React framework. I am able to Run it in localhost using npm start command.
I have a server in AWS ubuntu 14.04. I have uploaded all code there and run the application using npm command. It is working well. But if i close the terminal then it has been stopped. I know we need to run that instance or service. But how can we do it forever without any UI of ubuntu?
Did you link the public directory's file(HTMl/PHP file) to your dedicated host server?
If not, Look to do how to setup it with AWS in local you can do it with virtual hosting. I'm sure in AWS there'll be any same way to do.
You can use a library called forever which will keep the process running in the background.
https://github.com/foreverjs/forever

Nodejs and raw-socket package

I have a nodejs application that should start when I turn on the machine (I'm using Linux Ubuntu 16.04 so I put it in the rc.local). my application uses the raw-socket package.
Since my application cannot run as root, in the rc.local, I set:
sudo -u myuser node myapp.js &
when my application calls "new raw-socket", I got an error:
Operation not permitted.
I tried different methods such as "setcap" but they don't work for me.
Furthermore, if i try to run my app after the operating system is loaded, the app works perfectly without any problem....
should I have to add something in the rc.local....did I miss something?
node-raw-socket needs root access to create its raw sockets.
You will have to run
sudo node myapp.js

Run nodejs app on start Windows

I have a nodejs app which should start running when windows starts. As I'm new to node and have never done anything similar I don't know how to do it. I have been reading some articles from several webs that use AdvancedInstaller or other software but don't know if there are any ways to do it without using other software. Anyone can help?
You need to use pm2
There are other packages aimed at making pm2 a windows service:
pm2-windows-startup
pm2-windows-service
It seems like you need to create *.sh script and put in the Startup folder
maybe this can help you
or just google sh script startup nodejs windows

nodejs app with forever in cronjob does not work after reboot

I created a cronjob with sudo crontab -u USERNAME -e and tried to start my node.js app after each reboot.
It had worked well at another server.
But this time, I installed the Node.js via nvm
Then tried to run the same cronjob lines, but every time after reboot it failed. I tried to figure out all the related folders and tried again to run as different lines. When I check with forever list, server shows that no process is running.
#reboot /home/USERNAME/.npm/forever start -c /home/USERNAME/.nvm/current/bin/node /home/USERNAME/APPNAME/app
#reboot /home/USERNAME/.npm/forever start /home/USERNAME/APPNAME/app
#reboot /usr/local/bin/forever start -c /home/USERNAME/.nvm/current/bin/node /home/USERNAME/APPNAME/app
I think that the reason is somehow related with nvm. But I am not sure of that. I don't want to do anything unnecessary unless I am fully sure about it.
Edited July 26, 2015
Though I have used the npm's forever module to deploy nodejs production apps, I really do not see the need to use it on linux based servers as there are so many system level alternatives available.
One of them is upstart. It will help you run your scripts as system level services. Amazon Web Services also use upstart in their Elastic Beanstalk nodejs tiers to keep nodejs apps running forever.
If you really just need to get down to it, here is a link to run your nodejs app as an upstart service.
However, it is not limited to deploying nodejs apps only and if you learn upstart, you will be able to do a lot of things with it. Here is a link for that as well.
If your original approach was preferable (as it was in my case) you can fix your scheduled cron job by explicitly passing the path to node like this: (found in this answer)
#reboot /root/.nvm/versions/node/v7.1.0/bin/node /root/.nvm/versions/node/v7.1.0/bin/forever start /var/www/server.json
Apparently NVM works in magic by setting up some system paths which aren't setup at the moment the cron jobs run.

Resources