To run a Nuxt.js application under PM2 (a popular process manager for Node.js applications), follow these steps:
1. Ensure Node.js and PM2 are installed
First, verify that Node.js is installed on your server. After installation, use npm to install PM2 with the following command:
bashnpm install -g pm2
2. Set up your Nuxt.js application
Confirm your Nuxt.js application is properly configured and running locally. If starting from scratch, create a new project using the create-nuxt-app command:
bashnpx create-nuxt-app <project-name>
For existing projects, ensure all dependencies are installed:
bashnpm install
3. Adjust Nuxt.js configuration
To enable PM2 to manage the application, modify nuxt.config.js to listen on all IP addresses. Add the following configuration:
javascriptexport default { server: { host: '0.0.0.0' // Default is localhost } }
4. Create the PM2 configuration file
In your project root, create ecosystem.config.js to define startup parameters. A basic configuration is:
javascriptmodule.exports = { apps: [ { name: 'NuxtAppName', exec_mode: 'cluster', instances: 'max', // Or any number of instances script: './node_modules/nuxt/bin/nuxt.js', args: 'start' } ] }
This configuration specifies the application runs in cluster mode using Nuxt's startup script.
5. Launch your Nuxt.js application with PM2
With everything ready, start the application using:
bashpm2 start ecosystem.config.js
6. Verify application status
Check the status with:
bashpm2 status
This displays the status of all applications managed by PM2.
7. Configure logging and monitoring
PM2 provides robust logging and monitoring features. View logs using:
bashpm2 logs
Or use the monitoring tool:
bashpm2 monit
Following these steps, your Nuxt.js application should run successfully under PM2, ensuring stability and reliability in production environments. Always test all configurations in a local or development environment before deployment.