som*_*esh 103 web-hosting node.js
如何在共享主机中托管Node.Js应用程序
我想在共享主机中托管node.js应用程序.有没有人有任何参考或文件可供参考?
niu*_*ech 149
您可以在Linux,Apache和PHP(LAMP)的典型共享主机上运行node.js服务器.我已成功安装它,即使使用NPM,Express和Grunt也能正常工作.按照步骤:
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');
Run Code Online (Sandbox Code Playgroud)
2)以同样的方式使用npm安装你的节点应用程序,例如jt-js-sample:
<?php
exec('node/bin/npm install jt-js-sample');
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
瞧!看看PHP共享主机上的节点应用程序的演示.
编辑:我在GitHub上启动了一个Node.php项目.
vin*_*yll 47
使用SSH连接并按照这些说明在共享主机上安装Node
简而言之,您首先安装NVM,然后使用NVM安装您选择的Node版本.
wget -qO- https://cdn.rawgit.com/creationix/nvm/master/install.sh | bash
Run Code Online (Sandbox Code Playgroud)
你重新启动你的shell(关闭并重新打开你的会话).那么你
nvm install stable
Run Code Online (Sandbox Code Playgroud)
例如,安装最新的稳定版本.您可以安装任何您选择的版本.检查node --version
您当前使用的节点版本并nvm list
查看已安装的版本.
奖金你可以很容易地切换版本(nvm use <version>
)
如果你有SSH,不需要PHP或任何棘手的解决方法.
小智 12
我使用以下命令在bluehost.com(共享服务器)上安装了Node.js.
wget <path to download file>
tar -xf <gzip file>
mv <gzip_file_dir> node
Run Code Online (Sandbox Code Playgroud)
这将下载tar文件,解压缩到一个目录,然后将该目录重命名为名称"node"以使其更易于使用.
然后
./node/bin/npm install jt-js-sample
Returns:
npm WARN engine jt-js-sample@0.2.4: wanted: {"node":"0.10.x"} (current: {"node":"0.12.4","npm":"2.10.1"})
jt-js-sample@0.2.4 node_modules/jt-js-sample
??? express@4.12.4 (merge-descriptors@1.0.0, utils-merge@1.0.0, cookie-signature@1.0.6, methods@1.1.1, cookie@0.1.2, fresh@0.2.4, escape-html@1.0.1, range-parser@1.0.2, finalhandler@0.3.6, content-type@1.0.1, vary@1.0.0, parseurl@1.3.0, serve-static@1.9.3, content-disposition@0.5.0, path-to-regexp@0.1.3, depd@1.0.1, qs@2.4.2, on-finished@2.2.1, debug@2.2.0, etag@1.6.0, proxy-addr@1.0.8, send@0.12.3, type-is@1.6.2, accepts@1.2.7)
Run Code Online (Sandbox Code Playgroud)
我现在可以使用命令:
# ~/node/bin/node -v
v0.12.4
# ~/node/bin/npm -v
2.10.1
Run Code Online (Sandbox Code Playgroud)
出于安全原因,我已将节点目录重命名为其他目录.