Running Node.js Apps With OpenLiteSpeed

July 12, 2022 . 2 MIN READ

Running Node.js Apps with OpenLiteSpeed

Node.js typically runs as a standalone web server. However, OpenLiteSpeed can be configured to proxy requests to a Node.js application, allowing you to run apps (such as Ghost) seamlessly on your server.

This guide explains how to run Node.js applications with OpenLiteSpeed. It assumes that Node.js is already installed and your virtual hosts are properly configured.


Requirements

  • OpenLiteSpeed version 1.4.41 or higher

  • Node.js installed

  • Virtual host already set up


Setup

1. Install Node.js

If Node.js is not installed yet, install it using:

CentOS:

yum install nodejs

Ubuntu:

apt-get install nodejs

2. Configure App Server Context

In this example, we use the default URI: /node/

Navigate to:
Web Admin → Virtual Hosts → Context → Add

Set the following values:

  • Type: App Server

  • URI: /node/

  • Location: /usr/local/lsws/Example/node/

  • Binary Path: /usr/bin/node

  • Application Type: node


Verification

  1. Create a node directory inside your document root.

  2. Create a file named app.js with the following content:

const http = require(‘http’);

const hostname = ‘127.0.0.1’;
const port = 3000;

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader(‘Content-Type’, ‘text/plain’);
res.end(‘Hello World from Node.js app\n);
});

server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});

  1. Open your browser and visit:

http://<YOUR_SERVER_IP>/node/

You should see:

Hello World from Node.js app

Optional Settings

Custom Binary Path

If Node.js is installed in a different location, update:

  • Binary Path: /usr/node (or your custom path)


Custom Startup File

To use a different startup file (e.g., node.js instead of app.js):

  • Set Startup File: node.js in the context configuration


Final Notes

  • OpenLiteSpeed acts as a proxy between users and your Node.js app

  • Ensure correct file permissions and paths

  • Restart OpenLiteSpeed after making configuration changes

Reference:

https://openlitespeed.org/kb/running-node-js-apps-with-openlitespeed/

Leave a Reply

Your email address will not be published. Required fields are marked *