How to Install and Create a Node Server on Ubuntu VPS

Filed Under: Node.js

NodeJS is a very popular javascript-based backend framework. In this tutorial, we will run how to install NodeJS on an Ubuntu VPS and then create a simple Node HTTP server and access it through the browser.

Installing NodeJS on Ubuntu VPS

I have some apps and websites running on the HostArmada VPS machine. I will use one of those machines for this tutorial. It’s running Ubuntu 20.04 LTS version. We can install NodeJS using the following command.

apt install nodejs

If you are not logged in as root, it might need sudo. In that case, run it as a sudo user.

Install Nodejs Ubuntu
Install Nodejs Ubuntu

Checking NodeJS Version

We can check the NodeJS version to verify that the installation is working fine.

Nodejs Version
Nodejs Version

Creating a Simple NodeJS HTTP Server

Create a file named “nodeapp.js” and paste the following content.

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Congratulations! node.JS has successfully been installed !\n');
}).listen(3000, "45.79.000.000");
console.log('Server running at http://45.79.000.000:3000/');

Please make sure to change the IP address accordingly. I have changed the last parts to hide my machine IP address.

Run the Node app using the following command:

node nodeapp.js

Your console should print the log message that the server is running.

Accessing NodeJS server in Browser

Go to http://server-ip:3000 and you should see the below output.

Nodejs Http Server
Nodejs Http Server

That’s it. We have successfully installed NodeJS application and run a simple HTTP server within 5 minutes.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content