EC2サーバー構築手順

技術文書
2019/11/14

https://qiita.com/y-do/items/e127211b32296d65803a
https://github.com/openresty/docker-openresty

ログイン

[~]$ ssh -i ~/.ssh/moving_i_banner.pem ec2-user@ec2-3-112-221-137.ap-northeast-1.compute.amazonaws.com

タイムゾーン変更

$ date
2019年 11月 19日 火曜日 05:48:30 UTC
$ sudo vim /etc/sysconfig/clock
#ZONE="UTC" 
ZONE="Japan" 
UTC=true
$ sudo ln -sf /usr/share/zoneinfo/Japan /etc/localtime
$ sudo reboot
$ date
2019年 11月 19日 火曜日 14:53:12 JST

DockerとOpenRestyインストール

$ sudo yum install -y docker
$ sudo service docker start
$ sudo usermod -a -G docker ec2-user
$ sudo docker info
$ sudo curl -L https://github.com/docker/compose/releases/download/1.21.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
$ docker-compose --version
$ docker -v
$ sudo yum install git
$ git clone https://github.com/openresty/docker-openresty.git
$ cd docker-openresty/
$ vi alpine/Dockerfile
#    && ln -sf /dev/stdout /usr/local/openresty/nginx/logs/access.log \
#    && ln -sf /dev/stderr /usr/local/openresty/nginx/logs/error.log
    && apk --update add tzdata \
    && cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime \
    && apk del tzdata

$ vi nginx.vh.default.conf
location /click {
    content_by_lua_block {
        ngx.redirect(ngx.var.arg_lp)
    }
    }
$ sudo docker build -t openresty-test -f alpine/Dockerfile .
$ sudo docker run -d -p 80:80 openresty-test 
$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
ac8866f2073a        openresty-test      "/usr/local/openrest…"   16 minutes ago      Up 16 minutes       0.0.0.0:80->80/tcp   hardcore_perlman

$ sudo docker exec -it ac8866f2073a ash
$ sudo docker logs ac8866f2073a

nginxログ設定

[ec2-user@ip-172-31-45-87 docker-openresty]$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
01d301855435        openresty-test      "/usr/local/openrest…"   3 months ago        Up 3 months         0.0.0.0:80->80/tcp   distracted_poincare
[ec2-user@ip-172-31-45-87 docker-openresty]$ sudo docker exec -it 01d301855435 ash
/ # ls -l /usr/local/openresty/nginx/logs/
total 4
lrwxrwxrwx    1 root     root            11 Aug  1 18:00 access.log -> /dev/stdout
lrwxrwxrwx    1 root     root            11 Aug  1 18:00 error.log -> /dev/stderr
-rw-r--r--    1 root     root             2 Aug  1 18:08 nginx.pid
シンボリックリンク削除&再作成
/usr/local/openresty/nginx/logs # rm accesslog error.log
/usr/local/openresty/nginx/logs # touch accesslog error.log
/usr/local/openresty/nginx/logs # chmod 777 accesslog error.log 
/usr/local/openresty/nginx/logs # ls -l
total 4
-rwxrwxrwx    1 root     root             0 Nov 19 05:37 access.log
-rwxrwxrwx    1 root     root             0 Nov 19 05:38 error.log

ログをマウントしてDocker再起動
[ec2-user@ip-172-31-45-87 docker-openresty]$ sudo docker stop 01d301855435
[ec2-user@ip-172-31-45-87 docker-openresty]$ sudo docker run -d -p 80:80 -v /home/ec2-user/docker-openresty/logs:/usr/local/openresty/nginx/logs openresty-test
6c4d91f870a7966c38833bff32eb6f85ec8e6c54cd77c5f2537f80cae79c3f51
[ec2-user@ip-172-31-45-87 docker-openresty]$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
6c4d91f870a7        openresty-test      "/usr/local/openrest…"   5 seconds ago       Up 4 seconds        0.0.0.0:80->80/tcp   sharp_fermi
[ec2-user@ip-172-31-45-87 docker-openresty]$ ls -l logs/
合計 4
-rw-r--r-- 1 root root 0 11月 19 05:44 access.log
-rw-r--r-- 1 root root 0 11月 19 05:44 error.log
-rw-r--r-- 1 root root 2 11月 19 05:44 nginx.pid

ログローテート
[ec2-user@ip-172-31-45-87 ~]$ sudo vi /etc/logrotate.d/nginx
/home/ec2-user/docker-openresty/logs/*log {
    rotate 90
    missingok
    ifempty
    sharedscripts
    compress
    postrotate
        docker exec -it openresty-test systemctl reload nginx > /dev/null 2>/dev/null || true
    endscript
}
[ec2-user@ip-172-31-45-87 ~]$ sudo logrotate -fv /etc/logrotate.d/nginx
[ec2-user@ip-172-31-45-87 ~]$ ls -l /home/ec2-user/docker-openresty/logs/
合計 8
-rw-r--r-- 1 root root 287 11月 19 14:57 access.log.1.gz
-rw-r--r-- 1 root root  20 11月 19 14:44 error.log.1.gz

http://d1mc1u7pljcvbi.cloudfront.net/coslab_html5/index.html

ファイル