Deploy Javascript App to Cpanel

Natserract

Natserract

Apr 14, 2023

Deploy Javascript App to Cpanel
Specifications Niaga Hoster, NodeJS V.16^, NextJS (Turborepo) 13

Deploy NodeJS:

Setup nodejs app in cpanel:

copy package.json & dist/* -> applications directory

  • Setting main in package.json -> main.js

Login to ssh:

  • Setting environment variables(cpanel)
    • export PATH=$PATH:/opt/alt/alt-nodejs16/root/usr/bin/
    • Related Issue: (/usr/bin/env: node: No such file or directory)
  • /opt/alt/alt-nodejs16/root/usr/bin/npm install (maybe different, in other device e.g /opt/cpanel)
    • Several modules need install manually
      • Bycrpt node-pre-gyp, so install npm install node-gyp first
      • And do npm install again

Setting app environment

Go to cpanel (node js app management): run npm install and run js script

. . .

Deploy NextJS:

Setup NextJS build:

  • Set main: ”server.js” in package.json
  • Create new file server.js in root directory and copy this server.js gist
  • Add more scripts "serve:production": "NODE_ENV=production node server.js" in package.json
  • Set next.config.js output: "standalone"
  • yarn build
  • Run this script (or check the comment in shared gist above)
#!/bin/bash
NODE_ENV=production
rm -r build build.zip
yarn build && mkdir build
cp -R ./.next/standalone/node_modules ./.next/standalone/packages build
cp -R server.js package.json build
mkdir build/out
cp -R ./.next/standalone/apps/web/.next/* build/out
zip -r build.zip ./build

Setup node js app cpanel:

  • Application root: domain.com
  • Application startup: server.js
  • Add environment
    • HOSTNAME, PORT
    • Upload & extract build.zip to file manager
  • run npm install and run js script

Notes:

Issue: Can't acquire lock for app public_html/domain.com

  • Go to /home/your_username/nodevenv/public_html/domain.com/
  • Then remove .lock

Issue: number of process full

Further reading: