2017年10月22日日曜日

[AWS]AWS導入メモ3(angular導入)

これも公式通りにangular-cliを使用

https://github.com/angular/angular-cli

■疎通確認
nginxから4200をfowerdingしてもInvalid Host headerになる。
ホスト名チェックでエラーになっているようなので一時的に以下をtrue固定にして確認。動作確認出来たらもとに戻す。

node_modules/webpack-dev-server/lib/Server.js
・checkHostのreturnをtrue固定

■Angularのビルド
ng build
※dist/が作成される

■expressをインストール
angularで作成したフォルダに移動して以下を実行
npm install --save express

expressコマンドが作成した/bin/wwwをコピーしてAngularを表示するようにする
以下を参考
https://qiita.com/frost_star/items/75290a6749fb7933b420

bin/www

// 使用するライブラリの読み込み
const express = require('express');
const path = require('path');
const http = require('http');

// api.js(サーバサイドの処理スクリプト)の読み込み
const api = require('./server/routes/api');

// expressの初期化
const app = express();

// 静的ファイルパスの設定
// ビルド後のAngularのパスを設定する
app.use(express.static(path.join(__dirname, 'dist')));

// /apiでアクセスしたときはapi.jsを表示
app.use('/api', api);

// 任意のURLでアクセスしたときはdist/index.html(Angularのindex.html)を表示
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'dist/index.html'));
});

/**
 * ポート設定
 */
const port = process.env.PORT || '3000';
app.set('port', port);

/**
 * HTTPサーバの作成
 */
const server = http.createServer(app);

/**
 * ポート監視を開始する
 */
server.listen(port, () => console.log(`API running on localhost:${port}`));

server/routes/api.js

const express = require('express');
const router = express.Router();

router.get('/', (req, res) => {
  res.header('Content-Type', 'text/plain;charset=utf-8');
  res.send('これはサーバサイドスクリプトだよ');
});

module.exports = router;

■実行
forever start ./meanapp/bin/www

[AWS]AWS導入メモ2(mongo-db導入)

公式サイト通り
https://docs.mongodb.com/ecosystem/platforms/amazon-ec2/


※現時点の手順
echo "[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc" |
  sudo tee -a /etc/yum.repos.d/mongodb-org-3.4.repo
sudo yum -y update && sudo yum install -y mongodb-org-server \
    mongodb-org-shell mongodb-org-tools

2017年10月9日月曜日

[AWS]AWS導入メモ

Androidと全く関係ないですが・・・

1.AWSアカウントの作成
※以下AWSマネージメントコンソール
2.リージョンを選択(東京)
3.VPC作成
  • パブリックサブネットの作成
  • インターネットゲートウェイの作成(VPCにアタッチする)
  • ルートテーブルの作成(0.0.0.0/0をインターネットゲートウェイに設定)
  • Elastic IP(固定IP)の設定 ※インスタンスに関連付けを忘れたりインスタンスを停止したりすると課金が発生する・・・
3.EC2インスタンス作成
  • Amazon Linux AMIを選択
  • t2.microを選択(無料枠)
  • ssh用サーバ証明書をダウンロード
※以下Terminal(rlogin)
4.nginxをインストール
  • sudo yum update
  • sudo yum install nginx
  • sudo service nginx start
  • sudo chkconfig nginx on
※再びAWSマネージメントコンソール
5.セキュリティグループでインバウンド80ポートの開放
6.VPCを選択してDNS名前解決の編集を実行

※node/express
7.nvmとnodeをインストール
 https://github.com/creationix/nvm/
8.expressをインストール・実行
  $ npm install express-generator -g
  $ express myapp
  $ cd myapp && npm install
9.foreverをインストール・実行/停止
  $ npm install forever -g
  $ forever start ./myapp/bin/www
  $ forever stop ./myapp/bin/www

※app.jsの説明
  https://qiita.com/mito_log/items/735f7079f99ec78ea7e6#routerget%E3%81%A8%E3%81%AF

10.nginxからexpressへフォワーディング
  $ sudo vi /etc/nginx/nginx.conf
  ※serverブロックに以下を追加
    upstream node-backend {
          server localhost:3000;
      }
     
      location / {
           if ( -f $request_filename ) {
                break;
           }

           if ( !-f $request_filename ) {
                proxy_pass http://node-backend;
                break;
           }     
      }