{"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 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 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 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 Application Manager <\/em>is now available in user cPanel accounts and ready to host Node.js applications. <\/p>\n\n\n\n Hosting applications with cPanel is a two-step process:<\/p>\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 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 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 All being well, you will see a message that says:<\/p>\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 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 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 Click the Register Application <\/em>button.<\/p>\n\n\n\n In the Application Configuration<\/em> table, we need to provide four pieces of information:<\/p>\n\n\n\n The Deployment Environment <\/em>selector indicates whether the app is under development or ready for deployment in a production environment. <\/p>\n\n\n\n Underneath the 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\n\n\n For now, we\u2019ll click the 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\n\n\n Unlike a real app, our one-pager has no external dependencies. If it did, cPanel Application Manager <\/em>would display the Ensure Dependencies <\/em>button for one-click installation. The button is only available for apps with a package.json <\/em>file that lists dependencies. <\/p>\n\n\n\n If you\u2019re following our examples and would like to see Ensure Dependencies<\/em> in action, add a file called package,json <\/em>to the app\u2019s directory with the following contents:<\/p>\n\n\n\n As you can see, we have listed the Express web framework as a dependency. The Ensure Dependencies <\/em>button should now be available in Application Manager. <\/em>When you click it, <\/em>cPanel installs Express and any applications and libraries it depends on. <\/p>\n\n\n\n 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. Servers use environment variables to hold useful configuration data. <\/p>\n\n\n\n For example, CentOS uses the PATH environment variable<\/a> 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. If there was no PATH variable, you\u2019d have to enter the full path of every command: \u201c\/usr\/bin\/ls\u201d instead of just \u201cls.\u201d <\/p>\n\n\n\n The server administrator can change the PATH environment variable, controlling the directories the shell looks in without modifying its internal code. With cPanel\u2019s 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\n\n\n In our simple app.js <\/em>file, this line of JavaScript tells the app the port it should listen for connections on. <\/p>\n\n\n\n In reality, a developer is unlikely to know the correct port when they write the code. 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\n\n\n In Node.js, the JavaScript code looks something like this:<\/p>\n\n\n\n This sets the app\u2019s internal PORT variable to the value of the environment variable<\/a> called PORT or to a default of 3000 if an environment variable isn\u2019t set. <\/p>\n\n\n\n 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 Application Manager, <\/em>find the Environment Variables <\/em>interface beneath Application Configuration <\/em>and click Add Variable.<\/em><\/p>\n\n\n\n Enter a variable name and value, and click Save. <\/em><\/p>\n\n\n\n You can edit a Node application\u2019s registration configuration in the Application Manager.<\/em><\/p>\n\n\n\n Click the Edit <\/em>link in the Actions<\/em> column. In this interface, you can edit all the values you set when first registering your application, including the environment variables. <\/p>\n\n\n\n To uninstall or \u201cunregister \u201da Node.js application from cPanel, click the unregister <\/em>link at the right of the Application Manager <\/em>table. <\/p>\n\n\n\n When you unregister an application, cPanel removes it from the list, deletes its configuration file, and dissociates it from the domain. cPanel does not delete the application\u2019s files, but you are free to delete them yourself in File Manager <\/em>or on the command line. <\/p>\n\n\n\n PHP remains the most widely used server language by a large margin, but Node.js, JavaScript, and web frameworks like Express, Meteor, and Nest are increasingly popular with web developers and tech-savvy users. cPanel and WHM\u2019s Application Manager <\/em>makes it easy for web hosts to support Node apps and JavaScript-focused users on their platform.<\/p>\n\n\n\n As always, if you have any feedback or comments, please let us know. We are here to help in the best ways we can. You\u2019ll find us on Discord<\/a>, the cPanel forums<\/a>, and Reddit<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":" Node.js hosting was once a complicated business, especially on multi-user web hosting servers. However, with cPanel & 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 […]<\/p>\n","protected":false},"author":77,"featured_media":65553,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[61],"tags":[],"class_list":["post-57825","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tips-and-tricks"],"acf":[],"yoast_head":"\nGetting Started With the cPanel Application Manager<\/strong><\/h2>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
yum install ea-ruby24-mod_passenger ea-apache24-mod_env ea-nodejs10<\/code><\/pre>\n\n\n\n
Creating an Application in the cPanel Application Manager<\/strong><\/h2>\n\n\n\n
mkdir nodejsapp<\/code><\/pre>\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
\/opt\/cpanel\/ea-nodejs10\/bin\/node app.js<\/code><\/pre>\n\n\n\n
Server running at http:\/\/127.0.0.1:3000<\/code><\/pre>\n\n\n\n
curl http:\/\/127.0.0.1:3000<\/code><\/pre>\n\n\n\n
Registering Your Node.JS Application in the cPanel Application Manager<\/strong><\/h2>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
{\n \"name\": \"nodejsapp\",\n \"version\": \"1.0.0\",\n \"description\": \"A Node App\",\n \"main\": \"app.js\",\n \"author\": \"\",\n \"license\": \"ISC\",\n \"dependencies\": {\n \"express\": \"^4.17.1\"\n }\n}<\/code><\/pre>\n\n\n\n
Adding Environment Variables to Your Node.JS App\u2019s Environment<\/strong><\/h2>\n\n\n\n
const port = 3000;<\/code><\/pre>\n\n\n\n
const PORT = process.env.PORT || 3000;<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
Editing Your Node.JS Application<\/strong><\/h2>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
Uninstalling Your Node.JS Application<\/strong><\/h2>\n\n\n\n
<\/figure>\n\n\n\n
Bring Node.JS Hosting to Your cPanel Server<\/strong><\/h2>\n\n\n\n