CloudPanel:Part 9

Node.js deployment with pm2

Node.js deployment with pm2

For running Node.js applications in production, you need process manager who takes care of the node processes.

We explain how to use the PM2 for setting up a Node.js Application for production on this site.

PM2

PM2 is a daemon process manager that will help you manage and keep your application online.

Installation

  1. Log in via SSH with your Site User:
ssh john-doe@instance-ip-address
  1. Go to the root directory of your project:
cd htdocs/www.domain.com/
  1. Install the latest pm2 via npm.
npm install pm2@latest -g

Start the Application

Use the following command to start your application via pm2:

App Name

Replace the app-name variable with the name of your application.
pm2 start npm --name $app-name -- start

Your application is now running via pm2.

Save Configuration

To save the pm2 configuration, execute the save command:

pm2 save

Adding a Cron Job

To ensure, that your application is running after a reboot of your instance, you need to configure a cron job.

  1. First copy the output of the PATH variable:
echo $PATH
The output will look similar to this:
/home/john-doe/.nvm/versions/node/v14.19.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  1. Edit the crontab for the site user.
crontab -e
  1. Add the following lines to it:
PATH=$PASTE_THE_OUTPUT_OF_$PATH
@reboot pm2 resurrect &> /dev/null

Example configuration

  1. Reboot your instance and check if the application is running:
pm2 status

The status should be online to confirm that your Application is running after reboot.

Troubleshooting

PM2 provides a logs command to see the application’s output, which helps troubleshoot.

pm2 logs

Introduction

dploy is a powerful code deployment solution that simplifies releasing new application versions.
dploy allows developers to automate the deployment process and ensure their applications are always up-to-date and running smoothly. Whether deploying a small website or a complex web application, dploy makes it easy.

Features

  • Open Source (MIT License)
  • Setup done in less than 60 seconds
  • Solution for PHP, Node.js, Static HTML Sites and Python
  • Zero Downtime Deployment
  • Continuous Deployment

Applications ~ Ghost

On this site, you find a guide to install and configure Ghost on CloudPanel.

Creating a Node.js Site Via CloudPanel

  1. Click on + Add Site and then click on Create a Node.js Site.
  1. Enter the Domain Name, Node.js Version, and the App Port, and click on Create.

Via CloudPanel CLI

You can create a Node.js Site with the following command as root user if you like the command line.

clpctl site:add:nodejs --domainName=www.domain.com --nodejsVersion=16 --appPort=2368 --siteUser='john-doe' --siteUserPassword='!secretPassword!'

Creating a Ghost project

  1. Create a Database and copy the Database Name, Database User Name, Database User Password.

  2. Log in via SSH with the Site User:

ssh john-doe@server-ip-address
  1. Install the Ghost CLI:
npm install ghost-cli@latest -g
  1. Replace DATABASE_USERNAME, DATABASE_USER_PASSWORD, DATABASE_NAME, SITE_USER, and DOMAIN and install Ghost.
ghost install --db mysql --port 2368 \
--dbhost 127.0.0.1 \
--dbuser $DATABASE_USER_NAME \
--dbpass $DATABASE_USER_PASSWORD \
--dbname $DATABASE_NAME \
--process local \
--no-setup-linux-user \
--no-setup-ssl \
--no-setup-nginx \
--dir /home/$SITE_USER/htdocs/$DOMAIN/
  1. Enter your Blog Url like https://www.domain.com/
  1. Start Ghost by confirming with Y:
  1. The installation is done. Open your site in the browser and create an admin user.

Deployment

Ghost needs to be started after an instance reboot. We will create a script that gets executed after the reboot.

  1. Log in via SSH with the Site User:
ssh john-doe@server-ip-address
  1. Create scripts directory:
mkdir ~/scripts/
  1. Create a start script:
nano ~/scripts/ghost-start.sh

Modify the –dir value and save the start script:

#!/bin/bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
ghost start --dir $HOME/htdocs/ghost.moby.io/ --no-setup-linux-user > $HOME/logs/ghost.log &
  1. Set execution permissions:
chmod 700 ~/scripts/ghost-start.sh
  1. Edit the crontab for the site user.
crontab -e
  1. Replace SITE_USER with your site user and add the following line:
@reboot /home/$SITE_USER/scripts/ghost-start.sh &> /dev/null
  1. Reboot and test if Ghost is running as expected.

Applications ~ Strapi 4

On this site, you find a guide to install and configure Strapi 4 on CloudPanel.

Creating a Node.js Site Via CloudPanel

  1. Click on + Add Site and then click on Create a Node.js Site.
  1. Enter the Domain Name, Node.js Version, and the App Port, and click on Create.

Via CloudPanel CLI

You can create a Node.js Site with the following command as root user if you like the command line.

clpctl site:add:nodejs --domainName=www.domain.com --nodejsVersion=14 --appPort=1337 --siteUser='john-doe' --siteUserPassword='!secretPassword!'

Creating a Strapi project

  1. Log in via SSH with the Site User:
ssh john-doe@server-ip-address
  1. Go to htdocs and delete the directory which CloudPanel has created:
cd htdocs && rm -rf www.domain.com
  1. Create a Strapi Project:
npx create-strapi-app@latest www.domain.com

Building the Admin Panel

To build the Admin Panel, go to the root directory of your strapi installation

cd htdocs/www.domain.com/

and execute npm run build with NODE_ENV=production:

NODE_ENV=production npm run build

Running Strapi

To run Strapi, go to the root directory of your strapi installation

cd htdocs/www.domain.com/

and execute npm start with NODE_ENV=production:

NODE_ENV=production npm start

The output will look like this:

To create a user, you can now open your Strapi Installation in the browser https://www.domain.com/admin.

Production Deployment

For running Strapi in production, it’s essential to have an process manager like pm2 who takes care of the node processes.

Follow the Node.js Deployment for a step-by-step guide.