简体   繁体   English

如何在CentOS 6.6 vps上部署使用Visual Studio开发的Node JS应用

[英]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. 这是我对Node JS第一次Node.JS Tools ,由于我的背景是.NET技术,因此我将Visual StudioNode.JS Tools来为项目开发此Web服务。

I built the Web Service using express , node-rest-client and mysql modules. 我使用expressnode-rest-clientmysql模块构建了Web Service。 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 . 在我的环境中进行测试时,WS可以很好地工作,现在可以将其部署到我拥有的CentOS 6.6 VPS上的生产环境中,可能是利用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. 我习惯于在IIS上部署WCFWeb API Web Service,在这种情况下,过程似乎完全不同。 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? 要将Visual Studio中创建的项目部署到基于* nix的系统上,还需要执行其他一些步骤吗? How should I procede to deploy and (possibly) have it run with systemd ? 我应该如何进行部署,并且(可能)与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. 最终,您希望这些脚本大多通过脚本或诸如ansible或类似的配置管理系统来自动化。

  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 这可以通过git archive或通过签出要构建的标签来完成
    • 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依赖项
    • npm install --production
  3. Package the code with the dependencies up into a build with tar , npm pack , or zip 将具有依赖项的代码打包到带有tarnpm packzip的构建中
  4. Transfer that to your server via scp and extract it into place. 通过scp将其传输到您的服务器,并将其提取到位。
    • /opt/myapp is the recommended place to install it /opt/myapp是建议安装的位置
  5. Set up a systemd service file (Example below) 设置系统服务文件(以下示例)
  6. Install and configure a reverse proxy such as nginx 安装和配置反向代理,例如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. 研究现有技术的另一件好事是heroko node.js构建包 ,当您将其推送到heroku git遥控器时,heroku会以此构建您的应用程序。

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 . 安装到/etc/systemd/system/mynodeapp.service并运行systemctl daemon-reload然后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是在生产环境中部署Node.js应用程序的绝佳工具。
PM2 is a process manager that takes care of: PM2是一个流程管理器,负责以下工作:

  1. Starting Node.js applications 启动Node.js应用程序
  2. Re-starting them if they crash 如果它们崩溃,则重新启动它们
  3. Provide monitoring and logging 提供监控和记录
  4. Start Node.js applications automatically at startup. 启动时自动启动Node.js应用程序。

PM2 itself is written in Node.js, and can setup systemd/init.d/upstart for you. PM2本身是用Node.js编写的,可以为您设置systemd / init.d / upstart。 Also, it ensures that your apps are running with restricted permissions (not as root, as system.d may do by default). 此外,它还可以确保您的应用以受限的权限运行(不是以root身份运行,因为system.d在默认情况下可能会这样做)。

When using PM2, a possible workflow would be: 使用PM2时,可能的工作流程为:

  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). 他们中的许多人都使用GIT(通过向服务器添加部署密钥并在其中签出源代码)。
  2. Install PM2: $ (sudo) npm install -g pm2 安装PM2: $ (sudo) npm install -g pm2
  3. Let PM2 start at boot automatically: $ (sudo) pm2 startup 让PM2在启动时自动启动: $ (sudo) pm2 startup
  4. Start your apps with PM2: $ pm2 start /home/centos/app.js 使用PM2启动您的应用程序: $ pm2 start /home/centos/app.js
  5. Dump PM2 config so at boot it will re-load your apps: $ pm2 save 转储PM2配置,以便在启动时将重新加载您的应用程序: $ pm2 save

PM2 has plenty of options, and can also create clusters for you too! PM2有很多选择,还可以为您创建集群

Some people like to have a reverse-proxy (nginx) in front of Node.js applications. 有些人喜欢在Node.js应用程序之前安装反向代理(nginx)。 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) 当您的网站上包含静态内容时,您可以使用nginx来提供这些静态文件(速度非常快!),而不是使用Node应用程序,然后将其卸载(请记住Node应用程序是单线程的)
  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. 如果您的网站使用SSL / TLS,则可以在反向代理上终止SSL / TLS,并通过简单的HTTP(无SSL / TLS)请求连接到Node应用程序。 This should improve performances, as your workers (the Node apps) do not have to waste precious event loop cycles because of HTTPS 这将提高性能,因为您的工作人员(Node应用程序)不必因为HTTPS而浪费宝贵的事件循环周期
  3. If you want to load balance manually, without using Node.js cluster module. 如果要手动负载平衡,而不使用Node.js集群模块。 In this case, you launch multiple instances of your Node.js app listening on separate ports, and nginx will do the load balancing. 在这种情况下,您将启动Node.js应用程序的多个实例,这些实例在单独的端口上侦听,nginx将执行负载平衡。

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 进一步阅读:-PM2基本自述文件: https : //github.com/Unitech/PM2/blob/master/README.md-PM2高级自述文件: https : //github.com/Unitech/PM2/blob/master/ADVANCED_README。 MD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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