It's my first take on Node JS
and since my background is in .NET technologies I used Visual Studio
with Node.JS Tools
to develop this Web Service for a project.
I built the Web Service using express
, node-rest-client
and mysql
modules. The WS works fine when tested in my environment and it's now time to deploy it to production on a CentOS 6.6 VPS I own, possibly taking advantage of systemd
.
I am used to deploy WCF
and Web API
Web Service on IIS
, and the process seems to be quite different in this case. I tried googling, but the answers and tutorials don't seem to be straightforward and sometimes even contraddicting.
Is there some additional step to be done to deploy a project created in Visual studio to a *nix based system? How should I procede to deploy and (possibly) have it run with systemd
?
At a high level, the basic steps for a deployment are below. You can start doing these manually and understanding how they work. Eventually you'll want these mostly automated via scripts or a configuration management system such as ansible or similar.
git archive
or by checking out the tag you want to build npm install --production
tar
, npm pack
, or zip
scp
and extract it into place.
/opt/myapp
is the recommended place to install it nginx
More details can be found by reading through the build script I use for my web site . Another good thing to study for prior art is the heroko node.js build pack which is what heroku does to build your app when you push to the heroku git remote.
Those are the high level steps. There are a bunch of subtleties in the details, of course, which you can deal with as you gain some experience.
Example systemd service file. Install to /etc/systemd/system/mynodeapp.service
and run systemctl daemon-reload
then systemctl start mynodeapp
.
[Unit]
Description=My node.js App
[Service]
User=mynodejsapp
Group=mynodejsapp
WorkingDirectory=/opt/mynodejsapp
EnvironmentFile=/etc/mynodejsapp/config
Environment=NODE_ENV=production
ExecStart=/usr/bin/node cluster.js
Restart=always
[Install]
WantedBy=multi-user.target
A great tool to deploy Node.js applications in production is PM2 .
PM2 is a process manager that takes care of:
PM2 itself is written in Node.js, and can setup systemd/init.d/upstart for you. Also, it ensures that your apps are running with restricted permissions (not as root, as system.d may do by default).
When using PM2, a possible workflow would be:
$ (sudo) npm install -g pm2
$ (sudo) pm2 startup
$ pm2 start /home/centos/app.js
$ pm2 save
PM2 has plenty of options, and can also create clusters for you too!
Some people like to have a reverse-proxy (nginx) in front of Node.js applications. That can be a great idea, in my opinion, in three cases:
In other cases, it's not required for you to use a reverse-proxy.
Further reading: - PM2 Basic README: https://github.com/Unitech/PM2/blob/master/README.md - PM2 Advanced README: https://github.com/Unitech/PM2/blob/master/ADVANCED_README.md
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.