在 cpanel 托管服务器上运行 node.js
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44079039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run node.js on cpanel hosting server
提问by bb14816
It is a simple node.js code.
这是一个简单的 node.js 代码。
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.
我将它上传到 cpanel 托管服务器并安装了 node.js 并运行它。如果服务器是普通服务器,我可以通过访问“http://{serverip}:8080”来检查脚本结果。但是在 cpanel 上托管域和子域,并且每个域都与每个站点匹配。甚至 http://{serverip} 也不是有效的 url。如何访问我的 node.js 结果?请教我。谢谢。冰冰。
回答by Matee Gojra
Install/Setup NodeJS with CPanel
使用 CPanel 安装/设置 NodeJS
1. Log in to your account using SSH (it is not enabled for your account contact support team).
1. 使用 SSH 登录您的帐户(您的帐户联系支持团队未启用)。
2. Download the NodeJS
2. 下载 NodeJS
wget https://nodejs.org/dist/latest/node-v10.0.0-linux-arm64.tar.xz
wget https://nodejs.org/dist/latest/node-v10.0.0-linux-arm64.tar.xz
3. Extract the NodeJS Files
3. 解压 NodeJS 文件
tar xvf node-v10.0.0-linux-arm64.tar.xz
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
4. 现在将文件夹重命名为 nodejs 名称,要执行此操作,请键入以下命令
mv node-v10.0.0-linux nodejs
mv node-v10.0.0-linux nodejs
5. Now install node and npm binaries, type the next commands:
5. 现在安装 node 和 npm 二进制文件,输入以下命令:
mkdir ~/bin
cp nodejs/bin/node ~/bin
cd ~/bin
ln -s ../nodejs/lib/node_modules/npm/bin/npm-cli.js npm
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
6. Node.js 和 npm 已安装在您的帐户中。要验证,请键入以下命令
node --version
npm --version
节点 --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.
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
7. 启动 Node.js 应用程序
nohup node my_app.js &
nohup 节点 my_app.js &
8. Stop the Application
8. 停止应用程序
pkill node
pkill 节点
9. Integrating a Node.js application with the web server(optional)
9. 将 Node.js 应用程序与 Web 服务器集成(可选)
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.
根据您运行的 Node.js 应用程序的类型,您可能希望能够使用 Web 浏览器访问它。为此,您需要为 Node.js 应用程序选择一个未使用的端口进行侦听,然后定义将访问者重定向到应用程序的服务器重写规则。
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:
在文本编辑器中,将以下行添加到 /home/username/public_html 目录中的 .htaccess 文件,其中 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/ [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.
在两行 RewriteRule 中,将 XXXXX 替换为您的 Node.js 应用程序侦听的端口。要在托管服务器上运行 Node.js 应用程序,您必须选择一个未使用的端口,并且端口号必须介于 49152 和 65535(含)之间。将更改保存到 .htaccess 文件,然后退出文本编辑器。您网站的访问者被重定向到侦听指定端口的 Node.js 应用程序。
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.
如果您的应用程序无法启动,则您选择的端口可能已在使用中。检查应用程序日志中是否有错误代码,例如 EADDRINUSE,表明端口正在使用中。如果是,请选择其他端口号,更新应用程序的配置和 .htaccess 文件,然后重试。
回答by aap
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.
cPanel 通常运行 Apache 或其他在所有 cPanel/unix 帐户之间共享的 Web 服务器。Web 服务器侦听端口 80。根据请求的 URL 中的域名,Web 服务器使用“虚拟主机”来确定应该处理请求的 cPanel/unix 帐户,即在哪个主目录中查找要提供的文件和要运行的脚本。如果 URL 仅包含 IP 地址,则 cPanel 必须默认为 cPanel 帐户之一。
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/.
通常,在没有 root 访问权限的情况下,由 cPanel 帐户运行的作业无法侦听端口 80。实际上,可用端口可能非常有限。如果 8080 不起作用,您可以尝试 60000。要访问正在运行的 node.js 服务器,您需要有它正在侦听的端口号。由于这是在该服务器上侦听该端口的唯一工作,因此您应该能够将浏览器指向任何 cPanel 帐户的域名,甚至是服务器的 IP 地址,将端口号添加到 URL。但是,通常使用域名作为运行 node.js 作业的 cPanel 帐户,例如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.
当然,80 端口是 Web 服务的默认端口,而熟悉 URL 中可选端口号的用户相对较少。为了让用户更轻松,您可以使用 Apache 将端口 80 上的请求“反向代理”到 node.js 进程正在侦听的端口。这可以在配置或 .htaccess 文件中使用 Apache 的 RewriteRule 指令来完成。这种请求的反向代理可以说还有其他好处,例如,Apache 可能是面向公共 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.
不幸的是,node.js 的这种设置并未得到所有网络托管公司的认可。A2Hosting.com 是一家支持它的托管公司,即使是在其廉价的共享托管产品上。他们的知识库中还对设置过程进行了清晰的书面描述。
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.
最后,值得注意的是,cPanel 的开发人员正在致力于内置 node.js 支持。“如果所有的星星都对齐,我们可能会在 68 版中看到这片土地,”即可能在 2018 年初。
References
参考
Apache Virtual Hosting - http://httpd.apache.org/docs/2.4/vhosts/
Apache 虚拟主机 - http://httpd.apache.org/docs/2.4/vhosts/
Apache RewriteRule Directive - http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
Apache RewriteRule 指令 - 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
A2Hosting.com 关于配置 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
用于 node.js 支持的 cPanel 功能请求线程 - https://features.cpanel.net/topic/nodejs-hosting
Related StackOverflow Questions
相关的 StackOverflow 问题
How to host a Node.Js application in shared hosting
回答by Bogdan Stoica
You can use any domain pointed to that cPanel server and instead of accessing http://server-ip:8080try 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
您可以使用指向该 cPanel 服务器的任何域,而不是访问http://server-ip:8080尝试访问http://domain.tld:8080。默认情况下,cPanel 不绑定端口 8080。请务必检查服务器上是否有任何防火墙。如果是,则允许 tcp 端口 8080 上的传入连接。根据您的 WHM 服务器配置,它也应该与http://server-ip:8080 一起使用
回答by Savvas Radevic
cPanel Version 80 has nodejs 10.x support: https://documentation.cpanel.net/display/80Docs/80+Release+Notes#id-80ReleaseNotes-InstallanduseNode.jsapplications
cPanel 80 版支持 nodejs 10.x: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.
安装和使用 Node.js 应用程序
您现在可以在您的服务器上安装和使用 Node.js 应用程序。要使用 Node.js,请在 WHM 的 EasyApache 4 界面(WHM >> Home >> Software >> EasyApache 4)的 Additional Packages 部分安装 ea-nodejs10 模块。
您可以在 cPanel 的应用程序管理器界面(cPanel >> 主页 >> 软件 >> 应用程序管理器)中注册 Node.js 应用程序。有关更多信息,请阅读我们的 Node.js 安装指南文档。
For Application Manager to be enabled: https://documentation.cpanel.net/display/80Docs/Application+Manager
要启用应用程序管理器: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_envmodule. 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-nodejs10module if you want to register a Node.js? application.
您的托管服务提供商必须在 WHM 的功能管理器界面(WHM >> 主页 >> 包 >> 功能管理器)中启用应用程序管理器功能。
您的托管服务提供商必须安装以下 Apache 模块:
的
ea-ruby24-mod_passengermodule。注意:此模块禁用 Apache 的 mod_userdir 模块。该
ea-apache24-mod_env模块。注意:此模块允许您在注册应用程序时添加环境变量。有关环境变量的更多信息,请阅读下面的环境变量部分。的
ea-nodejs10,如果你想注册一个Node.js的模块?应用。
You can see how application manager looks like in this Youtube video: https://www.youtube.com/watch?v=ATxMYzLbRco
您可以在这个 Youtube 视频中看到应用程序管理器的样子:https: //www.youtube.com/watch?v=ATxMYzLbRco

