Node.js is an open source javascript server environment. It allows you to use Javascript for back-end web applications.
Installation
Node.js is not installed on a cPanel VPS by default. Installation requires root server access and can be installed using 2 different methods described below.
Installing from WHM
- Login to WHM to manage your VPS
- Open Easy Apache 4 (Home >> Software >> EasyApache 4)
- Next to “Currently Installed Packages” click on Customize
- Under Additional Packages section, select nodejs10
- Click on Next and then Provision and the Node.js packages will be installed
Installing on the command line
- SSH into your VPS as root
- Run this command:
yum install ea-nodejs10
This installs these packages:
The system stores these applications in the /opt/cpanel/ea-nodejs10/bin/ directory.
Once installed, you run your own Node.js apps by running this command in SSH:
/opt/cpanel/ea-nodejs10/bin/node /home/user/path_to_app/app.js
Custom Node.js application example
- Create a directory for your Node.js application, in this example we will use my_app
- In the my_app directory, create a file named app.js and copy and paste the following
const http = require('http')
const hostname = '127.0.0.1';
const port = 8080;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello! NodeJS is awesome! \n');
});
server.listen(port, hostname, () => {
console.log(`Node.js App Server at http://${hostname}:${port}/`);
});
- Now to run this simple application, run:
/opt/cpanel/ea-nodejs10/bin/node app.js
- Now to test this and see it in action, from another terminal window run this:
curl http://127.0.0.1:8080
- You should see this output from the Node.js application:
root@server [~]# curl http://127.0.0.1:8080
Hello! NodeJS is awesome!