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

20

MEAN.JS技术栈提供了"grunt build"任务来准备应用程序进行生产环境部署。不幸的是,关于接下来的步骤缺乏信息。实际上,如何部署应用程序到生产环境以及如何启动它还不清楚。

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

问题#2 好的,代码通过Git、rsync等方式部署到生产环境。仅运行它是否足够?

 $NODE_ENV=production node server.js& 

3
如果您投反对票,请留下评论;这是 StackOverflow 上的常见做法。 - Roman Podlinov
3
你好Roman,我不理解这个负面投票。我只是尝试使用mean.js,并且想知道如何将其部署到生产服务器上。你最终是怎么做的呢? - jjimenez
@jjimenez 你好。上面的评论是针对负评者的。我现在正在为你写答案。 - Roman Podlinov
1个回答

35

我建议按照以下步骤部署到生产环境:

  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.

  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. 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

  3. 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
    
  4. 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.

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

    $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. 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

    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

  6. (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. Instead of Nginx you can use Varnish


感谢您的回复Roman。我明白为了服务于express应用程序,您需要运行node。但是对于angular应用程序,通过像apache或nginx这样的Web服务器提供html和js文件就足够了。以您解释的方式(我怀疑这是“official”mean.js的方式),您正在通过node提供静态文件。这有效吗?我的意思是,您不是给node更多的“不必要”的工作吗? - jjimenez
@jjimenez 实际上我漏掉了一个要点。问题在于你需要配置Node.js响应端口80(http),但所有低于1024的端口都属于root。因此,这是一个棘手的问题(在常规非特权用户下使用端口80并运行Web应用程序)。我使用Nginx来解决这个问题。据我所知,这是一种常见的方法。 - Roman Podlinov
看起来 "grunt build" 只打包客户端将使用的 js 和 css。难道没有一种干净地为服务器打包文件的方法吗?理想情况下,我只想发送必要的文件并对它们进行缩小。 - Jim B.
@JimBaldwin,你可以通过 Grunt 来完成。 - Roman Podlinov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接