Setup Express.js Application on CyberPanel/OpenLiteSpeed

Express.js is a web application framework for Node.js. We’ve written quite a few articles on running multiple Node.js based applications in past because OpenLiteSpeed recently had native support to run Node.js based applications without doing any reverse proxy, this gives a great performance improvment.

Today we will specifically see that how we can run Express.js Application on CyberPanel using the feature provided to us by OpenLiteSpeed.

Related Read: HOW TO INSTALL NODEBB ON CYBERPANEL

Step 1: Install CyberPanel and Create Website

You first need to install CyberPanel and create your website. Skip this step if you have already done so. You can also issue SSL for this website.

Note: In this example we will use expressjs.cyberpanel.net as example domain, replace it with your domain whereever applicable.

Step 2: Install Node.js via Command Line

Next step is to install Node.js via command line.

CentOS

yum install nodejs

Ubuntu

apt-get install nodejs npm

Step 3: Setup basic Express.js Application

We will now set up basic Express.js application in the document root of our selected domain above (expressjs.cyberpanel.net).

cd /home/expressjs.cyberpanel.net/public_html

## npm init will be asking some questions, you can leave them all to defaults
## by pressing ENTER/RETURN

## For entry point please use app.js

npm init

## Finally install Express.js

npm install express --save

Create an app.js file containing Hello World application in /home/expressjs.cyberpanel.net/public_html/ with following content

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

Save this file, and for once click Fix Permissions from File Manager as you was doing all operations through command line so correct permissions needs to be in place.

Step 4: Setup AppServer Context to Serve Express.js Application

Open website launcher by visiting https:// < IP Address >:8090/websites/expressjs.cyberpanel.net and click vHost conf to add the context configurations.

Finally add this code to the end of the vHost Conf File as shown below:

context / {
  type                    appserver
  location                /home/expressjs.cyberpanel.net/public_html
  binPath                 /usr/bin/node
  appType                 node
  maxConns                100
 
  rewrite  {
 
  }
  addDefaultCharset       off
}

Click Save and your Express.js app should be running in the brwoser.

2 Likes

hello, I have done the corresponding steps. I have an api created with adonis.js (Node Framework). trying to access the site is always loading, it does not redirect to anything not even index.html. I have added the startupFile server.js line in my context to indicate that it should boot into that file. I must do something else

context / {
type appserver
location /home/back.padeirosdomundo.com/public_html
binPath /usr/bin/node
appType node
startupFile server.js
maxConns 100

rewrite {

}
addDefaultCharset off
}

1 Like

Also make sure you restart the server once and try.

whats it’s very needed?

i have create app using nodejs and socket.io it’s not working. But when i try this tutorial it’s work but not mine.

I have tested exactly this code and it has worked perfectly. I’m starting to believe that the problem is my backend. although if I use node server.js it runs in the console without error, but I can’t communicate with it. I ask, can I configure pm2 to release some port and thus launch my api?

I have managed to get it to work using pm2. I just had to add the port in my firewall. Thanks :grin:

1 Like

I followed all the steps above but still not working for my node js application. Can you please help me?

Please do provide more details of your issue.

yes, I have seen the same error. The express js app works till I try to connect it with MongoDB the moment I connect it with MongoDB the website does not render

This is not working! I did everything, I added nodejs files to server, and even started it using PM2 and It is accessible by IpAddress:port but not accessible through the domain!!! Can you please verify if this post is really up to date and working!! btw my domain is created with certain PHP version, As It was necessary to select version! Please help me out!

I even restarted server, no luck…

I fixed the problem if I change ‘appserver’ for ‘App Server’