{"id":57825,"date":"2020-10-20T20:40:50","date_gmt":"2020-10-21T01:40:50","guid":{"rendered":"https:\/\/blog.cpanel.com\/?p=57825"},"modified":"2020-10-20T20:40:50","modified_gmt":"2020-10-21T01:40:50","slug":"how-to-host-a-node-js-application-with-cpanel","status":"publish","type":"post","link":"https:\/\/devel.www.cpanel.net\/blog\/tips-and-tricks\/how-to-host-a-node-js-application-with-cpanel\/","title":{"rendered":"How to Host a Node.JS Application With cPanel"},"content":{"rendered":"\n

Node.js hosting was once a complicated business, especially on multi-user web hosting servers. However, with cPanel & WHM\u2019s Application Manager, <\/em>installing JavaScript applications and configuring Node.js environments is a breeze, empowering web hosts to offer intuitive Node.js hosting services with an easy-to-use graphical interface. <\/p>\n\n\n\n

In this article, we explain how to activate cPanel\u2019s Application Manager <\/em>and how to install, register, and manage Node.js applications. <\/p>\n\n\n\n

Getting Started With the cPanel Application Manager<\/strong><\/h2>\n\n\n\n

cPanel users can register Node.js applications and associate them with a domain in Application Manager<\/em>.  To offer this feature to hosting users, server administrators should first install a handful of support packages and ensure that Application Manager<\/em> is activated.<\/p>\n\n\n\n

We\u2019ll use WHM\u2019s Feature Manager<\/em> to turn on Application Manager<\/em>.  Open Feature Manager, <\/em>which is under Packages<\/em> in the WHM sidebar menu. <\/p>\n\n\n\n

Click the Edit <\/em>button under Manage Feature List <\/em>with the Default <\/em>list selected in the dropdown menu. <\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

Enter Application Manager<\/em> in the search field, and verify that its entry in the feature list is checked. If it isn\u2019t, click the adjacent box and then Save <\/em>at the bottom of the page. <\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

Next, we\u2019ll install the software that allows cPanel to run and manage Node.js applications, including the mod_passenger app server and EasyApache4\u2019s Node environment. You will need to log in as root with SSH<\/a> to install the software:<\/p>\n\n\n\n

yum install ea-ruby24-mod_passenger ea-apache24-mod_env ea-nodejs10<\/code><\/pre>\n\n\n\n

Application Manager <\/em>is now available in user cPanel accounts and ready to host Node.js applications. <\/p>\n\n\n\n

Creating an Application in the cPanel Application Manager<\/strong><\/h2>\n\n\n\n

Hosting applications with cPanel is a two-step process:<\/p>\n\n\n\n

  1. Install the app on the server. <\/li>
  2. Register it with Application Manager<\/em>.<\/li><\/ol>\n\n\n\n

    We\u2019ll look at registering an application in the next section, but first we need to install it on the server. Users of the Git™ version control system could clone a repository with its command-line client or cPanel\u2019s Git Version Control<\/em> interface. However, to illustrate the process, we\u2019ll create a simple single-file application.<\/p>\n\n\n\n

    Log in with SSH<\/a> as a cPanel user and create a new directory in their home directory. <\/p>\n\n\n\n

    mkdir nodejsapp<\/code><\/pre>\n\n\n\n

    You can call the directory anything you want. This is where your app\u2019s code files will live. Create a file called app.js <\/em>in this directory with your preferred text editor. cPanel expects the application\u2019s entry point to be called app.js<\/em>, so it\u2019s best to stick with the standard naming convention unless you have a good reason to change it. <\/em><\/p>\n\n\n\n

    Paste the following code and save the new file. <\/p>\n\n\n\n

    const http = require('http')\nconst hostname = '127.0.0.1';\nconst port = 3000;\n\nconst server = http.createServer((req, res) => {\n  res.statusCode = 200;\n  res.setHeader('Content-Type', 'text\/plain');\n  res.end('Hello World! I am your new NodeJS app! \\n');\n});\n\nserver.listen(port, hostname, () => {\n  console.log(`Server running at http:\/\/${hostname}:${port}\/`);\n});<\/code><\/pre>\n\n\n\n

    The code sets up an HTTP server on port 3000. It responds to web requests with the message: \u201cHello World! I am your new NodeJS app!\u201d  <\/p>\n\n\n\n

    We could go straight to the registration step, but let\u2019s make sure the app works first. Run the following command, which executes our mini-app with EasyApache\u2019s Node.js installation. <\/p>\n\n\n\n

    \/opt\/cpanel\/ea-nodejs10\/bin\/node app.js<\/code><\/pre>\n\n\n\n

    All being well, you will see a message that says:<\/p>\n\n\n\n

    Server running at http:\/\/127.0.0.1:3000<\/code><\/pre>\n\n\n\n

    Finally, open a new terminal window and log in to your server as the same cPanel user. We\u2019ll use the curl <\/em>HTTP client to send the app a request. <\/p>\n\n\n\n

    curl http:\/\/127.0.0.1:3000<\/code><\/pre>\n\n\n\n

    If it responds with \u201c’Hello World! I am a NodeJS app on cPanel,\u201d you can move on to the next stage: registering your Node.js app with cPanel. <\/p>\n\n\n\n

    Registering Your Node.JS Application in the cPanel Application Manager<\/strong><\/h2>\n\n\n\n

    Registering tells the Passenger app manager and cPanel where to find your application and which domain you would like to use. <\/p>\n\n\n\n

    In cPanel, open the Application Manager, <\/em>which you\u2019ll find under Software <\/em>on the main page. <\/p>\n\n\n\n

    \"\"<\/figure>\n\n\n\n

    Click the Register Application <\/em>button.<\/p>\n\n\n\n

    \"\"<\/figure>\n\n\n\n

    In the Application Configuration<\/em> table, we need to provide four pieces of information:<\/p>\n\n\n\n

    \"\"<\/figure>\n\n\n\n