简体   繁体   English

如何将MEAN.js(Node.js)应用程序部署到生产环境

[英]How to deploy MEAN.js (Node.js) application to Production environment

MEAN.JS stack proposes the "grunt build" task for preparing application to Production. MEAN.JS堆栈提出了为生产准备应用程序的“grunt构建”任务。 Unfortunately there is a lack of information about next steps. 不幸的是,缺乏有关后续步骤的信息。 Actually it's not clear how to deploy the application to production and how to launch it. 实际上,尚不清楚如何将应用程序部署到生产以及如何启动它。

Question #1 What must be configured in the project in addition to changes in the config/env/production.js? 问题#1除了config / env / production.js的变化之外,项目中必须配置什么? Eg how to work with custom fonts? 例如,如何使用自定义字体?

Question #2 Ok. 问题#2好的。 The code deployed to Production (via Git, rsync, etc). 代码部署到Production(通过Git,rsync等)。 Is it enough to run it as 运行它是否足够了

 $NODE_ENV=production node server.js& 

I recommend to do the following steps for deployment to production environment: 我建议执行以下步骤以部署到生产环境:

  1. Double check the /config/env/production.js config file and include all assets which you added manually into /config/env/all.js . 仔细检查/config/env/production.js配置文件,并将您手动添加的所有资产包含到/config/env/all.js It's a good approach to use minified versions for production. 这是使用缩小版本进行生产的好方法。

  2. (Optional) If you have custom fonts in your app I do recommend to update gruntfile.js and add a task which will copy fonts into /public/dist/ folder. (可选)如果你的应用程序中有自定义字体,我建议更新gruntfile.js并添加一个将字体复制到/ public / dist /文件夹的任务。 I did the following changes: 我做了以下更改:

     copy: { main:{ expand: true, flatten: true, src: ['public/modules/core/fonts/*'], dest: 'public/dist/', filter: 'isFile' } }, ... // Build task(s). grunt.registerTask('build', ['lint', 'loadConfig', 'ngmin', 'uglify', 'cssmin', 'copy']); 

    The copy task requires to install grunt-copy module 复制任务需要安装grunt-copy模块

  3. Now it's time to make single application.js, application.min.js & application.min.css files for production (see the /public/dist folder). 现在是时候制作单个application.js,application.min.js和application.min.css文件进行生产(参见/public/dist文件夹)。 Run in the app folder 在app文件夹中运行

     $grunt build 
  4. Copy files to production server. 将文件复制到生产服务器。 I prefer to use GIT push deployment. 我更喜欢使用GIT推送部署。 It sends to server only incremental changes. 它仅向服务器发送增量更改。 If you use GIT for the push-deployment it needs to add all files from `/public/dist/' folder into the repository. 如果您使用GIT进行推送部署,则需要将`/ public / dist /'文件夹中的所有文件添加到存储库中。

  5. Because you use express.js in your project it's enough to use command 因为你在项目中使用express.js就足以使用命令了

     $NODE_ENV=production node server.js& 

    I know some developers use forever (module for node.js), but I prefer to use UPSTART (event-based init daemon) which available by default on Ubuntu as system service. 我知道一些开发人员永远使用(node.js的模块),但我更喜欢使用UPSTART(基于事件的init守护进程),它默认在Ubuntu上可用作系统服务。 I create a config file in /etc/init folder, eg /etc/init/my-app.conf . 我在/etc/init文件夹中创建了一个配置文件,例如/etc/init/my-app.conf After this I can start, stop, restart my app as a service. 在此之后,我可以启动,停止,重新启动我的应用程序作为服务。 Eg service my-app start|stop|restart 例如,服务my-app开始|停止|重启

    The UPSTART will monitor and restart your service in case of failure (see respawn command in the UPSTART config). 如果发生故障,UPSTART将监控并重新启动您的服务(请参阅UPSTART配置中的respawn命令)。 You can find detailed answer how to make UPSTART config file here: upstart script for node.js 您可以在此处找到有关如何制作UPSTART配置文件的详细解答: node.js的upstart脚本

  6. (Optional, but recommended) Install nginx in front of Node.js . (可选,但建议)在Node.js前面安装nginx It's recommended to run you web app under unprivileged user due to security reasons, on the other hand if you want to use port 80 (it's a default port for the http protocol) for your web app (eg http://my-app-domain.com/ instead of http://my-app-domain.com:3000/ ) such configuration may be tricky. 由于安全原因,建议在非特权用户下运行您的Web应用程序,另一方面,如果您想为您的Web应用程序使用端口80(它是http协议的默认端口)(例如http:// my-app-) domain.com/而不是http://my-app-domain.com:3000/ )这样的配置可能很棘手。 Because of this I use Nginx (port 80) in front of my web app which actually works on the port available for unprivileged user (eg 3000) 因此,我在我的网络应用程序前面使用Nginx(端口80),它实际上可用于非特权用户可用的端口(例如3000)

    6a. 6A。 Instead of Nginx you can use Varnish 而不是Nginx你可以使用Varnish

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

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