bikram-cloud / Kddi_work

some document
0 stars 0 forks source link

EC2(WebApp)の設定 #11

Open bikram-cloud opened 4 years ago

bikram-cloud commented 4 years ago
bikram-cloud commented 4 years ago

必要な設定:

  今回の設定:

商用リリースする前の設定: 選択したinstanceは十分かどうかを確認する Tenancyの設定を確認する 未確定

bikram-cloud commented 4 years ago

テストする為に下記のpublic subnetのEC2のinstanceを作成しました。

【ec2 1】

【ec2 2】

bikram-cloud commented 4 years ago

テストする為に下記のサーバーを作成した ===================

※sshログインすれば、「ubuntu」のユーザー名を使用してください。

bikram-cloud commented 4 years ago

After Install

bikram-cloud commented 4 years ago

nginx: reverse proxy

【server block】

server {
    listen 80;

    proxy_set_header Host $proxy_host;
    proxy_set_header    X-Real-IP    $remote_addr;
    proxy_set_header    X-Forwarded-Host       $host;
    proxy_set_header    X-Forwarded-Server    $host;
    proxy_set_header    X-Forwarded-For    $proxy_add_x_forwarded_for;

    location /api/ {
      proxy_pass         http://localhost:5120/;
    }
}
bikram-cloud commented 4 years ago

@ymdz

AWS EC2 Reverse Proxy Guide & Linux2 vs Ubuntu

【参照】

https://robsears.com/reverse-proxy-with-nginx-and-ec2/
https://www.quora.com/I-am-working-on-a-new-web-application-and-going-for-Amazon-EC2-to-host-it-What-Linux-server-should-I-go-for-Ubuntu-or-CentOS-Why
https://serverfault.com/questions/275736/amazon-linux-vs-ubuntu-for-amazon-ec2

上記のサイトと「StackoverFlow」のコメントのついて「Linux2」を使用するより「Ubuntu」のサーバーを使用した法がいいと言われています。

一つの理由はありませんですが、Linux2はrolling distroなので将来でアプリが急に止める可能性があり、Linux2のパッケージの管理はたまに使いにくいと言われます。

またはAWSのインフラから別のサービスに移動すれば、問題なく新しいサーバーで作られます。

bikram-cloud commented 4 years ago

Linux2サーバーでReverse Proxyが動いている!

【設定】

/etc/nginx/conf.d/app.conf


server {
listen 127.0.0.1:80;
proxy_set_header Host $proxy_host;
proxy_set_header    X-Real-IP    $remote_addr;
proxy_set_header    X-Forwarded-Host       $host;
proxy_set_header    X-Forwarded-Server    $host;
proxy_set_header    X-Forwarded-For    $proxy_add_x_forwarded_for;

location /api {
  proxy_pass         http://localhost:5120/;
}

}


> /etc/nginx/conf.d/nginx.conf
下記のLocationを追加した
    location /api/ {
            proxy_pass         http://localhost:5120/;
    }
bikram-cloud commented 4 years ago

/etc/nginx/conf.d/nginx.conf

location /api/ {
    proxy_set_header Host $proxy_host;
    proxy_set_header    X-Real-IP    $remote_addr;
    proxy_set_header    X-Forwarded-Host       $host;
    proxy_set_header    X-Forwarded-Server    $host;
    proxy_set_header    X-Forwarded-For    $proxy_add_x_forwarded_for;
    proxy_pass         http://localhost:5120/;
}

location *the other path* {
  404
} 
bikram-cloud commented 4 years ago
bikram-cloud commented 4 years ago

/etc/nginx/conf.d/nginx.conf

location /api/ {
    proxy_set_header Host $proxy_host;
    proxy_set_header    X-Real-IP    $remote_addr;
    proxy_set_header    X-Forwarded-Host       $host;
    proxy_set_header    X-Forwarded-Server    $host;
    proxy_set_header    X-Forwarded-For    $proxy_add_x_forwarded_for;
    proxy_pass         http://localhost:5120/;
}

location *the other path* {
  404
} 

上記の設定を確認しました。ヘーダ設定はどちらのファイルでもいいですが「nginx.conf」で書いたら「app.conf」のファイルで下記の設定も必要ようです。

server {
    listen 127.0.0.1:80;

    location /api {
      proxy_pass         http://localhost:5120/;
    }
}
bikram-cloud commented 4 years ago

apt update

sudo apt-get update
sudo apt-get upgrade

external command (eg. sar / mpstat / iostat etc )

// https://heartbeats.jp/hbblog/2013/10/10-commands.html
// http://garapon.hatenablog.com/entry/2016/10/03/Ubuntu_%E3%81%ABsar%EF%BC%88sysstat%EF%BC%89%E3%82%92%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB%E3%81%99%E3%82%8B%E3%80%82

apt-get install -y sysstat zip unzip
vi /etc/default/sysstat
-----------------------
ENABLED="false"
  ↓
ENABLED="true"
-----------------------

/etc/init.d/sysstat stop
/etc/init.d/sysstat start

timezone settings

// date UTC to JST
date
> Fri Feb 14 04:14:11 UTC 2020

sudo su -
timedatectl set-timezone Asia/Tokyo

date
> Fri Feb 14 13:17:01 JST 2020

NTP

sudo su -

// https://blog.fenrir-inc.com/jp/2017/12/amazontimesyncserviceonubuntu.html
vi /etc/systemd/timesyncd.conf 
----------------------------------
# NTP=
   ↓
NTP=169.254.169.123
----------------------------------

service systemd-timesyncd stop
service systemd-timesyncd start

systemctl status systemd-timesyncd.service
> 
... systemd[1]: Starting Network Time Synchronization...
... systemd[1]: Started Network Time Synchronization.
... systemd-timesyncd[27950]: Synchronized to time server 169.254.169.123:123 (169.254.169.123).

Node.JS / forever install

// https://tech-blog.s-yoshiki.com/2019/05/1201/
sudo apt install nodejs npm
sudo npm install n -g
sudo n stable
sudo apt purge -y nodejs npm
exec $SHELL -l
// versions check 
node -v
> v12.16.0

npm i forever -g
mkdir /var/log/forever
chown ubuntu:ubuntu /var/log/forever

eg. if you use the forever command, set this option
↓
forever start -l /var/log/forever/application.log index.js

Nodejs Log format like apache

see more information ↓
http://blog.shonanshachu.com/2012/10/nodejsexpressaccess-log-log-rotate.html

Nginx

// https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-18-04

// install 
sudo apt install nginx

// version check
nginx -V
> nginx version: nginx/1.14.0 (Ubuntu)

ufw

sudo su -
ufw status
> Status: inactive

ufw enable
> Command may disrupt existing ssh connections. Proceed with operation (y|n)? 
y

ufw allow 22/tcp
ufw allow 80/tcp

ufw status numbered
--------------------------------------------------------
Status: active

     To                         Action      From
     --                         ------      ----
[ 1] 22/tcp                     ALLOW IN    Anywhere                  
[ 2] 80/tcp                     ALLOW IN    Anywhere                  
[ 3] 22/tcp (v6)                ALLOW IN    Anywhere (v6)             
[ 4] 80/tcp (v6)                ALLOW IN    Anywhere (v6)  

logrotate(sysstat)

vm-machine status log(log file path)
/etc/cron.d/sysstat 

logrotate(web server log)

logrotate file path : 
/etc/logrotate.d/nginx

note: 
https://christina04.hatenablog.com/entry/2015/07/21/094631
https://qiita.com/kotarella1110/items/c9f46d9e677a8ed3a5a2

update the /etc/logrotate.d/nginx file

postrotate
    invoke-rc.d nginx rotate >/dev/null 2>&
      ↓
     /etc/init.d/nginx rotate >/dev/null 2>&1

logrotate (application log) vi /etc/logrotate.d/forever

/var/log/forever/*.log {
  daily
  dateext
  dateformat _%Y-%m-%d
  missingok
  rotate 30
  compress
  delaycompress
  notifempty
  copytruncate
} 

ll 
> -rw-r--r--  1 root root  148 Feb 14 15:18 forever
bikram-cloud commented 4 years ago

EC2 インスタンス

【インスタンスのタイプ】

bikram-cloud commented 4 years ago

Auto Scaling Group: Launch Template 設定

【参照】

https://docs.aws.amazon.com/autoscaling/ec2/userguide/create-launch-template.html

Launch template contents

Network Interfaces

bikram-cloud commented 4 years ago

Auto-Scaling Group

事前作業

各使用したいゾーンでASGで追加する事が必要です。

① サーバー1a


② ポリシー

【参照】

https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-scaling-simple-step.html

bikram-cloud commented 4 years ago

ASG監視

① Enable: Auto Scaling Metrics ② Enable: Notification


① Enable: Auto Scaling Metrics

EC2 → ASG → 対象のASGを選択する → Monitoringタブ

Monitoringタブで「Enable Group Metrics Collection」のボタンがある

② Enable: Notification

EC2 → ASG → 対象のASGを選択する → Notificationタブ

【Create notification】