nodeJS / mongoose / mongodbアプリケーションをクラウドでホストしたいと思います。EC2には1年間無料のMicroInstanceがあるので、私の質問は次のとおりです。nodejs/を起動して実行するためのステップバイステップのチュートリアルはありますか? Amazon EC2のマングースアプリケーション?
3 に答える
Node.jsアプリケーションをAmazonEC2に配置するのに本当に役立つガイドをいくつか使用しました。
最初のものは、インスタンスの作成とそのインスタンスへのNode.jsのインストールをガイドします
AmazonEC2でNode.jsをセットアップする方法-完全ガイド
そして、もう1つ、役立つ可能性のあるものがあります。iptablesを調整してポート8080に転送することで、Node.jsアプリをポート80で利用できるようにする方法について詳しく説明しています。
MongoDBの場合、公式WebサイトのAmazonEC2でそれを設定するための公式ガイドがあります
アプリケーションのプロトタイプを作成するだけの場合は、 MongoLabの無料利用枠(500 MB)を使用して、MongoDBインスタンスを簡単に作成することも検討できます。これにより、Node.jsアプリケーションを実行するEC2マイクロインスタンスのリソースも節約できます。
Amazonの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{}configブロックで機能します
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-centos-or-fedora-linux/
私はステップバイステップで、私が従うEC2インスタンスでnode.jsアプリをセットアップする方法を書き留めました、これらのステップに従うことによって私のためにEC2でサーバーをセットアップするのにちょうど30分かかります
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ファイルタイプを作成する場合nanoserver.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サーバーが稼働し、素晴らしい1日を過ごします。
チップ
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
これが役に立った場合は、投票してください。ありがとう