简体   繁体   English

检查共享托管是否允许Node.js

[英]Check does shared hosting allow nodejs

I started learning nodeJs I done few things on localhost. 我开始学习nodeJs,在localhost上做了几件事。 I was looking up how to set it up on shared hosting server. 我一直在寻找如何在共享托管服务器上进行设置。 I found that many host servers are made for only php and they do not allow node? 我发现许多主机服务器仅用于php,并且它们不允许节点运行? Or I did not understand it well? 还是我不太了解? Now my question is how can I check does my hosting services allows installing nodeJs? 现在,我的问题是如何检查托管服务是否允许安装nodeJs?

Honestly man, if you are trying to do any development that falls out of the scope of a very basic web application, I would move away from shared hosting. 老实说,如果您尝试进行超出基本Web应用程序范围之外的任何开发,那么我将放弃共享托管。 Now, I am making an assumption that when you say "shared hosting", you mean something like godaddy's basic web hosting packages etc, which would more than likely not allow Node since you would need to be able to log into server and install and configure node, etc, which they do not allow since they keep you very "encapsulated" using a simple GUI / Cpanel type of environment. 现在,我假设,当您说“共享托管”时,您的意思是像Godaddy的基本Web托管程序包之类的东西,这很可能不允许Node,因为您需要能够登录服务器并安装和配置节点等,它们是不允许的,因为它们使用简单的GUI / Cpanel类型的环境使您非常“封装”。

So the best idea would be to honestly simply ask your shared hosting provider if they allow you ssh into the server and install / configure software, or instead why not spin up a small little server and use node on there using one of the major server providers such as AWS , Linode or Digital Ocean . 因此,最好的主意是诚实地询问您的共享托管服务提供商是否允许您SSH进入服务器并安装/配置软件,或者为什么不启动小型服务器并使用主要服务器提供商之一在该节点上使用节点例如AWSLinodeDigital Ocean While this is still technically shared hosting, with those providers, you are essential given "full control" of your instance so you can install and configure whatever software you like. 尽管从技术上讲,这仍然是与这些提供程序共享的主机,但必须对实例具有“完全控制”权,因此您可以安装和配置所需的任何软件。

There are tons of tutorials which can walk you through setting up a small linux instance and installing node on these providers: 有大量的教程可以指导您设置小型Linux实例并在这些提供程序上安装节点:

https://aws.amazon.com/sdk-for-node-js/ https://aws.amazon.com/sdk-for-node-js/

https://www.linode.com/docs/websites/nodejs/a-nodejs-installation-crash-course https://www.linode.com/docs/websites/nodejs/a-nodejs-installation-crash-course

https://www.digitalocean.com/community/tags/node-js?type=tutorials https://www.digitalocean.com/community/tags/node-js?type=tutorials

Best of luck! 祝你好运!

Actually you can do this. 实际上,您可以执行此操作。 I have successfully been able to set it up a couple different ways. 我已经成功地通过几种不同的方式进行了设置。 I think the second is probably what you want : 我认为第二个可能是您想要的

1. cgi-node http://www.cgi-node.org/home 1. cgi-node http://www.cgi-node.org/home

Basically this replaces PHP on the lamp stack. 基本上,这取代了灯架上的PHP。 You can run javascript through node like you would run PHP. 您可以像运行PHP一样通过节点运行javascript。 This has all the same functionality of node js but is only really geared towards template rendering. 它具有节点js的所有相同功能,但实际上仅适用于模板渲染。

    <html>
    <body>
     <?
       var helloWorld = 'Hello World!'; 
       write(helloWorld + '<br/>'); 
     ?>
     <?= helloWorld ?>
    <br/>
    <b>I can count to 10: </b>

    <?
      for (var index= 0; index <= 10; index++) write(index + ' ');  
    ?>
      <br/>
      <b>Or even this: </b>
    <?  
      for (var index= 0; index <= 10; index++) { 
    ?>
        <?= index ?> 
    <? } ?>

    </body>
</html>

OR 要么

2. Standalone Server (this works with NameCheap hosting and GoDaddy shared hosting) 2.独立服务器 (与NameCheap托管和GoDaddy共享托管一起使用)

In your shared hosting account you will need SSH in order to do this. 在共享主机帐户中,您将需要SSH才能执行此操作。 So you may need to upgrade or request SSH access from their customer support. 因此,您可能需要升级或请求其客户支持提供SSH访问。 Download the latest NodeJS https://nodejs.org/en/download/ . 下载最新的NodeJS https://nodejs.org/en/download/ The shared hosting is probably in linux 64 bit. 共享主机可能在Linux 64位中。 You can check this on linux or unix by running : 您可以通过运行以下命令在linux或unix上进行检查:

uname -a

Download the Linux binaries and put the bin/node (and the bin/npm file if you want to use npm on the server) file from the download in /home/username/bin/ (create the bin folder if it doesn't exist) on the server. 下载Linux二进制文件,然后从下载的/ home / username / bin /中放入bin / node(如果要在服务器上使用npm,则放入bin / npm文件)文件(如果不存在则创建bin文件夹) )。 Put permissions 755 on the node binary. 在节点二进制文件上放置权限755。 So you should have a new file here : 因此,您应该在这里有一个新文件:

/home/username/bin/node

Open up the .htaccess file in /home/username/public_html and add the following lines : 在/ home / username / public_html中打开.htaccess文件,并添加以下几行:

RewriteEngine on
RewriteRule  (.*)  http://localhost:3000/$1  [P,L] 

Create a file in /home/username/public_html and just call it app.js. 在/ home / username / public_html中创建一个文件,然后将其命名为app.js。 Add the following lines in that file : 在该文件中添加以下行:

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('NodeJS server running on Shared Hosting\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

SSH into the server run these commands : SSH进入服务器,运行以下命令:

cd /home/username/public_html
which node # this should return ~/bin/node
node app.js & # This will create a background process with the server running

If you can get this set up right this will save you a ton of money in the long run as opposed to using something like AWS or Heroku etc. 如果您能正确进行此设置,则从长远来看,与使用AWS或Heroku等相比,这将为您节省大量资金。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM