[英]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 Studio
与Node.JS Tools
来为项目开发此Web服务。
I built the Web Service using express
, node-rest-client
and mysql
modules. 我使用
express
, node-rest-client
和mysql
模块构建了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
上部署WCF
和Web 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或类似的配置管理系统来自动化。
git archive
or by checking out the tag you want to build git archive
或通过签出要构建的标签来完成 npm install --production
tar
, npm pack
, or zip
tar
, npm pack
或zip
的构建中 scp
and extract it into place. scp
将其传输到您的服务器,并将其提取到位。
/opt/myapp
is the recommended place to install it /opt/myapp
是建议安装的位置 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是一个流程管理器,负责以下工作:
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时,可能的工作流程为:
$ (sudo) npm install -g pm2
$ (sudo) npm install -g pm2
$ (sudo) pm2 startup
$ (sudo) pm2 startup
$ pm2 start /home/centos/app.js
$ pm2 start /home/centos/app.js
$ pm2 save
$ 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:
我认为,在以下三种情况下,这可能是一个好主意:
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.