> ## Documentation Index
> Fetch the complete documentation index at: https://kb.hosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Installing and configuring Node.js on managed hosting

> Learn how to install and configure Node.js on managed hosting accounts.

This article describes how to install the Node.js platform on managed VPS hosting accounts. You can use Node.js to host third-party applications or you can run your own applications.

<Warning>
  **Important**

  The procedures in this article are unsupported. Instead, cPanel now includes the **Node.js Selector** (with the exception of managed VPS accounts) which greatly simplifies installing and configuring Node.js applications. For information about how to use the Node.js Selector, please see [Creating a Node.js application with the Node.js Selector](/docs/create-application-with-nodejs-selector).
</Warning>

## Installation prerequisites

Your account must have the normal shell (not jailshell) enabled. To determine which shell your account is using, log in [using SSH](/docs/using-ssh-secure-shell), and then type the following command:

```bash theme={null}
echo $SHELL
```

If your account is using jailshell, please open a support ticket at [https://my.hosting.com](https://my.hosting.com) and request normal shell access for your account.

## Installing Node.js and npm

After you determine that your account meets the installation prerequisites, you can download and install Node.js and npm (the Node.js package manager). To do this, follow these steps:

1. Log in to your account [using SSH](/docs/using-ssh-secure-shell).

2. At the command prompt, type the following commands:

   ```bash theme={null}
   cd ~
   wget https://nodejs.org/dist/v16.20.2/node-v16.20.2-linux-x64.tar.xz
   ```

   > 📘 Note
   >
   > * This command downloads the binaries for the Node.js version 16.20.2 LTS (long-term support) release. If you download a newer development version of Node.js, change the version numbers in the following commands to match the version you downloaded.
   >
   > * Although there are more recent LTS versions such as 18.19.0 and 20.11.0, they require higher versions of the GNU C library (glibc) than what is currently installed on our managed servers.

3. To extract the Node.js files, type the following command:

   ```bash theme={null}
   tar xvf node-v16.20.2-linux-x64.tar.xz
   ```

4. To rename the extracted folder to the more convenient *nodejs* name, type the following command:

   ```bash theme={null}
   mv node-v16.20.2-linux-x64 nodejs
   ```

5. To install the *node* and *npm* binaries, type the following commands:

   ```bash theme={null}
   mkdir ~/bin
   cp nodejs/bin/node ~/bin
   cd ~/bin
   ln -s ../nodejs/lib/node_modules/npm/bin/npm-cli.js npm
   ```

   After you run these commands, Node.js and npm are installed on your account. To verify this, type the following commands:

   ```bash theme={null}
   node --version
   npm --version
   ```

   > 👍 Tip
   >
   > The *\~/bin* directory is in your path by default, which means you can run *node* and *npm* from any directory in your account.

## Starting a Node.js application

After you install Node.js, you are ready to run Node.js applications. However, the exact steps to do this vary depending on the application configuration.

### Method #1: Use npm

Many third-party and "production-ready" applications (such as Ghost) use the *npm* program to start the application, as shown by the following command:

```bash theme={null}
nohup npm start --production &
```

<Note>
  The *&* places the command in the background, and the *nohup* command ensures that the application continues running even if you log out of the current terminal session.
</Note>

For this method to work, there must be a valid *package.json* file for the application. The *package.json* file contains project metadata that the *npm* program reads to determine how to start the application, manage its dependencies, and more.

<Tip>
  To view the official *npm* documentation for the *package.json* file, please visit [https://docs.npmjs.com/files/package.json](https://docs.npmjs.com/files/package.json).
</Tip>

### Method #2: Run node directly

For simple applications, or for any application that does not have a *package.json* file, you can run the *node* executable directly and specify the application filename. For example:

```bash theme={null}
nohup node my_app.js &
```

However, you lose the benefits of using *npm* to manage the application.

<Note>
  As above, the *&* places the command in the background, and the *nohup* command ensures that the application continues running even if you log out of the current terminal session.
</Note>

## Stopping a Node.js application

To stop a currently running Node.js application, type the following command:

```bash theme={null}
pkill node
```

This command immediately stops all running Node.js applications.

## Integrating a Node.js application with the web server

Depending on the type of Node.js application you are running, you may want to be able to access it using a web browser. To do this, you need to select an unused port for the Node.js application to listen on, and then define server rewrite rules that redirect visitors to the application. The following steps demonstrate how to do this:

1. In a text editor, add the following lines to the *.htaccess* file in the */home/username/public\_html* directory, where ***username*** represents your account username:

   ```
   DirectoryIndex disabled
   RewriteEngine On
   RewriteRule ^$ http://127.0.0.1:XXXXX/ [P,L]
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)$ http://127.0.0.1:XXXXX/$1 [P,L]
   ```

   > 📘 Note
   >
   > These rewrite rules redirect incoming HTTP visitor requests to the Node.js application running locally on the server. *127.0.0.1* is the loopback address for *localhost*.

2. In both **RewriteRule** lines, replace ***XXXXX*** with the port on which your Node.js application listens.

   > 🚧 Important
   >
   > To run a Node.js application on a managed server, you must select an unused port between **30000** and **50000** (inclusive).

3. Save the changes to the *.htaccess* file, and then exit the text editor. Visitors to your web site are redirected to the Node.js application listening on the specified port.

   > 👍 Tip
   >
   > If your application fails to start, the port you chose may already be in use. Check the application log for error codes like **EADDRINUSE** that indicate the port is in use. If it is, select a different port number, update your application's configuration and the *.htaccess* file, and then try again.

## More information

For more information about Node.js, please visit [http://nodejs.org](http://nodejs.org).
