developer tip

공유 호스팅에서 Node.js 애플리케이션을 호스팅하는 방법

optionbox 2020. 8. 16. 20:11
반응형

공유 호스팅에서 Node.js 애플리케이션을 호스팅하는 방법


공유 호스팅에서 Node.js 애플리케이션을 호스팅하는 방법

공유 호스팅에서 node.js 애플리케이션을 호스팅하고 싶습니다. 참조 할 참조 나 문서가있는 사람이 있습니까?


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');

2) 동일한 방법으로 npm을 사용하여 노드 앱 (예 : jt-js-sample)을 설치합니다.

<?php
exec('node/bin/npm install jt-js-sample');

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);

짜잔! PHP 공유 호스팅에서 노드 앱데모를 살펴보십시오 .

편집 : GitHub 에서 Node.php 프로젝트를 시작했습니다 .


SSH로 연결하고 다음 지침에 따라 공유 호스팅에 노드를 설치합니다.

간단히 말해 NVM을 먼저 설치 한 다음 NVM을 사용하여 선택한 노드 버전을 설치합니다.

wget -qO- https://cdn.rawgit.com/creationix/nvm/master/install.sh | bash

셸을 다시 시작합니다 (세션을 닫았다가 다시 엽니 다). 그럼 너

nvm install stable

예를 들어 안정적인 최신 버전을 설치합니다. 원하는 버전을 설치할 수 있습니다. 확인 node --version현재 사용중인 노드 버전 및 nvm list설치 한 것을 볼 수 있습니다.

보너스로 매우 쉽게 버전을 전환 할 수 있습니다 ( nvm use <version>).

SSH가있는 경우 PHP 또는 까다로운 해결 방법이 필요하지 않습니다.


다음을 사용하여 bluehost.com (공유 서버)에 Node.js를 설치했습니다.

wget <path to download file>
tar -xf <gzip file>
mv <gzip_file_dir> node

이렇게하면 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)

이제 다음 명령을 사용할 수 있습니다.

# ~/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.


A2 Hosting permits node.js on their shared hosting accounts. I can vouch that I've had a positive experience with them.

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.

See a2hosting.com.


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.

You need either find a hosting designed for node.js or buy a Virtual Private Server and install it yourself.

참고URL : https://stackoverflow.com/questions/24777750/how-to-host-a-node-js-application-in-shared-hosting

반응형