Setting Up Node.js on native LSWS

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

If you have CloudLinux, cPanel or DirectAdmin and are running Enterprise Litespeed 5.3 or higher, as far as Node.js is working well on Apache, it should work well on LiteSpeed out of the box without a proxy. See: How to use CloudLinux Node.js Selector with LSWS.

The following wiki guides you through the steps to set up Node.js with LiteSpeed Web Server. The wiki assumes that you have a functional LSWS installation running off of Apache configurations (though the steps can be easily adapted to using LSWS-native configuration files).

Install Node.js

Get the latest Node.js package.

wget http://nodejs.org/dist/node-latest.tar.gz

Expand the tarball.

tar xvzf node-latest.tar.gz

Build Node.js from the source.

cd node-vX.X.X
./configure
make
make install

Add Your Node.js Application

For the rest of this wiki, we will be using the example Node.js web server example.js found on Node.js's homepage. This web server runs on port 1337 and responds with “Hello World” to every request.

Proxy Node.js Traffic

Now that Node.js is installed on your server, you will need to tell LSWS to send the proper traffic to your Node.js application. Starting with LSWS 4.2.13, this can be done with a just a rewrite rule (if Node.js is installed on the same server as LSWS). Simply add a rewrite rule that will redirect traffic to the port Node.js is listening on. The following is an example, the domain name for the site is “example.com”, Node.js application server is running on port 1337, add the following rewrite rule to .htaccess under document root directory.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ http://example.com:1337/$1 [P,L]

The above example redirects all traffic (after checking that it is a directory or file) to port 1337 (where example.js is running, as noted above). Static files are still served by LSWS.

If there is no need to serve static files through LSWS, you can let LSWS proxy all requests to backend application server with rewrite rule

RewriteEngine On
RewriteRule ^(.*)$ http://example.com:1337/$1 [P,L]

If Using LSWS Below 4.2.13 or Proxying to an External Server

In order for LSWS to proxy traffic to another web server, LSWS needs a proxy-type external application. Starting with LSWS 4.2.13, LSWS will automatically create a proxy external application for rewrite rules that redirect to a local web server. If you are using a version below 4.2.13 or are running Node.js on a different server, you will need to use the following steps to manually create a proxy external application to proxy to Node.js in addition to adding the rewrite rules above.

Create a Web Server External Application

Add a new external application (WebAdmin console > Configuration > Server > External App > Add). Choose “Web Server” as the Type.

Configure the Web Server External Application

In the above configurations we have made the Location 1.2.3.4:1337. It is important that the Location match the IP (127.0.0.1 if it is on the same server) and port your Node.js application is running on.

After saving the configurations, restart LSWS to save changes. Your traffic should be redirected to your Node.js application now.

Notes

  • If you encounter errors using Node.js, please make sure that Node.js is started and running when using it.
  • If you continue to encounter errors validate that your application works correctly with Node.js running without Litespeed.
 
litespeed_wiki/other-ext-apps/nodejs-setup.txt · Last modified: 2018/10/10 20:27 by Jackson Zhang