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

问题描述:

MEAN.JS 堆栈提出了"grunt build"任务,用于为生产准备应用程序. 不幸的是,缺少有关后续步骤的信息.实际上,目前尚不清楚如何将应用程序部署到生产环境以及如何启动它.

MEAN.JS stack proposes the "grunt build" task for preparing application to Production. 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.

问题#1 除了对config/env/production.js进行更改之外,还必须在项目中配置什么?例如.如何使用自定义字体?

Question #1 What must be configured in the project in addition to changes in the config/env/production.js? E.g. how to work with custom fonts?

问题2 好的.将代码部署到生产环境(通过Git,rsync等).是否足以将其运行为

Question #2 Ok. The code deployed to Production (via Git, rsync, etc). 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. 仔细检查/config/env/production.js配置文件,并包括您手动添加到/config/env/all.js的所有资产.这是在生产中使用缩小版本的好方法.

  1. Double check the /config/env/production.js config file and include all assets which you added manually into /config/env/all.js. It's a good approach to use minified versions for production.

(可选),如果您的应用中有自定义字体,我建议您更新gruntfile.js并添加一个将字体复制到/public/dist/文件夹中的任务.我做了以下更改:

(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. 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']);

复制任务需要安装grunt-copy模块

The copy task requires to install grunt-copy module

现在是时候制作单个application.js,application.min.js&用于生产的application.min.css文件(请参见/public/dist文件夹).在应用程序文件夹中运行

Now it's time to make single application.js, application.min.js & application.min.css files for production (see the /public/dist folder). Run in the app folder

$grunt build

  • 将文件复制到生产服务器.我更喜欢使用GIT推送部署.它仅向服务器发送增量更改.如果您使用GIT进行推送部署,则需要将"/public/dist/"文件夹中的所有文件添加到存储库中.

  • Copy files to production server. I prefer to use GIT push deployment. 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.

    由于您在项目中使用express.js,因此足以使用命令

    Because you use express.js in your project it's enough to use command

    $NODE_ENV=production node server.js&
    

    我知道有些开发人员永远使用 (node.js的模块),但是我更喜欢使用UPSTART(基于事件的初始化守护程序),默认情况下它在Ubuntu上作为系统服务提供.我在/etc/init文件夹中创建一个配置文件,例如/etc/init/my-app.conf.之后,我可以启动,停止,重新启动我的应用程序作为服务.例如. 服务my-app开始|停止|重新启动

    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. I create a config file in /etc/init folder, e.g. /etc/init/my-app.conf. After this I can start, stop, restart my app as a service. E.g. service my-app start|stop|restart

    如果发生故障,UPSTART将监视并重新启动您的服务(请参阅UPSTART配置中的respawn命令). 您可以在此处找到有关如何制作UPSTART配置文件的详细答案: node.js

    The UPSTART will monitor and restart your service in case of failure (see respawn command in the UPSTART config). You can find detailed answer how to make UPSTART config file here: upstart script for node.js

    (可选,但建议)将nginx安装在Node.js的前面.出于安全原因,建议在非特权用户下运行Web应用程序,另一方面,如果您想为Web应用程序使用端口80(这是http协议的默认端口)(例如 http://my-app-domain.com:3000/),这样的配置可能比较棘手.因此,我在网络应用程序的前面使用了Nginx(端口80),该端口实际上可用于非特权用户可用的端口(例如3000)

    (Optional, but recommended) Install nginx in front of Node.js. 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 (e.g. http://my-app-domain.com/ instead of http://my-app-domain.com:3000/) such configuration may be tricky. Because of this I use Nginx (port 80) in front of my web app which actually works on the port available for unprivileged user (e.g. 3000)

    6a. 您可以使用Varnish代替Nginx