How to install Node.js on a VPS or a Dedicated server

Node.js is a cross-platform JavaScript runtime environment. It’s the technology of choice for creating and hosting many different web-based applications. Programmers also use it to create web servers and networking tools.

In this tutorial, we'll walk you through how to install Node.js and deploy an app on a VPS or a Dedicated server, whether you have an Ubuntu/AlmaLinux server with or without cPanel.


NOTE: The Setup Node.js App feature is available only with CloudLinux OS. Our Shared servers run CloudLinux and include this feature by default. On our Dedicated servers, you can choose CloudLinux OS and install the required components to enable it.


Choose the section that matches your server for installing Node.js:



Server without a control panel


In this section, we will explain the steps required to install Node.js on Ubuntu or AlmaLinux without a control panel.


Log in to the server via SSH


First of all, it is required to access the server in question via SSH as a root user. You can use the following details with the SSH client:

Host Name: the IP of the server;

Port: your server’s port (22 by default);

Connection type: SSH.

In case you are using a Unix-based OS (Linux or macOS), you can easily run the Terminal Application (a command-line emulation program) and connect to the server using the command:

ssh root@serverip -pPORT


Feel free to refer to the guidelines here for detailed login steps.


Preparing the server


We need to ensure that the system packages are up-to-date. Also, it is advised to install curl, which is required to download the NVM installation script and configure the NodeSource repository later in this guide.

For Ubuntu:

apt update && apt upgrade -y

apt install curl -y


For AlmaLinux:

dnf update -y

dnf install curl -y


PLEASE NOTE: Administrative commands are executed as root, while application-related commands (such as installing dependencies, managing application files, and running the application) should be executed as a separate user, in our case: nodetest

Create the user and set its password. You will be prompted to enter a new password. Generate a strong password using any password generator and paste it when prompted after running this command:

useradd -m -s /bin/bash nodetest

passwd nodetest


Next, create a directory for the application and assign ownership to the newly created user:

mkdir -p /var/www/myapp

chown -R nodetest:nodetest /var/www/myapp


Installing Node.js


You can install Node.js via NVM (Node Version Manager) or with the official NodeSource package.

NVM is recommended if you need to manage multiple Node.js versions. It installs Node.js within the installing user's home directory, making it available only to that user by default.

NodeSource is a good choice for a straightforward, system-wide installation. It installs Node.js globally, making it available to all users on the server.


  • Installation with NVM

Download and install the latest NVM version from the official repository. It works the same on Ubuntu and AlmaLinux. Since NVM installs Node.js within the current user's home directory, sudo is not required. You can look up the latest release at the NVM GitHub repository.

NOTE: NVM is user-specific; make sure you are logged in as the separate user that will run the application (for example, nodetest) before proceeding. If you have not switched to this user yet, run su - nodetest.


To install NVM, run:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.41.0/install.sh | bash


Reload your shell and install the latest LTS version:

source ~/.bashrc

nvm install --lts

nvm use --lts


  • Installing the official NodeSource package

You can add the NodeSource repository and install Node.js with the commands below as a root user.

The following example uses the Node.js 26.x repository:

For Ubuntu:

curl -fsSL https://deb.nodesource.com/setup_26.x | sudo -E bash

apt install nodejs -y


NOTE: For a different version, replace setup_26.x with another Node.js version, such as setup_22.x, for instance.


For AlmaLinux, you can add the NodeSource repository and then install Node.js this way:

curl -fsSL https://rpm.nodesource.com/setup_26.x | sudo bash

dnf install nodejs -y


Only one Node.js version from NodeSource can be installed at a time. To switch versions, remove the old one first:

For Ubuntu:

sudo apt remove -y nodejs npm

sudo rm -f /etc/apt/sources.list.d/nodesource.list

rm -f /etc/apt/sources.list.d/nodesource.sources

rm -f /usr/share/keyrings/nodesource.gpg

sudo apt update


For Almalinux:

dnf remove -y nodejs npm

rm -f /etc/yum.repos.d/nodesource*

sudo dnf clean all


After that, you can reinstall a new version.

To check if Node.js was installed from NodeSource, you can run this command:

For Ubuntu:

apt list nodejs --installed


For AlmaLinux:

sudo dnf list installed nodejs


Uploading the app and installing dependencies


Feel free to add application files to the app root folder using any preferred method.

  1. Upload files from local storage

    You can upload files via SSH as the assigned separate application user. This ensures that the user has the necessary permissions to access and manage them.

  2. Clone from a Git repository

    If Git is not installed, install it using the appropriate command for your operating system as a root user:

    For Ubuntu:

    apt install -y git


    For AlmaLinux:

    dnf install -y git


    Then clone your repository:

    su - nodetest

    git clone https://github.com/your-account/your-repo.git /var/www/myapp


    Navigate to the application directory:

    cd /var/www/myapp


    Install the required dependencies:

    npm install



Testing your application


Before configuring a process manager or web server, make sure your application starts correctly. For example, if your application has an app.js startup file, you can run:

node app.js &


NOTE: If your application uses the Yarn package manager, we recommend enabling it through Corepack instead of installing Yarn globally.

Corepack is no longer bundled with Node.js 25 and later versions, so it must be installed separately:

npm install -g corepack

corepack enable


If you start the app with node app.js &, it stops as soon as you close the SSH session.

Depending on the application you use, you may consider using the PM2 module. It will daemonize your Node.js app, keep your app running in the background, restart it if it crashes, and start it after a server reboot.

To stop the app manually, we need to find its process ID by checking its processes:

pgrep -af node


The process ID is the second column from the left. Run this to kill the process:

kill PROCESS_ID


This may also work, but it will kill all Node.js app processes for a specific user:

pkill -u nodetest node


Once Node.js is installed and your application is running, you need to perform some additional configuration to make it accessible through your domain and secure the connection. For example, make sure that the domain is pointed to the server. Further configuration steps are mentioned below.


Setting up Nginx as a reverse proxy


To make your app accessible over standard HTTP/HTTPS ports (80/443) without requiring users to type your app's internal port number, configure Nginx as a reverse proxy.

For non-cPanel servers, the installation procedure depends on the operating system:

Once Nginx is installed, configure it to forward incoming HTTP/HTTPS requests to the port on which your Node.js application is listening. For configuration details, refer to the official NGINX documentation on reverse proxying.


Configuring SSL for your Node.js application


Once your domain is pointed to the server and the application is accessible over HTTP, configure HTTPS.

You can install a free SSL certificate using a third-party ACME client script such as Certbot, which can obtain and install certificates issued by Let's Encrypt.


NOTE: Certbot can be used only for servers without a control panel. It may conflict with cPanel's AutoSSL on a server with cPanel.


For business websites, we offer paid SSL certificates with Organization Validation (OV) and Extended Validation (EV). These certificates include additional business identity validation. You can learn more about the available SSL certificate types.

For instructions on installing a purchased SSL certificate on Nginx, refer to our guide on installing an SSL certificate on Nginx.

For an Apache web server, feel free to refer to the SSL installation here.



Server with cPanel/WHM



Log in to the server via WHM


Locate your Virtual Server access details in the Welcome Email sent to your Namecheap primary email address after your VPS/dedicated server activation.

Copy the WHM URL ( which is https://your-server-ip:2087 or https://server.domain.tld:2087) and paste it into your browser's address bar to log in.

For more details, you may refer to this guide.


Installing Node.js on a server using cPanel


For the following instructions, we used the AlmaLinux operating system with cPanel installed. However, the same basic steps apply to virtually all other Linux distributions supported by cPanel, provided they have Apache as their web server.


NOTE: The Setup Node.js App feature is available only with CloudLinux OS. Our Shared servers run CloudLinux and include this feature by default. On our Dedicated servers, you can choose CloudLinux OS and install the required components to enable it.


This guide describes the manual method that works on any cPanel server. If your server runs CloudLinux and has a Node.js Selector installed, feel free to refer to the steps here on how to work with Node.js app.


  • Installation from EasyApache 4 or OS repositories

cPanel provides its own implementation of Node.js, available in versions 16 (which is the only option available on CentOS 7), 18, 20, and 22. To use the official implementation, we strongly recommend following the official guide by cPanel, which has in-depth information about the cPanel implementation.

You can install the necessary dependencies using WHM. This method works for both Ubuntu and AlmaLinux operating systems with cPanel installed:

  1. Once logged in to WHM, go to Software > EasyApache 4 and click Customize next to your active profile.

  2. Open the Additional Packages section and enable the desired Node.js version (for example, nodejs22):







  3. Click Next until the Review step, and click Provision

They can also be installed via the command line using WHM's Terminal:

  1. Go to WHM > Terminal and run this:

    sudo dnf module list nodejs




  2. Then, from the list above, you can select the desired version and install it. Here is a command for installing version 22 from the AlmaLinux repository:

    sudo dnf module install -y nodejs:22


  3. Node.js and NPM should now appear on the server. You can double-check that they are installed with the following commands:

    node --version

    npm --version



  • Installing the official NodeSource package

In this example, we will be installing Node.js on an Ubuntu and an AlmaLinux server with cPanel in WHM > Terminal.
  1. You can add the NodeSource repository and install Node.js with the commands below as a root user.

    The following example uses the Node.js 26.x repository:

    For Ubuntu:

    curl -fsSL https://deb.nodesource.com/setup_26.x | sudo -E bash

    apt install nodejs -y


    NOTE: For a different version, replace setup_26.x with another Node.js version, such as setup_22.x, for instance.


    For AlmaLinux, you can add the NodeSource repository and then install Node.js this way:

    curl -fsSL https://rpm.nodesource.com/setup_26.x | sudo bash

    dnf install nodejs -y


  2. Only one Node.js version from NodeSource can be installed at a time. To switch versions, remove the old one first:

    For Ubuntu:

    apt remove -y nodejs npm

    rm -f /etc/apt/sources.list.d/nodesource.list

    rm -f /etc/apt/sources.list.d/nodesource.sources

    rm -f /usr/share/keyrings/nodesource.gpg

    apt update


    For Almalinux:

    dnf remove -y nodejs npm

    rm -f /etc/yum.repos.d/nodesource*

    dnf clean all


    After that, you can reinstall a new version.

  3. To check if Node.js was installed from NodeSource, you can run this command:

    For Ubuntu:

    apt list nodejs --installed


    For AlmaLinux:

    dnf list installed nodejs


Once Node.js is installed and your application is running, you need to perform some additional configuration to make it accessible through your domain and secure the connection. For example, make sure that the domain is pointed to the server.


Uploading the app


It is required to upload the application files. In the case of a cPanel server, we suggest uploading your application files within the folder of a cPanel account you intend to use the app on. However, it is not a requirement to use the root directory of a domain (public_html folder for the main domain, for example).

In our example, we will upload the files to the "node" folder within a cPanel account.

  1. Open up your cPanel and go to File Manager:




  2. Click on the + Folder button on the upper-left taskbar (pictured below) and enter the desired name of a folder in the prompt that comes up. Then, press Enter:




  3. Double-click on the newly created folder to enter it.

  4. Click on the Upload button on the upper-left taskbar, click Select File, and select the application archive folder from your computer.

  5. Right-click on the uploaded archive and click Extract.

After extracting, make sure all main application files are in the desired folder. You may move files and folders by selecting them via Ctrl+Click, right-clicking on the selected entries, and clicking Move. Then, you may enter the directory to move them to.

Once the files are in place, the next step is to run the application.


Running Node.js Apps


There are different possible configurations for Node.js apps; as such, there are multiple ways to run them.

First of all, we suggest you run the application itself as your cPanel user, not as a root user.


NOTE: If your application uses the Yarn package manager, we recommend enabling it through Corepack instead of installing Yarn globally.

Corepack is no longer bundled with Node.js 25 and later versions, so it must be installed separately:

npm install -g corepack

corepack enable


First, enable shell access for the cPanel account in WHM > List Accounts > “+” icon > Modify an Account, and then it will be possible to execute the following command while being logged in as a root user in WHM > Terminal (this makes sure the user environment will be loaded):

su - cpanelusername


The cPanel > Terminal menu can also be used to run commands as the cPanel user.

We suggest you refer to guidance provided by the developers of a Node.js application you intend to use. However, in general, there are two ways to run Node.js applications:

  1. To run a Node.js application that is production-ready and has a package.json file included, you can use the following command:

    NODE_ENV=production npm start &


  2. In the case you are running a Node.js app that does not have a package.json file included, you can use the following command (given the main app file is named "my_app.js"):

    node my_app.js &


In this case, you won’t be able to manage this application with npm.


NOTE: Depending on the application used, you may also consider using the PM2 module. It will daemonize your Node.js app and keep it running in the background, monitor its status, and restart it if it crashes


To stop the app manually, we need to find its process ID by checking its processes:

pgrep -af node


The process ID is the second column from the left. Run this to kill the process:

kill PROCESS_ID


This may also work, but it will kill all Node.js app processes:

pkill -u cpanelusername node



Managing Node.js applications with cPanel Application Manager


If Application Manager is enabled on your server, you can use it to register, deploy, and manage Node.js applications directly from your cPanel account.

To check if it is enabled, you can go to Feature Manager and see if the feature is enabled for your cPanel user:

To ensure it shows up on the cPanel user side, make sure that mod_env and mod_passenger modules are installed via WHM > EasyApache 4.

For more information, feel free to refer to the official cPanel documentation about Application Manager.


NOTE: Application Manager is not the same as CloudLinux's Setup Node.js App. Application Manager uses Phusion Passenger and is available on supported cPanel environments when the required packages and features are enabled.


Connect your Website with a Running Node.js App


Apache is standard for cPanel installations. If you run Apache, you can use a specific .htaccess file code to make your site work with the Node.js app. Follow these steps:

  1. Open up your cPanel and go to the File Manager > public_html folder.




    NOTE: public_html is the folder of your site if the domain you are using is the primary domain of the given cPanel account. In case your domain is an addon domain, please check the cPanel > Domains menu to see what folder it uses.


  2. Click on the Settings button in the upper-right and make sure that the Show Hidden Files (dotfiles) option is checked. Click Save after that.




  3. Click on the .htaccess file in the folder and click Edit:




    If the .htaccess file doesn't exist yet, create it using the + File option in the upper-left corner.

  4. Enter the following code:

    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]

    Now, replace XXXXX with the port your Node.js application is running on.

  5. Click Save Changes in the upper-right:




That’s it! Your site should now display the Node.js app.


Setting up Nginx as a reverse proxy


You can install Nginx in WHM > Home > Software > NGINX Manager if you want Nginx installed on the server:

More information on NGINX Manager is provided in this cPanel documentation article.

Once Nginx is installed, configure it to forward incoming HTTP/HTTPS requests to the port on which your Node.js application is running.

For configuration details, refer to the official NGINX Reverse Proxy documentation.

The guidelines here will be helpful for the server with cPanel installed.


Configuring SSL for your Node.js application


Once your domain is pointed to the server and the application is accessible over HTTP, configure HTTPS.

The SSL certificates can be installed via AutoSSL. For business websites, we offer paid SSL certificates with Organization Validation (OV) and Extended Validation (EV). These certificates include additional business identity validation. You can learn more about the available SSL certificate types.



Troubleshooting


Port already in use (EADDRINUSE)

The error “Error: listen EADDRINUSE: address already in use :::3000” means another process is already using the port your app is trying to use.

  1. Find the process that uses the port with the command sudo ss -tulpn | grep :3000
  2. If it is an old copy of your app managed by PM2, disable it with pm2 stop myapp. Otherwise, stop it with kill PID, using the process ID from the output above.
  3. Then, change the port in your app and update the port in the Nginx proxy_pass line (Ubuntu) or in the .htaccess file (cPanel).

The app stops after closing SSH

The app was started in your terminal session, so it ends when the session ends. Run it with the PM2 module.


502 Bad Gateway

The web server cannot reach your app. Check the following:

  • Is the app running? Run pm2 status. If the status is errored or stopped, check the logs with pm2 logs myapp. To get detailed output, you can also run the app manually with node app.js& and read the error it prints.
  • Does the port in the Nginx proxy_pass line (Ubuntu) or in the .htaccess file (cPanel) match the port your app listens on?
  • Check the web server error log:
    • Ubuntu: sudo tail -n 50 /var/log/nginx/error.log
    • cPanel: sudo tail -n 50 /etc/apache2/logs/error_log
  • On cPanel, also make sure that the mod_proxy and mod_proxy_http modules are enabled in WHM > EasyApache 4 > Apache Modules.

Permission errors

  • EACCES: permission denied when installing global packages: don't use sudo npm for this. Install Node.js with NVM, or install global packages as root. You can configure npm to use a directory you have write access to, as explained in the following NPM-related documentation.
  • listen EACCES: permission denied {IP_ADDRESS}: only root can use ports below 1024. Run your app on a port such as 3000 and let Nginx or Apache pass the traffic to it.
  • Permission denied on application files: make sure the user that runs the app owns the folder:
    • On non-cPanel servers:

      sudo chown -R nodetest:nodetest /var/www/myapp

    • On servers with cPanel, files should belong to the cPanel user:

      chown -R cpanelusername:cpanelusername /home/cpanelusername/myapp



Frequently asked questions


Shared or VPS/dedicated server hosting for Node.js?

A shared hosting plan is enough for small apps with the Setup Node.js App menu. Choose a VPS or a dedicated server if you need a newer Node.js version, background processes, WebSockets, custom ports, or full control over the server. Please note that the Setup Node.js App menu from Shared hosting is not available on VPS.

How much RAM does a Node.js app need?

It depends on the application. A small website or API can often run on 1 GB of RAM. Larger apps, apps with many dependencies, or ones that build on the server (for example, Next.js) may need 2 GB or more. You can check the steps here on how to check the real usage and manage it

Can I host several apps on one VPS/dedicated server?

Yes. Run each app on its own port. Then, add a separate Nginx server block (or a separate domain's .htaccess file on cPanel) for each domain, pointing to the matching port. If you use Let's Encrypt, request a certificate for each domain with Certbot. The apps share the resources of the server, so keep an eye on RAM and CPU usage.

Updated
Viewed
60949 times

Need help? We're always here for you.

notmyip