How to Set Up Node.js on Your VPS

Node.js is a powerful, open-source, server-side runtime environment for running JavaScript code. It is widely used for building scalable network applications. This guide will walk you through the process of setting up Node.js on your VPS.

Step 1: Update Your VPS Packages
Before installing Node.js, ensure your system packages are up to date.

  • Connect to your VPS using SSH.
  • Run the following command to update your package lists:
    sudo apt update  
    
  • Upgrade the installed packages with:
    sudo apt upgrade  
    

Step 2: Install Node.js
Node.js can be installed in several ways. Using NodeSource is a recommended method for the latest version.

  • Add the NodeSource repository:
    curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -  
    
    (Replace 16.x with your preferred version, such as 18.x for the latest LTS.)
  • Install Node.js:
    sudo apt install -y nodejs  
    

Step 3: Verify the Installation
Confirm that Node.js and npm (Node Package Manager) are installed correctly.

  • Check the Node.js version:
    node -v  
    
  • Check the npm version:
    npm -v  
    

Step 4: Install Build Tools (Optional)
For some npm packages, you may need to install additional build tools.

  • Install the required tools:
    sudo apt install -y build-essential  
    

Step 5: Set Up a Simple Node.js Application
Create a basic Node.js application to test your setup.

  • Create a new directory for your application:
    mkdir my-node-app  
    cd my-node-app  
    
  • Initialize a new Node.js project:
    npm init -y  
    
  • Create a simple JavaScript file:
    nano app.js  
    
    Add the following content to the file:
    const http = require('http');  
    const port = 3000;  
    
    const server = http.createServer((req, res) => {  
      res.statusCode = 200;  
      res.setHeader('Content-Type', 'text/plain');  
      res.end('Hello, World!\n');  
    });  
    
    server.listen(port, () => {  
      console.log(`Server running at http://localhost:${port}/`);  
    });  
    
  • Save and exit the file.

Step 6: Run Your Node.js Application
Start the Node.js application to ensure everything is working as expected.

  • Run the application:
    node app.js  
    
  • Open your browser and navigate to http://<your-vps-ip>:3000. You should see "Hello, World!" displayed.

Step 7: Use a Process Manager (Optional)
For production environments, it's a good idea to use a process manager like PM2 to keep your application running.

  • Install PM2:
    sudo npm install -g pm2  
    
  • Start your application with PM2:
    pm2 start app.js  
    
  • Save the PM2 configuration to restart automatically on reboot:
    pm2 startup  
    pm2 save  
    

Note: Setting up Node.js on your VPS opens the door to building robust and scalable applications. For enhanced performance, consider securing your application with HTTPS and optimizing your server settings.

Was this answer helpful? 0 Users Found This Useful (0 Votes)