{"id":548,"date":"2024-08-27T12:00:33","date_gmt":"2024-08-27T12:00:33","guid":{"rendered":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/?p=548"},"modified":"2024-08-26T12:10:33","modified_gmt":"2024-08-26T12:10:33","slug":"nodejs-application-cpanel","status":"publish","type":"post","link":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/nodejs-application-cpanel\/","title":{"rendered":"How to Host a Node.JS Application With cPanel"},"content":{"rendered":"<p>Node.js hosting was once a complicated business, especially on multi-user web hosting servers. However, with cPanel &amp; WHM\u2019s <em>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<p>In this article, we explain how to activate cPanel\u2019s <em>Application Manager <\/em>and how to install, register, and manage Node.JS Application.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_83 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/nodejs-application-cpanel\/#Getting_Started_With_the_cPanel_Application_Manager\" >Getting Started With the cPanel Application Manager<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/nodejs-application-cpanel\/#Creating_an_Application_in_the_cPanel_Application_Manager\" >Creating an Application in the cPanel Application Manager<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/nodejs-application-cpanel\/#Registering_Your_NodeJS_Application_in_the_cPanel_Application_Manager\" >Registering Your Node.JS Application in the cPanel Application Manager<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/nodejs-application-cpanel\/#Adding_Environment_Variables_to_Your_NodeJS_Application_Environment\" >Adding Environment Variables to Your Node.JS Application Environment<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Getting_Started_With_the_cPanel_Application_Manager\"><\/span><strong>Getting Started With the cPanel Application Manager<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>cPanel users can register Node.js applications and associate them with a domain in<em> Application Manager<\/em>.\u00a0 To offer this feature to hosting users, server administrators should first install a handful of support packages and ensure that <em>Application Manager<\/em> is activated.<\/p>\n<p>We\u2019ll use WHM\u2019s <em>Feature Manager<\/em> to turn on <em>Application Manager<\/em>.\u00a0 Open <em>Feature Manager, <\/em>which is under <em>Packages<\/em> in the WHM sidebar menu.<\/p>\n<p>Click the <em>Edit <\/em>button under <em>Manage Feature List <\/em>with the <em>Default <\/em>list selected in the dropdown menu.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-549\" src=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/feature-1024x332.jpg\" alt=\"\" width=\"640\" height=\"208\" srcset=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/feature-1024x332.jpg 1024w, https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/feature-300x97.jpg 300w, https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/feature-768x249.jpg 768w, https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/feature-1536x497.jpg 1536w, https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/feature.jpg 1717w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Enter <em>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 <em>Save <\/em>at the bottom of the page.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-551\" src=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/application-manager-1024x562.png\" alt=\"\" width=\"640\" height=\"351\" srcset=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/application-manager-1024x562.png 1024w, https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/application-manager-300x165.png 300w, https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/application-manager-768x421.png 768w, https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/application-manager-1536x842.png 1536w, https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/application-manager.png 1579w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>Next, we\u2019ll install the software that allows cPanel to run and manage Node.JS Application, including the mod_passenger app server and EasyApache4\u2019s Node environment<\/p>\n<p><em>Application Manager <\/em>is now available in user cPanel accounts and ready to host Node.js applications.<\/p>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_an_Application_in_the_cPanel_Application_Manager\"><\/span><strong>Creating an Application in the cPanel Application Manager<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Hosting applications with cPanel is a two-step process:<\/p>\n<ol class=\"wp-block-list\">\n<li>Install the app on the server.<\/li>\n<li>Register it with <em>Application Manager<\/em>.<\/li>\n<\/ol>\n<p>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&#x2122; version control system could clone a repository with its command-line client or cPanel\u2019s <em>Git Version Control<\/em> interface. However, to illustrate the process, we\u2019ll create a simple single-file application.<\/p>\n<p>Log in with SSH as a cPanel user and create a new directory in their home directory.<\/p>\n<pre class=\"wp-block-code\"><code>mkdir nodejsapp<\/code><\/pre>\n<p>You can call the directory anything you want. This is where your app\u2019s code files will live. Create a file called <em>app.js <\/em>in this directory with your preferred text editor. cPanel expects the application\u2019s entry point to be called <em>app.js<\/em>, so it\u2019s best to stick with the standard naming convention unless you have a good reason to change it<em>.\u00a0<\/em><\/p>\n<p>Paste the following code and save the new file.<\/p>\n<pre class=\"wp-block-code\"><code>const http = require('http')\r\nconst hostname = '127.0.0.1';\r\nconst port = 3000;\r\n\r\nconst server = http.createServer((req, res) =&gt; {\r\n  res.statusCode = 200;\r\n  res.setHeader('Content-Type', 'text\/plain');\r\n  res.end('Hello World! I am your new NodeJS app! \\n');\r\n});\r\n\r\nserver.listen(port, hostname, () =&gt; {\r\n  console.log(`Server running at http:\/\/${hostname}:${port}\/`);\r\n});<\/code><\/pre>\n<p>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<p>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<pre class=\"wp-block-code\"><code>\/opt\/cpanel\/ea-nodejs10\/bin\/node app.js<\/code><\/pre>\n<p>All being well, you will see a message that says:<\/p>\n<pre class=\"wp-block-code\"><code>Server running at http:\/\/127.0.0.1:3000<\/code><\/pre>\n<p>Finally, open a new terminal window and log in to your server as the same cPanel user. We\u2019ll use the <em>curl <\/em>HTTP client to send the app a request.<\/p>\n<pre class=\"wp-block-code\"><code>curl http:\/\/127.0.0.1:3000<\/code><\/pre>\n<p>If it responds with \u201c\u2019Hello 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<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Registering_Your_NodeJS_Application_in_the_cPanel_Application_Manager\"><\/span><strong>Registering Your Node.JS Application in the cPanel Application Manager<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Registering tells the Passenger app manager and cPanel where to find your application and which domain you would like to use.<\/p>\n<p>In cPanel, open the <em>Application Manager, <\/em>which you\u2019ll find under <em>Software <\/em>on the main page.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-552\" src=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/WhatsApp-Image-2024-08-26-at-12.55.20-PM.jpeg\" alt=\"\" width=\"995\" height=\"292\" srcset=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/WhatsApp-Image-2024-08-26-at-12.55.20-PM.jpeg 995w, https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/WhatsApp-Image-2024-08-26-at-12.55.20-PM-300x88.jpeg 300w, https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/WhatsApp-Image-2024-08-26-at-12.55.20-PM-768x225.jpeg 768w\" sizes=\"auto, (max-width: 995px) 100vw, 995px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Click the <em>Register Application <\/em>button.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-554\" src=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/register-application-1024x286.png\" alt=\"\" width=\"640\" height=\"179\" srcset=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/register-application-1024x286.png 1024w, https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/register-application-300x84.png 300w, https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/register-application-768x214.png 768w, https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/register-application-1536x429.png 1536w, https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/register-application.png 1674w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>In the <em>Application Configuration<\/em> table, we need to provide four pieces of information:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-555 size-large\" src=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/APPLICAtionamanger1-1024x679.png\" alt=\"Node.JS Application\" width=\"1024\" height=\"679\" srcset=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/APPLICAtionamanger1-1024x679.png 1024w, https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/APPLICAtionamanger1-300x199.png 300w, https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/APPLICAtionamanger1-768x509.png 768w, https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-content\/uploads\/2024\/08\/APPLICAtionamanger1.png 1350w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<ul class=\"wp-block-list\">\n<li><strong>Application Name: <\/strong>A display name for the application. You can choose any name, but it must start with a number or letter, and it can\u2019t contain periods.<\/li>\n<li><strong>Deployment Domain: <\/strong>The app\u2019s domain name. The domain must already be associated with the cPanel account.<\/li>\n<li><strong>Base Application URL: <\/strong>The app\u2019s home page will be available at the <em>deployment domain + the base URL. <\/em>For example, if the <em>Deployment Domain <\/em>is \u201cexample.com\u201d and you enter \u201cnodeapp\u201d in this field, the app will be served from \u201cexample.com\/nodeapp.\u201d The URL extension does not have to be the same as the directory that stores the app\u2019s files.<\/li>\n<li><strong>Application Path: <\/strong>The location of the app\u2019s files relative to the user\u2019s home. In our example, we created a directory in home called \u201cnodejsapp,\u201d so we\u2019d enter \u201cnodejsapp\u201d here.<\/li>\n<\/ul>\n<p>The <em>Deployment Environment <\/em>selector indicates whether the app is under development or ready for deployment in a production environment.<\/p>\n<p>Underneath the <em>Application Configuration <\/em>table is an interface for configuring environment variables. We\u2019ll look at them in greater depth in the next section.<\/p>\n<p>For now, we\u2019ll click the <em>Deploy <\/em>button at the bottom of the page, and cPanel will configure the app\u2019s environment and domain. Once it\u2019s finished, you can open your new app in a web browser at the URL you just configured: \u201cexample.com\/nodeapp\u201d in our examples.<\/p>\n<p>Unlike a real app, our one-pager has no external dependencies. If it did, cPanel <em>Application Manager <\/em>would display the <em>Ensure Dependencies <\/em>button for one-click installation. The button is only available for apps with a <em>package.json <\/em>file that lists dependencies.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-57873\" src=\"https:\/\/blog.cpanel.com\/wp-content\/uploads\/2020\/10\/cpanel-nodejs-ensure-dependencies.png\" alt=\"Node.JS Application\" width=\"1305\" height=\"399\" \/><\/p>\n<p>If you\u2019re following our examples and would like to see <em>Ensure Dependencies<\/em> in action, add a file called <em>package,json <\/em>to the app\u2019s directory with the following contents:<\/p>\n<pre class=\"wp-block-code\"><code>{\r\n  \"name\": \"nodejsapp\",\r\n  \"version\": \"1.0.0\",\r\n  \"description\": \"A Node App\",\r\n  \"main\": \"app.js\",\r\n  \"author\": \"\",\r\n  \"license\": \"ISC\",\r\n  \"dependencies\": {\r\n    \"express\": \"^4.17.1\"\r\n  }\r\n}<\/code><\/pre>\n<p>As you can see, we have listed the Express web framework as a dependency.\u00a0 The <em>Ensure Dependencies <\/em>button should now be available in <em>Application Manager. <\/em>When you click it<em>, <\/em><a href=\"https:\/\/www.hyderabadwebhosting.co.in\/cpanel-web-hosting.html\">cPanel<\/a> installs Express and any applications and libraries it depends on.<\/p>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Adding_Environment_Variables_to_Your_NodeJS_Application_Environment\"><\/span><strong>Adding Environment Variables to Your Node.JS Application Environment<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Environment variables are name-value pairs that store a chunk of information. They work much like variables in programming, except that they are not internal to the program but part of its external environment.\u00a0 Servers use environment variables to hold useful configuration data.<\/p>\n<p>For example, CentOS uses the PATH environment variable to store a list of directories that contain command-line programs. When you enter a command like \u201cls,\u201d the shell looks through those directories for an executable file with that name.\u00a0 If there was no PATH variable, you\u2019d have to enter the full path of every command:\u00a0 \u201c\/usr\/bin\/ls\u201d instead of just \u201cls.\u201d<\/p>\n<p>The server administrator can change the PATH environment variable, controlling the directories the shell looks in without modifying its internal code.\u00a0 With cPanel\u2019s <em>Application Manager, <\/em>you can add environment variables to your Node.js app\u2019s environment to configure its behavior in the same way.<\/p>\n<p>In our simple <em>app.js <\/em>file, this line of JavaScript tells the app the port it should listen for connections on.<\/p>\n<pre class=\"wp-block-code\"><code>const port = 3000;<\/code><\/pre>\n<p>In reality, a developer is unlikely to know the correct port when they write the code.\u00a0 To give users more flexibility, they could tell the app to look in its environment for a variable called PORT and to use its value when setting up the app.<\/p>\n<p>In Node.js, the JavaScript code looks something like this:<\/p>\n<pre class=\"wp-block-code\"><code>const PORT = process.env.PORT || 3000;<\/code><\/pre>\n<p>This sets the app\u2019s internal PORT variable to the <a href=\"https:\/\/nodejs.org\/dist\/latest-v8.x\/docs\/api\/process.html#process_process_env\" target=\"_blank\" rel=\"noopener\">value of the environment variable<\/a> called PORT or to a default of 3000 if an environment variable isn\u2019t set.<\/p>\n<p>Now the user can tell the app which port to bind to by setting a PORT variable in the app\u2019s environment. To do this in cPanel\u2019s <em>Application Manager, <\/em>find the <em>Environment Variables <\/em>interface beneath <em>Application Configuration <\/em>and click <em>Add Variable.<\/em><\/p>\n<p><img decoding=\"async\" class=\"wp-image-57877 aligncenter\" src=\"https:\/\/blog.cpanel.com\/wp-content\/uploads\/2020\/10\/05-cpanel-nodejs-environment-variable.png\" alt=\"\" \/><\/p>\n<p>Enter a variable name and value, and click <em>Save.\u00a0<\/em><\/p>\n<p><img decoding=\"async\" class=\"wp-image-57881 aligncenter\" src=\"https:\/\/blog.cpanel.com\/wp-content\/uploads\/2020\/10\/06-cpanel-save-environment-variable.png\" alt=\"\" \/><\/p>\n<p>From the above post you can know to host Node.JS Application in cpanel.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Node.js hosting was once a complicated business, especially on multi-user web hosting servers. However, with cPanel &amp; WHM\u2019s Application Manager, 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. In this article, we explain how to activate cPanel\u2019s Application Manager [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-548","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/posts\/548","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/comments?post=548"}],"version-history":[{"count":11,"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/posts\/548\/revisions"}],"predecessor-version":[{"id":564,"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/posts\/548\/revisions\/564"}],"wp:attachment":[{"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/media?parent=548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/categories?post=548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/tags?post=548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}