{"id":520,"date":"2024-05-05T15:34:06","date_gmt":"2024-05-05T15:34:06","guid":{"rendered":"https:\/\/zahiralam.com\/blog\/?p=520"},"modified":"2024-11-05T07:02:09","modified_gmt":"2024-11-05T07:02:09","slug":"setting-up-a-node-js-application-for-production-with-pm2","status":"publish","type":"post","link":"https:\/\/zahiralam.com\/blog\/setting-up-a-node-js-application-for-production-with-pm2\/","title":{"rendered":"Setting Up a Node.js Application for Production with PM2"},"content":{"rendered":"\n<p>When deploying a Node.js application in a production environment, ensuring its continuous operation and effective resource management is crucial. PM2 (Process Manager 2) is a popular process manager for Node.js applications that helps in managing and keeping applications online 24\/7. This article will guide you through setting up your Node.js application for production using PM2, including configuration, updating, monitoring, and logging.\n\n\n\n<h2 class=\"wp-block-heading\">Initial Setup with PM2:<\/h2>\n\n\n\n<p>1. <strong>Installing PM2:<\/strong>\n\n\n\n<p>Before deploying your application, you need to have PM2 installed on your <strong>server<\/strong>. Install PM2 globally using npm:\n\n\n\n<div class=\"code-block-container\">\n                        <pre class=\"wp-block-code\"><code id=\"code-1\">npm install pm2@latest -g<\/code><\/pre>\n                        <amp-iframe sandbox=\"allow-scripts\" width=\"94\" height=\"72\" frameborder=\"0\" \n                                    src=\"https:\/\/zahiralam.com\/blog\/wp-content\/plugins\/amp-copy-code-button\/copier.html#npm%20install%20pm2%40latest%20-g\">\n                            <button class=\"copy-button\" data-label=\"npm install pm2@latest -g\"  placeholder disabled>Copy<\/button>\n                        <\/amp-iframe>\n                    <\/div>\n\n\n\n<p><strong>2. Creating an Ecosystem File:<\/strong>\n\n\n\n<p>PM2 utilizes an <code>ecosystem.config.js<\/code> file which allows you to manage configurations for your applications. Here&#8217;s how you can set up this file:\n\n\n\n<p>Create a file named <code>ecosystem.config.js<\/code> in your project&#8217;s root directory with the following content:\n\n\n\n<div class=\"code-block-container\">\n                        <pre class=\"wp-block-code\"><code id=\"code-2\">module.exports = {\n    apps : [{\n      name: &quot;firstnodeapp&quot;,\n      script: &quot;.\/dist\/app.js&quot;,\n      instances: &quot;max&quot;, \/\/ This will use all CPUs. Adjust as needed.\n      autorestart: true,\n      watch: false, \/\/ Set to true if you want PM2 to restart on file changes\n      max_memory_restart: &#039;1G&#039;,\n      dotenv: &#039;.env&#039;\n    }]\n  };<\/code><\/pre>\n                        <amp-iframe sandbox=\"allow-scripts\" width=\"94\" height=\"72\" frameborder=\"0\" \n                                    src=\"https:\/\/zahiralam.com\/blog\/wp-content\/plugins\/amp-copy-code-button\/copier.html#module.exports%20%3D%20%7B%0A%20%20%20%20apps%20%3A%20%5B%7B%0A%20%20%20%20%20%20name%3A%20%22firstnodeapp%22%2C%0A%20%20%20%20%20%20script%3A%20%22.%2Fdist%2Fapp.js%22%2C%0A%20%20%20%20%20%20instances%3A%20%22max%22%2C%20%2F%2F%20This%20will%20use%20all%20CPUs.%20Adjust%20as%20needed.%0A%20%20%20%20%20%20autorestart%3A%20true%2C%0A%20%20%20%20%20%20watch%3A%20false%2C%20%2F%2F%20Set%20to%20true%20if%20you%20want%20PM2%20to%20restart%20on%20file%20changes%0A%20%20%20%20%20%20max_memory_restart%3A%20%271G%27%2C%0A%20%20%20%20%20%20dotenv%3A%20%27.env%27%0A%20%20%20%20%7D%5D%0A%20%20%7D%3B\">\n                            <button class=\"copy-button\" data-label=\"module.exports = {\n    apps : [{\n      name: &quot;firstnodeapp&quot;,\n      script: &quot;.\/dist\/app.js&quot;,\n      instances: &quot;max&quot;, \/\/ This will use all CPUs. Adjust as needed.\n      autorestart: true,\n      watch: false, \/\/ Set to true if you want PM2 to restart on file changes\n      max_memory_restart: &#039;1G&#039;,\n      dotenv: &#039;.env&#039;\n    }]\n  };\"  placeholder disabled>Copy<\/button>\n                        <\/amp-iframe>\n                    <\/div>\n\n\n\n<p>In this configuration:\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>name<\/code>: Sets the application name in PM2.<\/li>\n\n\n\n<li><code>script<\/code>: Specifies the entry file of your application.<\/li>\n\n\n\n<li><code>instances<\/code>: Defines the number of instances to run. Using <code>\"max\"<\/code> utilizes all available CPUs, enhancing performance and load distribution.<\/li>\n\n\n\n<li><code>autorestart<\/code>: Enables the application to restart automatically if it crashes.<\/li>\n\n\n\n<li><code>watch<\/code>: Monitors changes in the application files, which is useful during development.<\/li>\n\n\n\n<li><code>max_memory_restart<\/code>: Automatically restarts your app if it reaches the specified memory limit, helping to keep memory leaks in check.<\/li>\n\n\n\n<li><code>env<\/code>: Specifies environment variables that are necessary for running your application in production.<\/li>\n<\/ul>\n\n\n\n<p><strong>3. Starting the Application:<\/strong>\n\n\n\n<p>Once your <code>ecosystem.config.js<\/code> is configured, you can start your application by running:\n\n\n\n<div class=\"code-block-container\">\n                        <pre class=\"wp-block-code\"><code id=\"code-3\">pm2 start ecosystem.config.js<\/code><\/pre>\n                        <amp-iframe sandbox=\"allow-scripts\" width=\"94\" height=\"72\" frameborder=\"0\" \n                                    src=\"https:\/\/zahiralam.com\/blog\/wp-content\/plugins\/amp-copy-code-button\/copier.html#pm2%20start%20ecosystem.config.js\">\n                            <button class=\"copy-button\" data-label=\"pm2 start ecosystem.config.js\"  placeholder disabled>Copy<\/button>\n                        <\/amp-iframe>\n                    <\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Updating the Application:<\/h2>\n\n\n\n<p>When you make changes to your application and push them to your production server (via Git or other deployment methods), you must restart the application to apply these changes. Follow these steps:\n\n\n\n<p>1. Pull the latest changes:\n\n\n\n<div class=\"code-block-container\">\n                        <pre class=\"wp-block-code\"><code id=\"code-4\">git pull origin main<\/code><\/pre>\n                        <amp-iframe sandbox=\"allow-scripts\" width=\"94\" height=\"72\" frameborder=\"0\" \n                                    src=\"https:\/\/zahiralam.com\/blog\/wp-content\/plugins\/amp-copy-code-button\/copier.html#git%20pull%20origin%20main\">\n                            <button class=\"copy-button\" data-label=\"git pull origin main\"  placeholder disabled>Copy<\/button>\n                        <\/amp-iframe>\n                    <\/div>\n\n\n\n<p>2. Rebuild your application if necessary (for example, when using TypeScript):\n\n\n\n<div class=\"code-block-container\">\n                        <pre class=\"wp-block-code\"><code id=\"code-5\">npm run build<\/code><\/pre>\n                        <amp-iframe sandbox=\"allow-scripts\" width=\"94\" height=\"72\" frameborder=\"0\" \n                                    src=\"https:\/\/zahiralam.com\/blog\/wp-content\/plugins\/amp-copy-code-button\/copier.html#npm%20run%20build\">\n                            <button class=\"copy-button\" data-label=\"npm run build\"  placeholder disabled>Copy<\/button>\n                        <\/amp-iframe>\n                    <\/div>\n\n\n\n<p>3. Restart the application using PM2:\n\n\n\n<div class=\"code-block-container\">\n                        <pre class=\"wp-block-code\"><code id=\"code-6\">pm2 restart firstnodeapp<\/code><\/pre>\n                        <amp-iframe sandbox=\"allow-scripts\" width=\"94\" height=\"72\" frameborder=\"0\" \n                                    src=\"https:\/\/zahiralam.com\/blog\/wp-content\/plugins\/amp-copy-code-button\/copier.html#pm2%20restart%20firstnodeapp\">\n                            <button class=\"copy-button\" data-label=\"pm2 restart firstnodeapp\"  placeholder disabled>Copy<\/button>\n                        <\/amp-iframe>\n                    <\/div>\n\n\n\n<p>\n\n\n\n<h2 class=\"wp-block-heading\">Monitoring and Logging:<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Monitoring:<\/strong><\/h3>\n\n\n\n<p>PM2 provides a built-in monitoring tool that can be accessed via:\n\n\n\n<div class=\"code-block-container\">\n                        <pre class=\"wp-block-code\"><code id=\"code-7\">pm2 monit<\/code><\/pre>\n                        <amp-iframe sandbox=\"allow-scripts\" width=\"94\" height=\"72\" frameborder=\"0\" \n                                    src=\"https:\/\/zahiralam.com\/blog\/wp-content\/plugins\/amp-copy-code-button\/copier.html#pm2%20monit\">\n                            <button class=\"copy-button\" data-label=\"pm2 monit\"  placeholder disabled>Copy<\/button>\n                        <\/amp-iframe>\n                    <\/div>\n\n\n\n<p>This command opens a dashboard where you can monitor CPU and memory usage for each instance of your application.\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Logging:<\/strong><\/h3>\n\n\n\n<p>PM2 automatically handles standard output and error logs. You can view these logs using:\n\n\n\n<div class=\"code-block-container\">\n                        <pre class=\"wp-block-code\"><code id=\"code-8\">pm2 logs firstnodeapp<\/code><\/pre>\n                        <amp-iframe sandbox=\"allow-scripts\" width=\"94\" height=\"72\" frameborder=\"0\" \n                                    src=\"https:\/\/zahiralam.com\/blog\/wp-content\/plugins\/amp-copy-code-button\/copier.html#pm2%20logs%20firstnodeapp\">\n                            <button class=\"copy-button\" data-label=\"pm2 logs firstnodeapp\"  placeholder disabled>Copy<\/button>\n                        <\/amp-iframe>\n                    <\/div>\n\n\n\n<p>You can also specify log file paths in your <code>ecosystem.config.js<\/code> if you need more control over log management.\n\n\n\n<p>\n\n\n\n<h2 class=\"wp-block-heading\">Ensuring Maximum Resource Utilization:<\/h2>\n\n\n\n<p>To ensure that your application efficiently utilizes server resources:\n\n\n\n<ul class=\"wp-block-list\">\n<li>Set <code>instances<\/code> to <code>\"max\"<\/code> to use all CPU cores.<\/li>\n\n\n\n<li>Monitor memory and CPU usage to prevent overutilization.<\/li>\n\n\n\n<li>Consider enabling the <code>watch<\/code> feature in development for instant feedback on code changes.<\/li>\n<\/ul>\n\n\n\n<p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>PM2 is a powerful tool for managing Node.js applications in production. It not only ensures that your application stays online but also provides essential features such as load balancing, monitoring, and logging. By configuring <code>ecosystem.config.js<\/code> appropriately and following best practices for deployment and maintenance, you can achieve a stable and efficient production environment for your Node.js applications.\n","protected":false},"excerpt":{"rendered":"<p>When deploying a Node.js application in a production environment, ensuring its continuous operation and effective resource management is crucial. PM2 (Process Manager 2) is a [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[75,76,66],"class_list":["post-520","post","type-post","status-publish","format-standard","hentry","category-nodejs","tag-pm2","tag-typescript","tag-ubuntu-24-04"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/posts\/520","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/comments?post=520"}],"version-history":[{"count":2,"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/posts\/520\/revisions"}],"predecessor-version":[{"id":1407,"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/posts\/520\/revisions\/1407"}],"wp:attachment":[{"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/media?parent=520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/categories?post=520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zahiralam.com\/blog\/wp-json\/wp\/v2\/tags?post=520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}