{"id":2194,"date":"2016-07-13T13:47:05","date_gmt":"2016-07-13T17:47:05","guid":{"rendered":"https:\/\/www.globo.tech\/learning-center\/?p=2194"},"modified":"2017-12-12T16:12:44","modified_gmt":"2017-12-12T21:12:44","slug":"pm2-nodejs-process-manager-deploy-apps-ubuntu-16","status":"publish","type":"post","link":"https:\/\/www.globo.tech\/learning-center\/pm2-nodejs-process-manager-deploy-apps-ubuntu-16\/","title":{"rendered":"Install and use the PM2 NodeJS to deploy apps in production on Ubuntu 16"},"content":{"rendered":"<h1>Install and use the PM2 NodeJS to deploy apps in production on Ubuntu 16<\/h1>\n<p>NodeJS applications are used in backends all over the web to perform various tasks. But unlike most web software, NodeJS scripts run in the foreground, rather than in the background as a daemon process, which inconveniently requires an open terminal window using a program such as screen or tmux to run on the server.<\/p>\n<p>However, it is possible to convert a NodeJS application to run as a daemon process. PM2 is a production process manager for NodeJS that can be used for this purpose.<\/p>\n<h2>Getting started<\/h2>\n<p>To complete this guide, you will need the following:<br \/>\n\u2022 1 Node (<a href=\"https:\/\/www.globo.tech\/cloud-server-pricing\" target=\"_blank\"><b>Cloud Server<\/b><\/a> or <a href=\"http:\/\/www.globo.tech\/dedicated-server-hosting\" target=\"_blank\"><b>Dedicated Server<\/b><\/a>) running Ubuntu 16.<br \/>\n\u2022 All commands should be run as the root user<br \/>\n\u2022 Do not run NodeJS applications as root in production. To increase security, create a new user specifically for running NodeJS applications.<br \/>\n\u2022 NodeJS\/NPM must be installed.<br \/>\n\u2022 You should have a pre-existing NodeJS application to daemonize.<\/p>\n<h2>Tutorial<\/h2>\n<p>You will first need to install PM2 globally via NPM.<\/p>\n<p><code>npm install -g pm2<\/code><\/p>\n<p>Now you can run your NodeJS application using PM2.<\/p>\n<p><code>cd \/home\/nodeapp<br \/>\npm2 start nodetest.js<\/code><\/p>\n<p>Here is a sample of what the expected output should look like:<\/p>\n<p><code class=\"gris\">root@nodejs:\/home\/nodeapp# pm2 start nodetest.js<br \/>\n[PM2] Spawning PM2 daemon<br \/>\n[PM2] PM2 Successfully daemonized<br \/>\n[PM2] Starting nodetest.js in fork_mode (1 instance)<br \/>\n[PM2] Done.<br \/>\n????????????????????????????????????????????????????????????????????????????????                                                                                                                                                             ?????<br \/>\n? App name ? id ? mode ? pid   ? status ? restart ? uptime ? memory      ? watch                                                                                                                                                             ing ?<br \/>\n????????????????????????????????????????????????????????????????????????????????                                                                                                                                                             ?????<br \/>\n? nodetest ? 0  ? fork ? 26935 ? online ? 0       ? 0s     ? 21.680 MB   ? disab                                                                                                                                                             led ?<br \/>\n????????????????????????????????????????????????????????????????????????????????                                                                                                                                                             ?????<br \/>\n Use `pm2 show <id|name>` to get more details about an app<\/code><\/p>\n<p>PM2 is now keeping track of your application. If it crashes, PM2 will restart it and keep a log of any errors.<\/p>\n<p>PM2 has another feature whereby when the server reboots, PM2 can make sure to start up any applications it manages.<\/p>\n<p>First, let&#8217;s configure the PM2 utility to boot up automatically with your system. Generate and install the PM2 init scripts.<\/p>\n<p><code>pm2 startup ubuntu<\/code><\/p>\n<p><code class=\"gris\">root@nodejs:\/home\/nodeapp# pm2 startup ubuntu<br \/>\n[PM2] Generating system init script in \/etc\/init.d\/pm2-init.sh<br \/>\n[PM2] Making script booting at startup...<br \/>\n[PM2] -enabled- Using the command:<br \/>\n      su -c \"chmod +x \/etc\/init.d\/pm2-init.sh && update-rc.d pm2-init.sh defaults\"<br \/>\n[PM2] Done.Make the PM2 utility boot automatically with your system<\/code><\/p>\n<p>Take note of the command that this outputs. In order for the PM2 utility to start automatically, you will need to run this command as root.<\/p>\n<p><code>su -c \"chmod +x \/etc\/init.d\/pm2-init.sh && update-rc.d pm2-init.sh defaults\"<\/code><\/p>\n<p>Afterwards, verify the status of PM2, and make sure that it has been added to the init portion of your operating system.<\/p>\n<p><code>systemctl status pm2<\/code><\/p>\n<p>This is the output that you should see.<\/p>\n<p><code class=\"gris\">? pm2.service - PM2 next gen process manager for Node.js<br \/>\n   Loaded: loaded (\/etc\/systemd\/system\/pm2.service; disabled; vendor preset: enabled)<br \/>\n   Active: inactive (dead)<\/code><\/p>\n<p>Enable PM2 to boot automatically.<\/p>\n<p><code>systemctl enable pm2<\/code><\/p>\n<p>If you see the following output, then it should be the case that the symlink has been created and that PM2 will load upon system boot.<\/p>\n<p><code class=\"gris\">Created symlink from \/etc\/systemd\/system\/multi-user.target.wants\/pm2.service to \/etc\/systemd\/system\/pm2.service.<\/code><\/p>\n<h2>Conclusion<\/h2>\n<p>Now you will be able to use PM2 to easily manage and automatically restart your NodeJS applications. If this guide was helpful to you, kindly share it with others who may also be interested. <\/p>\n<h2>Quick reference<\/h2>\n<p>Below are a few useful commands that you can use with PM2 to manage the status of an application. For the purpose of example, we are using an application named nodetest.<\/p>\n<p>List applications<\/p>\n<p><code>pm2 list<\/code><\/p>\n<p>Output:<\/p>\n<p><code class=\"gris\">root@nodejs:\/home\/nodeapp# pm2 list<br \/>\n?????????????????????????????????????????????????????????????????????????????????????<br \/>\n? App name ? id ? mode ? pid   ? status ? restart ? uptime ? memory      ? watching ?<br \/>\n?????????????????????????????????????????????????????????????????????????????????????<br \/>\n? nodetest ? 0  ? fork ? 26935 ? online ? 0       ? 24m    ? 31.672 MB   ? disabled ?<br \/>\n?????????????????????????????????????????????????????????????????????????????????????<br \/>\n Use `pm2 show <id|name>` to get more details about an app<\/code><\/p>\n<p>Stop your application<\/p>\n<p><code>pm2 info nodetest<\/code><\/p>\n<p>Output:<\/p>\n<p><code class=\"gris\">root@nodejs:\/home\/nodeapp# pm2 info nodetest<br \/>\n Describing process with id 0 - name nodetest<br \/>\n????????????????????????????????????????????????????????????<br \/>\n? status            ? online                               ?<br \/>\n? name              ? nodetest                             ?<br \/>\n? restarts          ? 0                                    ?<br \/>\n? uptime            ? 25m                                  ?<br \/>\n? script path       ? \/home\/nodeapp\/nodetest.js            ?<br \/>\n? script args       ? N\/A                                  ?<br \/>\n? error log path    ? \/root\/.pm2\/logs\/nodetest-error-0.log ?<br \/>\n? out log path      ? \/root\/.pm2\/logs\/nodetest-out-0.log   ?<br \/>\n? pid path          ? \/root\/.pm2\/pids\/nodetest-0.pid       ?<br \/>\n? interpreter       ? node                                 ?<br \/>\n? interpreter args  ? N\/A                                  ?<br \/>\n? script id         ? 0                                    ?<br \/>\n? exec cwd          ? \/home\/nodeapp                        ?<br \/>\n? exec mode         ? fork_mode                            ?<br \/>\n? node.js version   ? 6.2.2                                ?<br \/>\n? watch & reload    ? ?                                    ?<br \/>\n? unstable restarts ? 0                                    ?<br \/>\n? created at        ? 2016-07-01T06:27:55.763Z             ?<br \/>\n????????????????????????????????????????????????????????????<br \/>\n Code metrics value<br \/>\n???????????????????????<br \/>\n? Loop delay ? 0.62ms ?<br \/>\n???????????????????????<br \/>\n Add your own code metrics: http:\/\/bit.ly\/code-metrics<br \/>\n Use `pm2 logs nodetest (--lines 1000)` to display logs<br \/>\n Use `pm2 monit` to monitor CPU and Memory usage nodetest<\/code><\/p>\n<p>Restart your application<\/p>\n<p><code>pm2 restart nodetest<\/code><\/p>\n<p>Output:<\/p>\n<p><code class=\"gris\">root@nodejs:\/home\/nodeapp# pm2 restart nodetest<br \/>\n[PM2] Applying action restartProcessId on app [nodetest](ids: 0)<br \/>\n[PM2] [nodetest](0) ?<br \/>\n?????????????????????????????????????????????????????????????????????????????????????<br \/>\n? App name ? id ? mode ? pid   ? status ? restart ? uptime ? memory      ? watching ?<br \/>\n?????????????????????????????????????????????????????????????????????????????????????<br \/>\n? nodetest ? 0  ? fork ? 27341 ? online ? 1       ? 0s     ? 15.570 MB   ? disabled ?<br \/>\n?????????????????????????????????????????????????????????????????????????????????????<br \/>\n Use `pm2 show <id|name>` to get more details about an app<\/code><\/p>\n<p>PM2 Monitor (similar to the top program for Linux)<\/p>\n<p><code>pm2 monit<\/code><\/p>\n<p>Output:<\/p>\n<p><code class=\"gris\">root@nodejs:\/home\/nodeapp# pm2 monit<br \/>\n? PM2 monitoring (To go further check out https:\/\/app.keymetrics.io)<br \/>\n  ? nodetest                            [                              ] 0 %<br \/>\n[0] [fork_mode]                        [||||||||||||||||||||||||||    ] 35.691 MB<\/code><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Install and use the PM2 NodeJS to deploy apps in production on Ubuntu 16 NodeJS applications are used in backends all over the web to perform various tasks. But unlike most web software, NodeJS scripts run in the foreground, rather than in the background as a daemon process, which inconveniently requires an open terminal window<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[71],"tags":[],"class_list":["post-2194","post","type-post","status-publish","format-standard","hentry","category-applications","operating_system-ubuntu-16-04"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Install and use the PM2 NodeJS to deploy apps in production on Ubuntu 16 - Globo.Tech<\/title>\n<meta name=\"description\" content=\"This tutorial will show you how to install and use the PM2 NodeJS process manager to deploy your apps in production on your Ubuntu 16 Server. Read now!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.globo.tech\/learning-center\/pm2-nodejs-process-manager-deploy-apps-ubuntu-16\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Install and use the PM2 NodeJS to deploy apps in production on Ubuntu 16 - Globo.Tech\" \/>\n<meta property=\"og:description\" content=\"This tutorial will show you how to install and use the PM2 NodeJS process manager to deploy your apps in production on your Ubuntu 16 Server. Read now!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.globo.tech\/learning-center\/pm2-nodejs-process-manager-deploy-apps-ubuntu-16\/\" \/>\n<meta property=\"og:site_name\" content=\"Globo.Tech\" \/>\n<meta property=\"article:published_time\" content=\"2016-07-13T17:47:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-12-12T21:12:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.globo.tech\/learning-center\/wp-content\/uploads\/2016\/09\/GloboTech-Logo.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"963\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"GloboTech Communications\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"GloboTech Communications\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.globo.tech\/learning-center\/pm2-nodejs-process-manager-deploy-apps-ubuntu-16\/\",\"url\":\"https:\/\/www.globo.tech\/learning-center\/pm2-nodejs-process-manager-deploy-apps-ubuntu-16\/\",\"name\":\"Install and use the PM2 NodeJS to deploy apps in production on Ubuntu 16 - Globo.Tech\",\"isPartOf\":{\"@id\":\"https:\/\/www.globo.tech\/learning-center\/#website\"},\"datePublished\":\"2016-07-13T17:47:05+00:00\",\"dateModified\":\"2017-12-12T21:12:44+00:00\",\"author\":{\"@id\":\"https:\/\/www.globo.tech\/learning-center\/#\/schema\/person\/e17784b37f4a4f49b7bc611847912e87\"},\"description\":\"This tutorial will show you how to install and use the PM2 NodeJS process manager to deploy your apps in production on your Ubuntu 16 Server. Read now!\",\"breadcrumb\":{\"@id\":\"https:\/\/www.globo.tech\/learning-center\/pm2-nodejs-process-manager-deploy-apps-ubuntu-16\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.globo.tech\/learning-center\/pm2-nodejs-process-manager-deploy-apps-ubuntu-16\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.globo.tech\/learning-center\/pm2-nodejs-process-manager-deploy-apps-ubuntu-16\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.globo.tech\/learning-center\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Install and use the PM2 NodeJS to deploy apps in production on Ubuntu 16\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.globo.tech\/learning-center\/#website\",\"url\":\"https:\/\/www.globo.tech\/learning-center\/\",\"name\":\"Globo.Tech\",\"description\":\"Welcome to the Official Globo.Tech Learning Center\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.globo.tech\/learning-center\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.globo.tech\/learning-center\/#\/schema\/person\/e17784b37f4a4f49b7bc611847912e87\",\"name\":\"GloboTech Communications\",\"sameAs\":[\"http:\/\/www.gtcomm.net\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Install and use the PM2 NodeJS to deploy apps in production on Ubuntu 16 - Globo.Tech","description":"This tutorial will show you how to install and use the PM2 NodeJS process manager to deploy your apps in production on your Ubuntu 16 Server. Read now!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.globo.tech\/learning-center\/pm2-nodejs-process-manager-deploy-apps-ubuntu-16\/","og_locale":"en_US","og_type":"article","og_title":"Install and use the PM2 NodeJS to deploy apps in production on Ubuntu 16 - Globo.Tech","og_description":"This tutorial will show you how to install and use the PM2 NodeJS process manager to deploy your apps in production on your Ubuntu 16 Server. Read now!","og_url":"https:\/\/www.globo.tech\/learning-center\/pm2-nodejs-process-manager-deploy-apps-ubuntu-16\/","og_site_name":"Globo.Tech","article_published_time":"2016-07-13T17:47:05+00:00","article_modified_time":"2017-12-12T21:12:44+00:00","og_image":[{"width":1920,"height":963,"url":"https:\/\/www.globo.tech\/learning-center\/wp-content\/uploads\/2016\/09\/GloboTech-Logo.png","type":"image\/png"}],"author":"GloboTech Communications","twitter_misc":{"Written by":"GloboTech Communications","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.globo.tech\/learning-center\/pm2-nodejs-process-manager-deploy-apps-ubuntu-16\/","url":"https:\/\/www.globo.tech\/learning-center\/pm2-nodejs-process-manager-deploy-apps-ubuntu-16\/","name":"Install and use the PM2 NodeJS to deploy apps in production on Ubuntu 16 - Globo.Tech","isPartOf":{"@id":"https:\/\/www.globo.tech\/learning-center\/#website"},"datePublished":"2016-07-13T17:47:05+00:00","dateModified":"2017-12-12T21:12:44+00:00","author":{"@id":"https:\/\/www.globo.tech\/learning-center\/#\/schema\/person\/e17784b37f4a4f49b7bc611847912e87"},"description":"This tutorial will show you how to install and use the PM2 NodeJS process manager to deploy your apps in production on your Ubuntu 16 Server. Read now!","breadcrumb":{"@id":"https:\/\/www.globo.tech\/learning-center\/pm2-nodejs-process-manager-deploy-apps-ubuntu-16\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.globo.tech\/learning-center\/pm2-nodejs-process-manager-deploy-apps-ubuntu-16\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.globo.tech\/learning-center\/pm2-nodejs-process-manager-deploy-apps-ubuntu-16\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.globo.tech\/learning-center\/"},{"@type":"ListItem","position":2,"name":"Install and use the PM2 NodeJS to deploy apps in production on Ubuntu 16"}]},{"@type":"WebSite","@id":"https:\/\/www.globo.tech\/learning-center\/#website","url":"https:\/\/www.globo.tech\/learning-center\/","name":"Globo.Tech","description":"Welcome to the Official Globo.Tech Learning Center","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.globo.tech\/learning-center\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.globo.tech\/learning-center\/#\/schema\/person\/e17784b37f4a4f49b7bc611847912e87","name":"GloboTech Communications","sameAs":["http:\/\/www.gtcomm.net"]}]}},"_links":{"self":[{"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/posts\/2194","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/comments?post=2194"}],"version-history":[{"count":7,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/posts\/2194\/revisions"}],"predecessor-version":[{"id":4026,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/posts\/2194\/revisions\/4026"}],"wp:attachment":[{"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/media?parent=2194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/categories?post=2194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/tags?post=2194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}