Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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& 
like image 685
Roman Podlinov Avatar asked Jul 01 '14 17:07

Roman Podlinov


People also ask

How do I run a node js in production environment?

You can signal Node. js that you are running in production by setting the NODE_ENV=production environment variable. in the shell, but it's better to put it in your shell configuration file (e.g. . bash_profile with the Bash shell) because otherwise the setting does not persist in case of a system restart.

CAN node js be used in production?

Node. JS is ideal for fast, lightweight, real-time web applications such as audio/video streaming, browser games, chats, collaboration tools social media, time trackers, and much more. For this reason, many companies decide to use Node. js in production.


1 Answers

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

like image 150
Roman Podlinov Avatar answered Oct 04 '22 06:10

Roman Podlinov