* AWS EC2上で Expressアプリ起動 [#j284858f] #setlinebreak(on); [[Express(Nodejs)でWebAPI作成]] で作成した ExpressアプリをEC2上に配備してパフォーマンスを検証する。 EC2インスタンス、MySQL等は [[AWS EC2上で Spring Bootアプリ起動]] で使用したものと同じものを使用する。 &br; #contents ** ExpressアプリをEC2インスタンスにアップロード [#s1d52c76] #html(<div style="padding-left:10px">) [[Express(Nodejs)でWebAPI作成]] で作成したアプリをEC2にアップロードする。 ※実際の開発時はコード三兄弟等を使用してデプロイする事になると思うが、ここではSFTPで直接アップする。 *** アプリをzip圧縮 [#i3a68844] #html(<div style="padding-left:10px">) #code(myterm2 nolinenums){{ zip -r sample.zip sample }} #html(</div>) *** SFTPでアップロード [#f06fc8b2] #html(<div style="padding-left:10px">) #code(myterm2 nolinenums){{ sftp -i ~/Desktop/pem/キーペア.pem ec2-user@パブリックDNS sftp> put sample.zip sftp> exit }} #html(</div>) #html(</div>) ** EC2に Nodejs 及び npm をインストール [#c80d6b56] #html(<div style="padding-left:10px">) *** EC2インスタンスにSSH接続する [#l27bd83c] #html(<div style="padding-left:10px">) #code(myterm2 nolinenums){{ ssh -i ~/Desktop/pem/キーペア.pem ec2-user@パブリックDNS }} #html(</div>) *** AmazonLinuxのアップデート [#ddaa581e] #html(<div style="padding-left:10px">) #code(myterm2 nolinenums){{ sudo yum update }} #html(</div>) *** 必要なパッケージのインストール [#w4383125] #html(<div style="padding-left:10px">) #code(myterm2 nolinenums){{ sudo yum -y install gcc-c++ make openssl-devel sudo yum -y install git }} #html(</div>) *** Node.jsのインストール [#z458c384] *** Node.jsのインストール(8.9.0) [#o8de1ea5] #html(<div style="padding-left:10px">) #code(myterm2 nolinenums){{ cd ~/ git clone https://github.com/nodejs/node cd node wget https://nodejs.org/dist/v8.9.0/node-v8.9.0.tar.gz tar -xf node-v8.9.0.tar.gz cd node-v8.9.0 ./configure sudo make && make install make sudo make install }} #html(</div>) *** npmのインストール [#naa8f02f] *** Node.jsパッケージをインストール用に /etc/sudoers を編集する [#h7d2d477] #html(<div style="padding-left:10px">) secure_path に/usr/local/bin を追加する #code(){{ sudo su visudo Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin }} #html(</div>) *** expressインストール(グローバル) [#v8a72071] #html(<div style="padding-left:10px">) #code(myterm2 nolinenums){{ cd ~/ git clone https://github.com/isaacs/npm.git cd npm sudo make install sudo npm install -g express }} #html(</div>) *** expressコマンドを使用できるようにする(グローバル) [#m12048a3] #html(<div style="padding-left:10px">) #code(myterm2 nolinenums){{ sudo npm install -g express-generator }} #html(</div>) #html(</div>) ** Expressアプリの起動 [#c358e031] #html(<div style="padding-left:10px">) *** アップしておいたzipを解凍 [#f1a52d25] #html(<div style="padding-left:10px">) #code(myterm2 nolinenums){{ unzip sample.zip }} #html(</div>) *** DB接続用の環境変数を設定 [#q348a53c] #html(<div style="padding-left:10px">) ~/.bash_profile に追記 #code(mycode2){{ export NODE_DB_HOST=Aurora MySQLのエンドポイント export NODE_DB_PORT=3306 export NODE_DB_NAME=example_db export NODE_DB_USER=example_user export NODE_DB_PASS=example_pass }} 反映 #code(myterm2 nolinenums){{ source ~/.bash_profile }} #html(</div>) *** Expressアプリを起動 [#j039e21e] #html(<div style="padding-left:10px">) #code(myterm2 nolinenums){{ cd sample DEBUG=sample:* npm start }} #html(</div>) #html(</div>) ** EC2インスタンスのセキュリティグループを修正 [#c5945373] #html(<div style="padding-left:10px">) [[マネージメントコンソール>https://ap-northeast-1.console.aws.amazon.com/ec2/v2/home?region=ap-northeast-1#SecurityGroups:sort=groupId]]から、対象のEC2インスタンスのセキュリティグループでポート3000を許可するように変更。 ※もしくは新しいセキュリティグループを作成して、EC2インスタンスに追加で割り当てる。 ※あくまでも動作確認用(本番運用時は 443 ポート以外は開けない) #html(</div>) ** 動作確認 [#m880e561] #html(<div style="padding-left:10px">) #code(myterm2 nolinenums){{ curl -v http://ec2インスタンスのパブリックDNS:3000/book }} #html(</div>)