Run node.js on cpanel hosting server

爷,独闯天下 提交于 2019-11-27 13:47:44

问题


It is a simple node.js code.

var http = require('http');
http.createServer(function(req, res) {
    res.writeHead(200, { 'Content-Type' : 'text/plain'});
    res.end('Hello World!');
}).listen(8080);

I uploaded it on cpanel hosting server and installed node.js and run it. If a server is normal server I can check script result by accessing 'http://{serverip}:8080'. But on cpanel is hosting domain and sub domain and every domain is matched by every sites. Even http://{serverip} is not valid url. How can I access my node.js result? Kindly teach me. Thanks. bingbing.


回答1:


cPanel typically runs Apache or another web server that is shared among all the cPanel/unix accounts. The web server listens on port 80. Depending on the domain name in the requested URL, the web server uses "Virtual Hosting" to figure out which cPanel/unix account should process the request, i.e. in which home directory to find the files to serve and scripts to run. If the URL only contains an IP address, cPanel has to default to one of cPanel accounts.

Ordinarily, without root access, a job run by a cPanel account cannot listen on port 80. Indeed, the available ports might be quite restrictive. If 8080 doesn't work, you might try 60000. To access a running node.js server, you'll need to have the port number it's listening on. Since that is the only job listening on that port on that server, you should be able to point your browser to the domain name of any of the cPanel accounts or even the IP address of the server, adding the port number to the URL. But, it's typical to use the domain name for the cPanel account running the node.js job, e.g. http://cPanelDomainName.com:60000/ .

Of course port 80 is the default for web services, and relatively few users are familiar with optional port numbers in URLs. To make things easier for users, you can use Apache to "reverse proxy" requests on port 80 to the port that the node.js process is listening on. This can be done using Apache's RewriteRule directive in a configuration or .htaccess file. This reverse proxying of requests arguably has other benefits as well, e.g. Apache may be a more secure, reliable and manageable front-end for facing the public Internet.

Unfortunately, this setup for node.js is not endorsed by all web hosting companies. One hosting company that supports it, even on its inexpensive shared hosting offerings, is A2Hosting.com. They also have a clearly written description of the setup process in their Knowledge Base.

Finally, it's worth noting that the developers of cPanel are working on built-in node.js support. "If all of the stars align we might see this land as soon as version 68," i.e. perhaps early 2018.

References

Apache Virtual Hosting - http://httpd.apache.org/docs/2.4/vhosts/

Apache RewriteRule Directive - http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html

A2Hosting.com Knowledge Base Article on Configuring Node.js - https://www.a2hosting.com/kb/installable-applications/manual-installations/installing-node-js-on-managed-hosting-accounts

cPanel Feature Request Thread for node.js Support - https://features.cpanel.net/topic/nodejs-hosting

Related StackOverflow Questions

How to host a Node.Js application in shared hosting

Why node.js can't run on shared hosting?




回答2:


Install/Setup NodeJS with CPanel

1. Log in to your account using SSH (it is not enabled for your account contact support team).

2. Download the NodeJS

wget https://nodejs.org/dist/latest/node-v10.0.0-linux-arm64.tar.xz

3. Extract the NodeJS Files

tar xvf node-v10.0.0-linux-arm64.tar.xz

4. Now rename the folder to nodejs name, to do this type the following command

mv node-v10.0.0-linux nodejs

5. Now install node and npm binaries, type the next commands:

mkdir ~/bin
cp nodejs/bin/node ~/bin
cd ~/bin
ln -s ../nodejs/lib/node_modules/npm/bin/npm-cli.js npm

6. Node.js and npm are installed on your account. To verify, type the following commands

node --version
npm --version

The ~/bin directory is in your path by default, which means you can run node and npm from any directory in your account.

7. Start Node.js Application

nohup node my_app.js &

8. Stop the Application

pkill node

9. Integrating a Node.js application with the web server(optional)

Depending on the type of Node.js application you are running, you may want to be able to access it using a web browser. To do this, you need to select an unused port for the Node.js application to listen on, and then define server rewrite rules that redirect visitors to the application.

In a text editor, add the following lines to the .htaccess file in the/home/username/public_html directory, where username represents your account username:

RewriteEngine On
RewriteRule ^$ http://127.0.0.1:XXXXX/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:XXXXX/$1 [P,L]

In both RewriteRule lines, replace XXXXX with the port on which your Node.js application listens. To run a Node.js application on a managed server, you must select an unused port, and the port number must be between 49152 and 65535(inclusive). Save the changes to the .htaccess file, and then exit the text editor. Visitors to your website are redirected to the Node.js application listening on the specified port.

If your application fails to start, the port you chose may already be in use. Check the application log for error codes like EADDRINUSE that indicate the port is in use. If it is, select a different port number, update your application’s configuration and the .htaccess file, and then try again.




回答3:


You can use any domain pointed to that cPanel server and instead of accessing http://server-ip:8080 try accessing http://domain.tld:8080. By default cPanel does not bind on port 8080. Be sure to check if there is any firewall on the server. If it is, then allow incoming connections on tcp port 8080. Depending on your WHM server configuration, it should also work with http://server-ip:8080




回答4:


cPanel Version 80 has nodejs 10.x support: https://documentation.cpanel.net/display/80Docs/80+Release+Notes#id-80ReleaseNotes-InstallanduseNode.jsapplications

Install and use Node.js applications

You can now install and use Node.js applications on your server. To use Node.js, install the ea-nodejs10 module in the Additional Packages section of WHM's EasyApache 4 interface (WHM >> Home >> Software >> EasyApache 4).

You can register Node.js applications in cPanel's Application Manager interface (cPanel >> Home >> Software >> Application Manager). For more information, read our Guide to Node.js Installations documentation.

For Application Manager to be enabled: https://documentation.cpanel.net/display/80Docs/Application+Manager

Your hosting provider must enable the Application Manager feature in WHM's Feature Manager interface (WHM >> Home >> Packages >> Feature Manager).

Your hosting provider must install the following Apache modules:

The ea-ruby24-mod_passengermodule. Note: This module disables Apache's mod_userdir module.

The ea-apache24-mod_env module. Note: This module allows you to add environment variables when you register your application. For more information about environment variables, read the Environment Variables section below.

The ea-nodejs10 module if you want to register a Node.js™ application.

You can see how application manager looks like in this Youtube video: https://www.youtube.com/watch?v=ATxMYzLbRco



来源:https://stackoverflow.com/questions/44079039/run-node-js-on-cpanel-hosting-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!