Skip to content

Node.js

Node.js is a platform that generally runs as a separate web server. OpenLiteSpeed Web Server can be configured to proxy traffic to Node.js so that users can run Node.js applications (like Ghost) on their sites.

Requirements

  • OpenLiteSpeed version 1.4.41+
  • Virtual host set up to run Node.js on

Setup

Install Node.js

If you haven't already, install the Node.js application:

yum install nodejs
apt-get install nodejs

Set up Context

This example assumes you are using the default document root + /node/. Navigate to Web Admin > Virtual Hosts > Context > Add, and set the following values:

  • Type = App Server
  • URI = /node/
  • Location = /usr/local/lsws/Example/node/
  • Binary Path = /usr/bin/node
  • Application Type = node

Verification

Create a node directory under your document root. Create an app.js file 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.js\n');
});

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

Visit http://Your_Server_IP:Port/node/ in your browser and you should see:

Hello World from node js app.jsĀ 

Optional Settings

Custom Binary Path

If you want to change the node path, just update the Context and set Binary Path = /usr/node.

Custom Startup File Name

If you want to change the default startup file name from app.js to node.js, just update the Context and set Startup File = node.js.