20

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.

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?

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& 
Roman Podlinov
  • 23,806
  • 7
  • 41
  • 60
  • 3
    Can you please left a comment if vote negative; it's a common practice here on StackOverflow – Roman Podlinov Jul 02 '14 at 05:32
  • 3
    Hi Roman, I don't understand the negative vote. I am just giving a try to mean.js, and I'm also wondering how to deploy it to a production server. How did you do that finally? – jjimenez Jul 24 '14 at 13:31
  • @jjimenez Hi. The comment above is for negative voters. I'm writing an answer for you right now – Roman Podlinov Jul 24 '14 at 13:34

1 Answers1

35

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

Community
  • 1
  • 1
Roman Podlinov
  • 23,806
  • 7
  • 41
  • 60
  • Thanks for your response Roman. I understand that in order to serve the express application you need to run node. But for the angular app, it would be enougth to serve the html and js files through a web server, like apache or nginx. In the way you explained (and I suspect that is the "official" mean.js way), you are serving the static files through node. Is that efficient? I mean, aren't you giving more "unnecessary" work to node? – jjimenez Jul 24 '14 at 14:08
  • @jjimenez Actually I left one point. The issue is you need to configure Node.js to response on port 80 (http), but all ports below 1024 belongs to root. So it's a tricky to do it ( to use port 80 and run you web app under regular unprivileged user). I use Nginx for this purpose. As I know it's common approach. – Roman Podlinov Jul 25 '14 at 08:15
  • It looks like "grunt build" only packages the js and css that will be used by the client. Is there no way to package the files cleanly for the server? Ideally, I'd like to only ship the necessary files and minify them as well. – Jim B. Nov 16 '14 at 01:55
  • @JimBaldwin you can do it via Grunt. – Roman Podlinov Jan 19 '15 at 08:57