How to Host Node.js Applications on Your Dedicated Server

Node.js is a powerful JavaScript runtime environment that allows developers to build scalable and high-performance applications. With Node.js, you can build everything from APIs to full web applications. This guide will walk you through setting up your dedicated server to host a Node.js application.


Step 1: Update Your Server

Before starting the installation, make sure your server is up to date with the latest security patches and software updates.

  • For Ubuntu/Debian:
    • Run the following command:
      • sudo apt update && sudo apt upgrade -y
  • For CentOS/RHEL:
    • Run the following command:
      • sudo yum update -y

Step 2: Install Node.js

To install Node.js on your server, you'll need to use a package manager for your operating system.

  • Install Node.js on Ubuntu/Debian:

    • Add the NodeSource repository:
      • curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
    • Install Node.js:
      • sudo apt install nodejs -y
    • Verify the installation:
      • node -v
  • Install Node.js on CentOS/RHEL:

    • Add the NodeSource repository:
      • curl -sL https://rpm.nodesource.com/setup_16.x | sudo bash -
    • Install Node.js:
      • sudo yum install nodejs -y
    • Verify the installation:
      • node -v

Step 3: Install npm (Node Package Manager)

npm comes bundled with Node.js, but if it's not installed, follow these steps:

  • For Ubuntu/Debian:
    • Run the following command to install npm:
      • sudo apt install npm -y
  • For CentOS/RHEL:
    • Run the following command to install npm:
      • sudo yum install npm -y

Step 4: Set Up Your Node.js Application

Once Node.js and npm are installed, you can now set up your Node.js application.

  • Create a new directory for your application:

    • Navigate to the directory where you want to store your application:
      • cd /var/www
    • Create a new directory for your project:
      • mkdir my-node-app
      • cd my-node-app
  • Create a simple Node.js application:

    • Create a file called app.js:
      • nano app.js
    • Add a basic Node.js server setup inside app.js:
      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 by pressing CTRL + X, then Y, and ENTER.

Step 5: Run Your Node.js Application

  • Start your Node.js application:
    • Run the following command to start your application:
      • node app.js
    • Your server should now be running and accessible on port 3000. You can test it by opening a browser or using curl:
      • curl http://your-server-ip:3000

Step 6: Set Up a Process Manager (PM2)

To ensure that your Node.js application runs continuously in the background and restarts if the server reboots, use a process manager like PM2.

  • Install PM2 globally:

    • Run the following command:
      • sudo npm install pm2@latest -g
  • Start your application using PM2:

    • Run the following command to start your Node.js application with PM2:
      • pm2 start app.js
    • To check the status of your application, run:
      • pm2 list
    • You can also set PM2 to restart your app if the server reboots:
      • pm2 startup
    • Save the PM2 process list:
      • pm2 save

Step 7: Set Up Nginx as a Reverse Proxy (Optional but Recommended)

Using Nginx as a reverse proxy is a common practice for production applications, as it provides load balancing, security, and performance benefits.

  • Install Nginx:

    • For Ubuntu/Debian:
      • sudo apt install nginx -y
    • For CentOS/RHEL:
      • sudo yum install nginx -y
  • Configure Nginx:

    • Create a new configuration file for your Node.js application:
      • sudo nano /etc/nginx/sites-available/my-node-app
    • Add the following Nginx configuration:
      server {
        listen 80;
        server_name your-server-ip;
      
        location / {
          proxy_pass http://localhost:3000;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection 'upgrade';
          proxy_set_header Host $host;
          proxy_cache_bypass $http_upgrade;
        }
      }
      
    • Enable the site by creating a symbolic link:
      • sudo ln -s /etc/nginx/sites-available/my-node-app /etc/nginx/sites-enabled/
  • Restart Nginx:

    • sudo systemctl restart nginx

Step 8: Set Up SSL for Your Node.js Application (Optional)

For security purposes, it’s recommended to secure your Node.js application with SSL using Let’s Encrypt.

  • Install Certbot:
    • For Ubuntu/Debian:
      • sudo apt install certbot python3-certbot-nginx -y
    • For CentOS/RHEL:
      • sudo yum install certbot python3-certbot-nginx -y
  • Obtain an SSL certificate:
    • Run the following command to obtain and install the SSL certificate:
      • sudo certbot --nginx -d your-domain.com
  • Verify SSL setup:
    • Check your site by navigating to https://your-domain.com.

Step 9: Maintain Your Node.js Application

  • Monitoring:

    • Use PM2 to monitor your application with the following command:
      • pm2 monit
  • Logs:

    • View logs for your Node.js application using:
      • pm2 logs
  • Auto Restart:

    • Ensure your application automatically restarts if it crashes by using the following PM2 command:
      • pm2 restart app.js

Conclusion

Your Node.js application is now successfully hosted on your dedicated server! You have installed Node.js, configured a process manager to keep the app running, set up Nginx as a reverse proxy, and optionally secured your application with SSL. With this setup, your Node.js application is ready to handle production traffic with reliability and security.

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