在 EC2 中托管 nodejs 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10578249/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:42:36  来源:igfitidea点击:

hosting nodejs application in EC2

node.jsamazon-ec2hosting

提问by Erik

I'm interested in hosting nodejs applications in a cloud and I'm looking for a free cloud hosting for my purpose. I've found that Amazon has one but I have the following question: Are there any tutorials around how I can set up and run nodejs application in Amazon EC2?

我对在云中托管 nodejs 应用程序很感兴趣,我正在为我的目的寻找免费的云托管。我发现 Amazon 有一个,但我有以下问题:是否有关于如何在 Amazon EC2 中设置和运行 nodejs 应用程序的教程?

EDIT: Can you provide any good hostings for nodejs (except heroku)?

编辑:你能为 nodejs (heroku 除外) 提供任何好的托管吗?

回答by nab

I've been using Node.js with Amazon EC2 for a while and was quite happy with both of them. For the moment AWS seems to be the cheapest and the most robust cloud provider, so picking up Amazon wouldn't be a mistake. There's nothing special about running Node.js in the cloud - you work with it like if it were your own PC. Below are some general steps to follow for the simplest Node.js application running on EC2 Ubuntu server:

我已经将 Node.js 与 Amazon EC2 一起使用了一段时间,并且对它们都非常满意。目前,AWS 似乎是最便宜、最强大的云提供商,所以选择亚马逊不会是一个错误。在云中运行 Node.js 并没有什么特别之处 - 您可以像使用自己的 PC 一样使用它。以下是在 EC2 Ubuntu 服务器上运行的最简单的 Node.js 应用程序要遵循的一些一般步骤:

  1. Create Amazon EC2 account.

  2. From AWS console start t1.microinstance with any Ubuntu AMI (example).

  3. Login via SSH to your instance.

  4. Install node.js: sudo apt-get install nodejs

  5. Create new file test_server.jswith the following content:

    require("http").createServer(function(request, response){
      response.writeHeader(200, {"Content-Type": "text/plain"});  
      response.write("Hello World!");  
      response.end();
    }).listen(8080);
    
  6. Start the server: node test_server.js

  7. Check it's working from another console: curl http://localhost:8080

  1. 创建Amazon EC2 帐户

  2. t1.micro带有任何 Ubuntu AMI 的AWS 控制台启动实例(示例)。

  3. 通过 SSH 登录到您的实例。

  4. 安装 node.js: sudo apt-get install nodejs

  5. test_server.js使用以下内容创建新文件:

    require("http").createServer(function(request, response){
      response.writeHeader(200, {"Content-Type": "text/plain"});  
      response.write("Hello World!");  
      response.end();
    }).listen(8080);
    
  6. 启动服务器: node test_server.js

  7. 从另一个控制台检查它是否正常工作: curl http://localhost:8080

回答by Noam Manos

Based on this tutorial, here's an updated step by step:

基于本教程,这里有一个逐步更新:

1) Make an account on Amazon Web Services.

1) 在Amazon Web Services上创建一个帐户。

2) Create an EC2 instance; I chose Ubuntu micro.

2)创建一个EC2实例;我选择了 Ubuntu micro。

3) Configure Security Group (name it "Node") and add ports:

3)配置安全组(将其命名为“节点”)并添加端口:

HTTP (80), HTTPS (443), and a custom TCP port for your Node app (e.g. 3000)

HTTP (80)、HTTPS (443) 和 Node 应用程序的自定义 TCP 端口(例如 3000)

4) Launch the instance and save the pem file (private key), e.g. "node.pem".

4) 启动实例并保存 pem 文件(私钥),例如“node.pem”。

5) On Windows - install Cygwin+ OpenSSH package. it is also recommended to install WinScpto have "explorer like" access to the linux.

5) 在 Windows 上 - 安装Cygwin+ OpenSSH 包。还建议安装WinScp以具有对 linux 的“类似资源管理器”的访问权限。

6) Open Cygwin Terminal as Administrator, and set correct permissions to "node.pem" file:

6) 以管理员身份打开 Cygwin 终端,并为“node.pem”文件设置正确的权限:

chown :Users node.pem
chmod 400 node.pem

7) Find your EC2 instance public DNS name in the EC2 dasboard, and connect to it with SSH:

7) 在 EC2 仪表板中找到您的 EC2 实例公共 DNS 名称,并使用 SSH 连接到它

ssh -i node.pem ubuntu@{your EC2 public DNS name}

8) Update Ubuntu and install NodeJS:

8) 更新 Ubuntu 并安装 NodeJS

sudo apt-get update
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo apt-get install -y build-essential

9) Copy your NodeJS application into the EC2 instance (via Cygwin, or Winscp).

9) 将您的 NodeJS 应用程序复制到 EC2 实例(通过 Cygwin 或 Winscp)。

10) Install all of your Node app required modules:

10) 安装所有 Node 应用程序所需的模块:

cd /home/ubuntu/My_Node_App
npm install --save

11) Re-route ports with IPtablesso that your app can be accessed on default http port 80:

11) 使用IPtables重新路由端口,以便您的应用程序可以在默认的 http 端口 80 上访问:

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 3000

To view the iptables routing entries, run:

要查看 iptables 路由条目,请运行:

sudo iptables -t nat -L

If you need to remove routing entry (first line), run:

如果您需要删除路由条目(第一行),请运行:

sudo iptables -t nat -D PREROUTING 1

12) Run your app as a background process:

12) 将您的应用程序作为后台进程运行:

sudo nohup node app.js &

To kill your app process:

要终止您的应用程序进程:

ps -ef | grep app.js
sudo kill {proccess id number}

回答by Daniel K.

My blog post on how to deploy Node-based apps on EC2: http://devblog.daniel.gs/2014/01/deploying-node-apps-on-aws-ec2-with.html

我关于如何在 EC2 上部署基于节点的应用程序的博客文章:http: //devblog.daniel.gs/2014/01/deploying-node-apps-on-aws-ec2-with.html

Explaining:

解释:

  • Deploying Node apps from your github repo (private+public)
  • Automating the deployment process using scripts
  • Reverse proxy using Nginx
  • and using Forever utility.
  • 从您的 github 存储库部署 Node 应用程序(私有+公共)
  • 使用脚本自动化部署过程
  • 使用 Nginx 进行反向代理
  • 并使用 Forever 实用程序。

Hope this helps.

希望这可以帮助。

回答by Steffen Opel

There are quite some hosting solutions for Node.jsavailable, here are a couple of these:

有很多可用的Node.js托管解决方案,这里有几个:

Joyent

乔伊恩

Joyentis the corporate sponsor and trademark owner of Node.js and provides an appealing alternative to Amazon EC2for many things, not the least Node.js hosting of course, see the Joyent's Node.js Development Environment(please check the Node.js? Development SmartMachine Terms of Servicethough).

Joyent是 Node.js 的企业赞助商和商标所有者,并在许多方面提供了Amazon EC2的有吸引力的替代方案,当然不仅仅是 Node.js 托管,请参阅Joyent 的 Node.js 开发环境(请查看Node.js?开发智能机器服务条款)。

Apparently they are just restructuring this development offering though:

显然,他们只是在重组这个开发产品:

For the past year, Joyent Cloud has provided a free development sandbox for users of Node.js. Over time, the community has made it clear that they want more tools and more capacity. To this end, we are excited to announce a partnership with Nodejitsuto provide both of these in a world-class Node.js development environment with Nodejitsu's development and management tools running on Joyent Cloud's Infrastructure-as-a-Service platform. The new service will launch very shortly.

在过去的一年中,Joyent Cloud 为 Node.js 用户提供了一个免费的开发沙箱。随着时间的推移,社区已经明确表示他们需要更多工具和更多容量。为此,我们很高兴地宣布与Nodejitsu建立合作伙伴关系,在世界一流的 Node.js 开发环境中提供这两者,Nodejitsu 的开发和管理工具在 Joyent Cloud 的基础设施即服务平台上运行。新服务将很快推出。

Accordingly, it is not entirely clear yet how the pricing options for a production hosting of a Node.js solution will end up, but given Joyent's competitive pricing, I'd expect a similar option at least.

因此,目前尚不完全清楚 Node.js 解决方案的生产托管的定价选项将如何结束,但鉴于 Joyent 的竞争性定价,我预计至少会有类似的选项。

Cloud Foundry

云铸造

The Cloud FoundryOpen Platform as a Service Projectsupport Node.js as well, amongst many other frameworks (which makes the platform so exciting), The platform is getting quite some traction recently and is meanwhile used by several solution Platform as a service (PaaS)providers as their backend accordingly - amongst these are (in no particular order and not necessarily complete):

Cloud Foundry的开放平台即服务项目支持Node.js的还有,以及许多其他框架(这使得平台太令人兴奋了),该平台是获得相当长的一段最近的牵引力,并同时使用几个解决方案平台即服务(PaaS )提供者相应地作为他们的后端 - 其中包括(没有特定的顺序,不一定完整):

  • AppFog- Simple PaaS for Java, Node, .Net, Ruby, PHP, MySQL, Mongo, PostgreSQL, and more... Freedom to move between IaaS at will with the easiest pricing in the cloud.
  • Cloud Foundry (VMware)(corporate sponsor of Cloud Foundry) - Deploy and scale applications in seconds, without locking yourself into a single cloud.
  • Iron Foundry- Iron Foundry is an open source project that extends Cloud Foundry? to the .NET ecosystem by providing services, installers, and developer tools.
  • AppFog-适用于 Java、Node、.Net、Ruby、PHP、MySQL、Mongo、PostgreSQL 等的简单 PaaS... 以最简单的云定价在 IaaS 之间自由移动。
  • Cloud Foundry (VMware)(Cloud Foundry 的公司赞助商)-在几秒钟内部署和扩展应用程序,而无需将自己锁定在单个云中。
  • Iron Foundry- Iron Foundry 是一个扩展 Cloud Foundry 的开源项目?.NET 生态系统通过提供服务、安装程序和开发人员工具。

Most of these are in beta still and the pricing isn't settled yet, but given the competition I'd expect quite some interesting options here over time.

其中大部分仍处于测试阶段,价格尚未确定,但考虑到竞争,我预计随着时间的推移会有一些有趣的选择。

回答by Yusuf X

The easiest way to run node.js for free on EC2 is IMHO on Heroku.

在 EC2 上免费运行 node.js 的最简单方法是在 Heroku 上恕我直言。

回答by Michael Blankenship

I just went through the Heroku sign-up and application tutorial. Could not have been easier. What a delightful experience...

我刚刚浏览了 Heroku 注册和应用教程。再容易不过了。多么愉快的经历...

...right up to the point where you can't have a MongoDB instance as a free gear. The minimum cost (other than a free trial month) is $18/month per GB of storage.

...直到您无法将 MongoDB 实例用作免费设备的地步。最低费用(免费试用月除外)为每 GB 存储空间 18 美元/月。

Honestly, the better choice then is Openshift. It's got three free gears which is enough for a lot of beginner stuff like what I'm doing. Both Heroku and Openshift are within Amazon's space but their customer interface is different. I thought Heroku's was easier for beginners to get started but as I mentioned, there's no free lunch on the database side of things.

老实说,更好的选择是 Openshift。它有三个自由齿轮,足以满足很多初学者的需求,比如我正在做的事情。Heroku 和 Openshift 都在亚马逊的领域内,但它们的客户界面不同。我认为 Heroku 对初学者来说更容易上手,但正如我所提到的,在数据​​库方面没有免费的午餐。

回答by techprd.com

check out this complete tutorial here. This tutorial shows how to install Node.js on EC2 and configure HTTP ports and nginx for port forwarding as well as using supervisor to run the Node.js forever as it normally stops on closing your SSH console session.

在此处查看此完整教程。本教程展示了如何在 EC2 上安装 Node.js 并配置 HTTP 端口和 nginx 以进行端口转发,以及如何使用主管永远运行 Node.js,因为它通常会在关闭 SSH 控制台会话时停止。