Skip to content

Instantly share code, notes, and snippets.

@montasim
Created July 6, 2024 14:26
Show Gist options
  • Save montasim/0e64911718a53deabd8fd8a764d80e32 to your computer and use it in GitHub Desktop.
Save montasim/0e64911718a53deabd8fd8a764d80e32 to your computer and use it in GitHub Desktop.
The ecosystem.config.json file is a configuration file used by PM2, a popular process manager for Node.js applications. This file allows you to define and manage complex application environments with multiple configurations. Below is a detailed overview and description of the settings specified in your ecosystem.config.json file for a library ma…
{
"apps": [
{
"name": "library-management-system-server:production",
"script": "build/server.js",
"instances": 1,
"autorestart": true,
"watch": false,
"time": true,
"exec_mode": "cluster",
"error_file": "logs/err.log",
"out_file": "logs/out.log",
"merge_logs": true,
"log_date_format": "YYYY-MM-DD HH:mm:ss",
"env": {
"NODE_ENV": "production",
"PORT": 5000,
"LOG_LEVEL": "info"
},
"env_production": {
"NODE_ENV": "production",
"API_KEY": "your_production_api_key_here",
"DATABASE_URL": "your_production_database_url_here"
},
"max_memory_restart": "500M",
"restart_delay": 5000
}
]
}
@montasim
Copy link
Author

montasim commented Jul 6, 2024

File Overview

ecosystem.config.json specifies various operational parameters for how the application should be started and managed by PM2. It supports multiple applications in a single file, making it ideal for microservices or applications with multiple parts that need to run simultaneously.

Configuration Options

"apps": An array of applications that PM2 will manage. Each object in the array represents a separate application configuration.

"name": A unique name for the application process, useful for identifying the process within PM2 management tools.

"script": The path to the script file that will be executed by PM2 to start the application. In this case, it points to build/server.js, which suggests that the application is compiled or built and the output is placed in the build directory.

"instances": The number of instances of the application that should be run. Setting this to 1 means only one instance of the application will run.

"autorestart": If set to true, PM2 will automatically restart the application if it crashes.

"watch": If true, PM2 will watch the files in the application directory for changes and restart the application as needed. This is typically used in development.

"time": Adds time stamps to log output, which can be crucial for debugging and monitoring.

"exec_mode": Defines the mode of execution; setting this to cluster allows running multiple instances of Node.js for load balancing.

"error_file" and "out_file": Paths to files where standard error and standard output logs will be redirected.

"merge_logs": If true, PM2 will merge logs from all instances of the application into a single log file.

"log_date_format": Specifies the format of timestamp for the logs.

"env": An object containing environment variables that should be available to the script at runtime.

"env_production": Defines environment variables specifically for the production environment, such as API keys and database URLs.

"max_memory_restart": The maximum amount of memory the application can use before being restarted automatically. This is set to "500M", meaning 500 megabytes.

"restart_delay": The amount of time (in milliseconds) PM2 should wait before restarting a crashed application.

Purpose and Benefits

Using ecosystem.config.json with PM2 provides several benefits:

Centralized Configuration: Keeps all application configuration in one place, which simplifies management and deployment.

Scalability: Easily scale applications by increasing the number of instances.

Resilience: Automatically restart applications upon failure, improving the reliability of the system.

Efficiency: Manage memory usage and optimize the use of system resources.

Logging and Monitoring: Configure detailed logging and enable timestamps to help with monitoring and troubleshooting.

This configuration file is essential for deploying and managing Node.js applications in production environments, ensuring that they run smoothly and recover gracefully from crashes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment