16

我想在云中托管一个 nodeJS/mongoose/mongodb 应用程序,并且由于 EC2 有一个免费 1 年的 MicroInstance,我的问题是:是否有任何分步教程如何启动和运行 nodejs/ Amazon EC2 中的猫鼬应用程序?

4

3 回答 3

12

我使用了一些指南,它们真正帮助我在 Amazon EC2 上放置 Node.js 应用程序。

第一个指导您完成实例的创建和在该实例上安装 Node.js

如何在 Amazon EC2 上设置 Node.js - 完整指南

然后还有一个对你也有帮助的,它有一些关于如何通过调整 iptables 将其转发到端口 8080 来使 Node.js 应用程序在端口 80 上可用的更多细节

我如何让 Node.js 在 Amazon EC2 上运行

对于 MongoDB,官方网站上有一个在 Amazon EC2 上设置的官方指南

Amazon EC2 上的 MongoDB

如果您只是对应用程序进行原型设计,您还可以考虑使用MongoLab上的免费套餐 (500 MB) 轻松创建 MongoDB 实例,这也将节省运行 Node.js 应用程序的 EC2 微型实例的一些资源。

于 2012-11-09T11:21:05.023 回答
1

我推荐使用亚马逊的 AMI

为您的 js 应用程序创建一个新贵脚本

以下文件可以进入 /etc/init

nodeapp.conf

description "node app"

start on runlevel [23]
stop on runlevel [016]

console owner

exec /bin/bash -l -c 'cd /path/to/app; /usr/bin/node server.js -p 9001'

respawn

从这里您想使用 nginx 或 apache 代理到您的节点应用程序

nginx可以通过安装yum install nginx

对于 nginx,以下块将在您的 http {} 配置块中工作

upstream app_cluster_1 {
server 127.0.0.1:9001;
}
server {
listen 80;
server_name domain.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://app_cluster_1/;
proxy_redirect off;
}
}

然后你可以启动你的应用程序和 nginx

start nodeapp

service nginx start/restart

如果您在本地托管 mongo-db,请确保也启动它。我相信它附带了一个 init.d 脚本

service mongod start

自动运行 nginx 和 mongo

chkconfig nginx on

chkconfig mongod on

如果 mongo 不作为包提供,请按照以下步骤操作

http://docs.mongodb.org/manual/tutorial/install-mongodb-on-redhat-c​​entos-or-fedora-linux/

于 2012-11-08T17:36:40.620 回答
1

我已经一步一步写下了如何在我遵循的 EC2 实例中设置 node.js 应用程序,按照这些步骤在 EC2 中设置服务器只需要半小时

Step1: Create EC2 Instance with ubuntu, eligible for a free tier

Step2: click on connect button after successfully creating the instance.

step3: then follow the steps- In your terminal
       chmod 400 yourpemfile.pem
       after this copy, the line shown in the example and paste it into your terminal.

现在您已成功登录到您的 EC2 实例。

Step4: install all dependencies listed below.
       Nodejs
       npm
       mongodb
       git - this will be already installed in your instance, try typing git.
       pm2

现在我们已经安装了 node.js 应用程序运行所需的所有依赖项

step5: clone your project
       Go to your latest branch

       Install dependencies (npm install) or simply type yarn

现在,如果您将 .env 文件的副本保留为 env.example 或与之相关的内容,则键入此内容以复制您的文件 cp .env.example .env 以复制并创建 .env 文件

here I am copying .env.example file and creating a new file named .env

Step6: Now go on running instance scroll down and find security group.
       Here you will find launch wizard, click on it.
       Now click on inbound rules, and then click on edit button.
       Now click on add rule.
       select custom tcp.
       enter the port number which your node.js app is using.
       Select range anywhere, so that you can access from anywhere you want to.

现在来到您已连接到 EC2 实例的终端。

Step7: Build your project

Step8: create server.sh file out from your project which you cloned.
       Here type cd path-from-where-you-would-do-yarn-start/node index.js

创建 server.sh 文件类型 nano server.sh 这将打开文本编辑器。开始输入,如步骤 8 所示要退出此按 ctrl+o 然后按 enter 然后按 ctrl+x 现在您已成功创建 server.sh 文件

step9: Now where your server.sh file is located, here type
       pm2 start server.sh --name=name-you-like

现在您的 Node.js 服务器已启动并运行,祝您有美好的一天。

提示

Tips: 
Form next time.

  To see your list of a servers running type pm2 list.

  To stop your server type pm2 stop name-of-server-you-want-to-stop.

  To start pm2 start name-of-server-you-want-to-start.

  To restart pm2 restart name-of-server-you-want-to-restart

如果这有帮助,请投票。谢谢

于 2018-08-05T13:32:52.360 回答