Start Front-Commerce with PM2
Front-Commerce is a node.js application. You may want to run it in production with the PM2 Process Manager. This page explains how you could achieve this, and benefit from PM2 features such as; zero-downtime deployment, cluster mode, auto-reload, logs aggregation…
PM2 is not the only way to deploy Front-Commerce in production, however, we often get asked about how PM2 should be configured for Front-Commerce. This page is here to get you started but we recommend that you discuss it with your system administrator.
ecosystem.config.cjs
We recommend to use an ecosystem.config.cjs
file so you could track changes to
your deployment configuration, however these settings can also be passed to PM2
CLI.
Below is an annotated example of configuration:
module.exports = {
apps: [
{
// Name of the app
name: "front-commerce",
// Path to the script to run
script: "./server.mjs",
// Path to your front-commerce app
cwd: "/path/to/your/frontcommerce/app",
// Node arguments
node_args: "--import tsx/esm",
// Environment variables
env: {
NODE_ENV: "production",
// You may also prefer to use this configuration file
// to define environment variables
},
// collocate PM2 logs with default Front-Commerce logs
error_file: "./logs/pm2-error.log",
out_file: "./logs/pm2-out.log",
log_file: "./logs/pm2-combined.log",
// It is safe to let PM2 restart your processes in case of
// an unexpected failure, or if they consume too much memory.
// Memory leaks on long-running processes such as node servers
// are common, and it is safe to have a watchdog for them
autorestart: true,
restart_delay: 1000,
kill_timeout: 15000,
wait_ready: true,
max_memory_restart: "2G",
// PM2's cluster mode allows your application to leverage all
// the CPUs available on your server. It is a good way to make
// your app faster.
exec_mode: "cluster",
instances: "max",
},
],
};
This page only focuses on Front-Commerce related configurations. You should look into additional PM2 configuration keys and ensure they are adapted to your deployment environment for an optimal configuration.
Start your application
If an ecosystem.config.cjs
file is versioned at the root of your project, you
could then start or replace your Front-Commerce app in production with a new
version using the command below:
pm2 reload ecosystem.config.cjs
It will allow you to deploy the new version of your application with zero downtime!
For other questions about using PM2, we recommend that you read their official documentation.