简体   繁体   中英

How to deploy on CentOS 6.6 vps a Node JS app developed with Visual Studio

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.

  1. Get a snapshot of the code into a known state
    • This can be done with git archive or by checking out the tag you want to build
    • To start, you can just use your working copy, but eventually you'll want to build from a pristine place to avoid any artifacts from your development directory leaking into the build unintentionally
  2. Install your production npm dependencies
    • npm install --production
  3. Package the code with the dependencies up into a build with tar , npm pack , or zip
  4. Transfer that to your server via scp and extract it into place.
    • /opt/myapp is the recommended place to install it
  5. Set up a systemd service file (Example below)
  6. Install and configure a reverse proxy such as 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:

  1. Starting Node.js applications
  2. Re-starting them if they crash
  3. Provide monitoring and logging
  4. Start Node.js applications automatically at startup.

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:

  1. Deploy the source code for the application to the server. There are countless options here. Many of them use GIT (by adding a deploy key to the server and checking out the source code there).
  2. Install PM2: $ (sudo) npm install -g pm2
  3. Let PM2 start at boot automatically: $ (sudo) pm2 startup
  4. Start your apps with PM2: $ pm2 start /home/centos/app.js
  5. Dump PM2 config so at boot it will re-load your apps: $ 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:

  1. When your web site has static content, you can use nginx to serve those static files (it's blazing fast!) instead of your Node app, offloading it (remember that Node apps are single-threaded)
  2. If your website uses SSL/TLS, you can terminate SSL/TLS on the reverse-proxy, and connect to the Node app with a simple HTTP (without SSL/TLS) request. This should improve performances, as your workers (the Node apps) do not have to waste precious event loop cycles because of HTTPS
  3. If you want to load balance manually, without using Node.js cluster module. In this case, you launch multiple instances of your Node.js app listening on separate ports, and nginx will do the load balancing.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM