如何在共享主机中托管 Node.Js 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24777750/
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
How to host a Node.Js application in shared hosting
提问by somesh
How to host a Node.Js application in a shared hosting
如何在共享主机中托管 Node.Js 应用程序
I want to host a node.js application in shared hosting. Does anyone have any reference or documentation to refer to?
我想在共享主机中托管一个 node.js 应用程序。有没有人有任何参考或文档可以参考?
回答by niutech
You canrun node.js server on a typical shared hosting with Linux, Apache and PHP (LAMP). I have successfully installed it, even with NPM, Express and Grunt working fine. Follow the steps:
您可以在具有 Linux、Apache 和 PHP (LAMP) 的典型共享主机上运行 node.js 服务器。我已经成功安装了它,即使 NPM、Express 和 Grunt 工作正常。按照步骤:
1) Create a new PHP file on the server with the following code and run it:
1)使用以下代码在服务器上创建一个新的PHP文件并运行它:
<?php
//Download and extract the latest node
exec('curl http://nodejs.org/dist/latest/node-v0.10.33-linux-x86.tar.gz | tar xz');
//Rename the folder for simplicity
exec('mv node-v0.10.33-linux-x86 node');
2) The same way install your node app, e.g. jt-js-sample, using npm:
2) 使用 npm 以同样的方式安装您的节点应用程序,例如 jt-js-sample:
<?php
exec('node/bin/npm install jt-js-sample');
3) Run the node app from PHP:
3)从PHP运行节点应用程序:
<?php
//Choose JS file to run
$file = 'node_modules/jt-js-sample/index.js';
//Spawn node server in the background and return its pid
$pid = exec('PORT=49999 node/bin/node ' . $file . ' >/dev/null 2>&1 & echo $!');
//Wait for node to start up
usleep(500000);
//Connect to node server using cURL
$curl = curl_init('http://127.0.0.1:49999/');
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//Get the full response
$resp = curl_exec($curl);
if($resp === false) {
//If couldn't connect, try increasing usleep
echo 'Error: ' . curl_error($curl);
} else {
//Split response headers and body
list($head, $body) = explode("\r\n\r\n", $resp, 2);
$headarr = explode("\n", $head);
//Print headers
foreach($headarr as $headval) {
header($headval);
}
//Print body
echo $body;
}
//Close connection
curl_close($curl);
//Close node server
exec('kill ' . $pid);
Voila! Have a look at the demo of a node app on PHP shared hosting.
瞧!在 PHP 共享主机上查看节点应用程序的演示。
EDIT:I started a Node.php project on GitHub.
编辑:我在 GitHub 上启动了一个Node.php 项目。
回答by vinyll
Connect with SSH and follow these instructions to install Node on a shared hosting
使用 SSH 连接并按照这些说明在共享主机上安装 Node
In short you first install NVM, then you install the Node version of your choice with NVM.
简而言之,您首先安装 NVM,然后使用 NVM 安装您选择的 Node 版本。
wget -qO- https://cdn.rawgit.com/creationix/nvm/master/install.sh | bash
Your restart your shell (close and reopen your sessions). Then you
您重新启动您的外壳(关闭并重新打开您的会话)。那么你
nvm install stable
to install the latest stable version for example. You can install any version of your choice. Check node --versionfor the node version you are currently using and nvm listto see what you've installed.
例如,安装最新的稳定版本。您可以安装您选择的任何版本。检查node --version您当前使用的节点版本并nvm list查看您已安装的版本。
In bonus you can switch version very easily (nvm use <version>)
作为奖励,您可以非常轻松地切换版本 ( nvm use <version>)
There's no need of PHP or whichever tricky workaround if you have SSH.
如果您有 SSH,则不需要 PHP 或任何棘手的解决方法。
回答by iiboone.com
I installed Node.js on bluehost.com (a shared server) using:
我使用以下命令在 bluehost.com(共享服务器)上安装了 Node.js:
wget <path to download file>
tar -xf <gzip file>
mv <gzip_file_dir> node
This will download the tar file, extract to a directory and then rename that directory to the name 'node' to make it easier to use.
这将下载 tar 文件,解压缩到一个目录,然后将该目录重命名为名称“node”,以使其更易于使用。
then
然后
./node/bin/npm install jt-js-sample
Returns:
npm WARN engine [email protected]: wanted: {"node":"0.10.x"} (current: {"node":"0.12.4","npm":"2.10.1"})
[email protected] node_modules/jt-js-sample
└── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected])
I can now use the commands:
我现在可以使用以下命令:
# ~/node/bin/node -v
v0.12.4
# ~/node/bin/npm -v
2.10.1
For security reasons, I have renamed my node directory to something else.
出于安全原因,我已将我的节点目录重命名为其他目录。
回答by aap
A2 Hosting permits node.js on their shared hosting accounts. I can vouch that I've had a positive experience with them.
A2 Hosting 允许在他们的共享主机账户上使用 node.js。我可以保证我对他们有过积极的体验。
Here are instructions in their KnowledgeBase for installing node.js using Apache/LiteSpeed as a reverse proxy: https://www.a2hosting.com/kb/installable-applications/manual-installations/installing-node-js-on-managed-hosting-accounts. It takes about 30 minutes to set up the configuration, and it'll work with npm, Express, MySQL, etc.
以下是使用 Apache/LiteSpeed 作为反向代理安装 node.js 的知识库中的说明:https://www.a2hosting.com/kb/installable-applications/manual-installations/installing-node-js-on-managed-托管帐户。设置配置大约需要 30 分钟,它可以与 npm、Express、MySQL 等一起使用。
See a2hosting.com.
请参阅 a2hosting.com。
回答by Marek
You should look for a hosting company that provides such feature, but standard simple static+PHP+MySQL hosting won't let you use node.js.
您应该寻找提供此类功能的托管公司,但标准的简单静态+PHP+MySQL 托管不会让您使用 node.js。
You need either find a hosting designed for node.js or buy a Virtual Private Serverand install it yourself.
您需要找到专为 node.js 设计的主机或购买虚拟专用服务器并自行安装。

