【AWS】LaravelアプリをEC2デプロイ④【CircleCI / CodeDeploy編】

# 0. はじめに
大阪のLaravel初学者サウナーこと、kazumakishimoto([@kazuma_dev](https://twitter.com/kazuma_dev))です！
`CircleCI / CodeDeploy`の自動デプロイ方法です。

## 0-1. 前回記事
- 【AWS】LaravelアプリをEC2デプロイ【まとめ編】

https://qiita.com/kazumakishimoto/items/e0e109fca443a4acd2a1

- 【AWS】LaravelアプリをEC2デプロイ①【CloudFormation / EC2 / RDS編】

https://qiita.com/kazumakishimoto/items/0859a5a365b226c1b43a

- 【AWS】LaravelアプリをEC2デプロイ②【Route53編】

https://qiita.com/kazumakishimoto/items/825f8c5f94d0160b2364

- 【AWS】LaravelアプリをEC2デプロイ③【ACM / ELB編】

https://qiita.com/kazumakishimoto/items/bdb07c6d4881384f7063

## 0-2. 全体の流れ
> [1.CircleCI](#1-circleci)
> [2.CodeDeploy](#2-codedeploy)
> [補足](#補足)
> [Reference](#reference)
> [次回記事](#次回記事)

## 0-3. 本記事の対象者
- `CircleCI / CodeDeploy`で自動デプロイしたい方

## 0-4. 事前準備
- AWSアカウント作成済み
- CircleCIアカウント作成済み
- リージョンはアジアパシフィック(東京)ap-northeast-1
- `grfl`や`hoge`はサンプル名なので適宜変更して下さい

https://aws.amazon.com/jp/register-flow/

## 0-5. 要件
- `CircleCI`で自動デプロイ
- `CodeDeploy`で自動デプロイ

## 0-6. 本番環境
|ツール|バージョン|
|:--|:--|
|OS|Amazon Linux 2|
|nginx|1.12|
|PHP|7.4.28|
|Laravel|6.20.44|
|MySQL|5.7.37|
|Composer|1.10.26|
|Node.js|13.14.0|

## 0-7. AWS構成図
![aws](https://user-images.githubusercontent.com/68370181/178103075-eec5508a-4d29-409c-84f4-3f687fa9cd5d.png)

# 1. CircleCI
## 1-1. 秘密鍵と公開鍵の作成
```terminal:ec2-user
$ ssh ec2-user@xx.xxx.xxx.xxx -i ~/.ssh/hoge.pem
$ ssh-keygen -m pem -C ""
Enter×2
```

## 1-2. 公開鍵をGitHubへ登録
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/2445a340-f270-b4a8-3a52-5f0ef7c106b2.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/a74c2fee-193d-508d-342d-782a79cc2a33.png)
- `id_rsa.pub`の内容を貼り付け
```terminal:ec2-user
$ cat ~/.ssh/id_rsa.pub
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/7ac29a27-0d8f-7268-9ab8-c8553418471b.png)

```terminal:ec2-user
$ ssh git@github.com
PTY allocation request failed on channel 0
Hi あなたのGitHubアカウント名/grfl! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.
```

## 1-3. 秘密鍵をCircleCIへ登録
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/fc3afd82-0430-112f-07f0-83efd89ca59b.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/5f1e9390-695f-caf6-8923-4d8c15737a10.png)
- `id_rsa`の内容を貼り付け
```terminal:ec2-user
$ cat ~/.ssh/id_rsa
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/93649d48-d0b0-0697-1dec-65e2d241b40a.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/fbad3c1e-9f3f-7fc1-0cc2-e1e3103ebef8.png)

## 1-4. 公開鍵認証
```terminal:ec2-user
$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
$ chmod 600 ~/.ssh/authorized_keys
```

## 1-5. 環境変数
- `HOST_NAME`にEC2のパブリックIP(EIP)追加
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/fc3afd82-0430-112f-07f0-83efd89ca59b.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/5c3cd673-9139-771e-3e9a-5de0d47953be.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/3512092a-e9f0-39c2-5976-e9c4ade91de2.png)

## 1-6. config.yml
```diff:grfl/.circleci/config.yml
version: 2.1

executors:
  laravel-circleci:
    docker:
      - image: circleci/php:7.4-node-browsers
      - image: circleci/mysql:5.7
    environment:
      - APP_DEBUG: true
      - APP_ENV: testing
      - APP_KEY: base64:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=
      - DB_CONNECTION: circle_testing
      - MYSQL_ALLOW_EMPTY_PASSWORD: true
    working_directory: ~/repo

commands:
  install-dockerize:
    steps:
      - run:
          name: Install dockerize
          command: wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && sudo tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
          environment:
            DOCKERIZE_VERSION: v0.6.1
  install-php-extensions:
    steps:
      - run:
          name: Install PHP Exetensions
          command: sudo docker-php-ext-install pdo_mysql
          working_directory: src
  restore-cache-composer:
    steps:
      - restore_cache:
          key: v1-dependencies-{{ checksum "src/composer.json" }}
  install-composer:
    steps:
      - run:
          name: Install Composer
          command: composer install -n --prefer-dist
          working_directory: src
  save-cache-composer:
    steps:
      - save_cache:
          key: v1-dependencies-{{ checksum "src/composer.json" }}
          paths:
            - vendor
  npm-ci:
    steps:
      - run:
          name: npm CI
          command: |
            if [ ! -d node_modules ]; then
              npm ci
            fi
          working_directory: src
  restore-cache-npm:
    steps:
      - restore_cache:
          key: npm-cache-{{ checksum "src/package-lock.json" }}
  npm-run-dev:
    steps:
      - run:
          name: Run npm
          command: npm run dev
          working_directory: src
  save-cache-npm:
    steps:
      - save_cache:
          key: npm-cache-{{ checksum "src/package-lock.json" }}
          paths:
            - node_modules
  migration-seeding:
    steps:
      - run:
          name: Migration & Seeding
          command: php artisan migrate --seed
          working_directory: src
  test-unittest:
    steps:
      - run:
          name: Run PHPUnit
          command: vendor/bin/phpunit
          working_directory: src

jobs:
  build:
    executor:
      name: laravel-circleci
    steps:
      - checkout
      - install-dockerize
      - install-php-extensions
      - restore-cache-composer
      - install-composer
      - save-cache-composer
      - restore-cache-npm
      - npm-ci
      - save-cache-npm
      - npm-run-dev
      - migration-seeding
      - test-unittest
  deploy:
    docker:
      - image: circleci/php:7.4-node-browsers
    steps:
      - checkout
+     - add_ssh_keys
      - run:
+         name: aws deploy
          command: |
+           ssh -o StrictHostKeyChecking=no -t ec2-user@${HOST_NAME} "cd grfl && \
+             git pull origin master && \
+             cd /var/www/grfl/src && \
+             composer install -n --no-dev --prefer-dist && \
+             npm ci && \
+             npm run prod && \
+             php artisan migrate --force && \
+             php artisan config:cache"

workflows:
  version: 2
  build_deploy:
    jobs:
      - build
      - deploy:
          requires:
            - build
          filters:
            branches:
              only:
                - master

```

## 1-7. 自動デプロイ(CircleCI)
- ローカルのmasterブランチに、リモートのmasterブランチの内容を反映しておく
- commit & push → deploy!
- 下記のようなエラーログがCircleCIで発生する場合、権限や所有権を確認してください

### 1-7-1. エラーログ
```
Warning: Permanently added '**************' (ECDSA) to the list of known hosts.
error: cannot open .git/FETCH_HEAD: Permission denied
```
```
error: unable to unlink old '.circleci/config.yml': Permission denied
```
```
npm WARN prepare removing existing node_modules/ before installation
[..................] / : WARN prepare removing existing node_modules/ before innpm ERR! code EACCES
npm ERR! syscall rmdir
npm ERR! path /var/www/****/src/node_modules/@ampproject
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, rmdir '/var/www/****/src/node_modules/@ampproject'
npm ERR!  [OperationalError: EACCES: permission denied, rmdir '/var/www/****/src/node_modules/@ampproject'] {
npm ERR!   cause: [Error: EACCES: permission denied, rmdir '/var/www/****/src/node_modules/@ampproject'] {
npm ERR!     errno: -13,
npm ERR!     code: 'EACCES',
npm ERR!     syscall: 'rmdir',
npm ERR!     path: '/var/www/****/src/node_modules/@ampproject'
npm ERR!   },
npm ERR!   isOperational: true,
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'rmdir',
npm ERR!   path: '/var/www/****/src/node_modules/@ampproject'
npm ERR! }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/****/.npm/_logs/2022-05-31T09_51_53_058Z-debug.log
```

### 1-7-2. 権限変更
```terminal:ec2-user
$ cd /var/www/grfl
$ sudo chmod 777 .git
$ sudo chown -R ec2-user:ec2-user .git/
$ sudo chown -R ec2-user:ec2-user .circleci
$ cd /var/www/grfl/src
$ sudo chown -R ec2-user:ec2-user node_modules
```


# 2. CodeDeploy
## 2-1. S3バケット作成
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/a68eb3bf-4844-0bdf-08b0-3c40dfee71f8.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/93d333cf-df02-e68d-b1e4-cf7ed9d4c0b9.png)

## 2-2. IAMロール作成(CodeDeploy用)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/b9c11969-8521-0dff-b9dc-8bf9c44d363a.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/80604a60-18d2-675d-8ad3-8467ab7de6cf.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/88093adf-c047-fd1a-2cf3-2afaca614cd1.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/a52a5ea2-85bb-7650-dbb4-feecb608037e.png)

## 2-3. CodeDeploy作成
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/2d8353ce-4c18-8b0a-0667-5dd57250c0c1.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/3e0b58fc-001f-9154-6658-0ee128584798.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/c405c305-02eb-0879-9415-5659bf0ea539.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/13f1e6a9-46e2-468e-26eb-db7f9fc185f2.png)

## 2-4. IAMロール作成(EC2用)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/b9c11969-8521-0dff-b9dc-8bf9c44d363a.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/03682e06-bb58-31d6-b0d1-e64dbf8d2e89.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/9a8eadee-d33e-3544-7077-46df4462ce88.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/86525fbd-bd0b-ab34-de92-5bf11b52740a.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/36b96625-03fb-ab69-9377-102b51e2cd30.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/9e11680a-909f-2ea9-a360-530332867a0d.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/eb6d9342-2d93-20c4-690c-07326a67fbb9.png)
```json:json
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:GetObjectVersion",
                "s3:ListBucket"
            ],
            "Resource": [
                "arn:aws:s3:::grfl-codedeploy-ap-northeast-1-xxxxxxxxxxxx",
                "arn:aws:s3:::grfl-codedeploy-ap-northeast-1-xxxxxxxxxxxx/*"
            ]
        }
    ]
}
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/caf451ab-7a96-0d07-6715-40ea8c05c88c.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/84399405-19ec-15d2-36df-442e2479b770.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/4f0a4219-13ed-e296-6918-c290bd4f509c.png)

## 2-5. CodeDeployAgentインストール
```terminal:ec2-user
$ ssh ec2-user@xx.xxx.xxx.xxx -i ~/.ssh/hoge.pem
$ sudo yum install ruby -y
$ wget https://aws-codedeploy-ap-northeast-1.s3.ap-northeast-1.amazonaws.com/latest/install
$ chmod +x ./install
$ sudo ./install auto
$ sudo service codedeploy-agent status
The AWS CodeDeploy agent is running as PID xxxxx

$ sudo service codedeploy-agent restart
10〜20秒待機
[ec2-user@ip-xxx-xxx-xxx-xxx ~]$
```

## 2-6. IAMユーザー作成&環境変数(CircleCI用)
### 2-6-1. IAMユーザー作成
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/3843f7a5-7419-53cb-6f7f-797d33150feb.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/1938c29f-3454-8d13-6ed3-e9a9f6616651.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/94630c7f-3a5b-a15d-b910-dc5bcf9c7eeb.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/aae99e52-e56a-14ef-df2c-0a33a4050931.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/0076801a-35d3-bdc4-e7e6-7be436eb4a02.png)

### 2-6-2. 環境変数
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/47e88874-c557-792e-91b1-adff9f38f103.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/182ea82e-bb1d-95d9-32f0-de3e91a60b8b.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/ff28accf-11d5-54be-0e6b-c0062fa74ee5.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/bca07a39-082d-f1e6-b0bd-5a684bd4e254.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/31a024c8-bc6e-9f9f-dbd5-d85484ea6b58.png)

### 2-6-3. ポリシー作成
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/bc9b84f6-5bfb-27bb-500a-833e468d9813.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/a18fbb09-a4e5-ff02-1382-cfdcf9e635ce.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/41e61061-8e1b-7905-9f53-29b0ba91dfdd.png)
```json:JSON
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "s3:PutObject",
            "Resource": "arn:aws:s3:::grfl-codedeploy-ap-northeast-1-xxxxxxxxxxxx/*"
        }
    ]
}
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/7eb96ff5-5754-9f3b-a349-9ba87bcbcef7.png)

## 2-7. appspec.yml
```yaml:grfl/appspec.yml
version: 0.0
os: linux
files:
  - source: /
    destination: /var/www/grfl/src

permissions:
  - object: /var/www/grfl/src
    owner: grfl
    group: grfl

hooks:
  AfterInstall:
    - location: ./scripts/after_install.sh
      timeout: 60
      runas: grfl
```

## 2-8. after_install.sh
```sh:grfl/scripts/after_install.sh
#!/bin/bash

set -eux

cd /var/www/grfl/src
php artisan migrate --force
php artisan config:cache
```

```terminal:ec2-user
$ cd /var/www/grfl/src
$ chmod +x scripts/after_install.sh
$ ls -l scripts
-rwxr-xr-x  1 xxxx  xxxx  67 May  4 18:22 after_install.sh
```

## 2-9. config.yml
```yaml:.circleci/config.yml
version: 2.1

executors:
  laravel-circleci:
    docker:
      - image: circleci/php:7.4-node-browsers
      - image: circleci/mysql:5.7
    environment:
      - APP_DEBUG: true
      - APP_ENV: testing
      - APP_KEY: base64:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=
      - DB_CONNECTION: circle_testing
      - MYSQL_ALLOW_EMPTY_PASSWORD: true
    working_directory: ~/repo

commands:
  install-dockerize:
    steps:
      - run:
          name: Install dockerize
          command: wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && sudo tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
          environment:
            DOCKERIZE_VERSION: v0.6.1
  install-php-extensions:
    steps:
      - run:
          name: Install PHP Exetensions
          command: sudo docker-php-ext-install pdo_mysql
          working_directory: src
  restore-cache-composer:
    steps:
      - restore_cache:
          key: v1-dependencies-{{ checksum "src/composer.json" }}
  install-composer:
    steps:
      - run:
          name: Install Composer
          command: composer install -n --prefer-dist
          working_directory: src
  save-cache-composer:
    steps:
      - save_cache:
          key: v1-dependencies-{{ checksum "src/composer.json" }}
          paths:
            - vendor
  npm-ci:
    steps:
      - run:
          name: npm CI
          command: |
            if [ ! -d node_modules ]; then
              npm ci
            fi
          working_directory: src
  restore-cache-npm:
    steps:
      - restore_cache:
          key: npm-cache-{{ checksum "src/package-lock.json" }}
  npm-run-dev:
    steps:
      - run:
          name: Run npm
          command: npm run dev
          working_directory: src
  save-cache-npm:
    steps:
      - save_cache:
          key: npm-cache-{{ checksum "src/package-lock.json" }}
          paths:
            - node_modules
  migration-seeding:
    steps:
      - run:
          name: Migration & Seeding
          command: php artisan migrate --seed
          working_directory: src
  test-unittest:
    steps:
      - run:
          name: Run PHPUnit
          command: vendor/bin/phpunit
          working_directory: src

jobs:
  build:
    executor:
      name: laravel-circleci
    steps:
      - checkout
      - install-dockerize
      - install-php-extensions
      - restore-cache-composer
      - install-composer
      - save-cache-composer
      - restore-cache-npm
      - npm-ci
      - save-cache-npm
      - npm-run-dev
      - migration-seeding
      - test-unittest

  deploy:
    docker:
      - image: circleci/php:7.4-node-browsers
    environment:
      AWS_DEFAULT_REGION: ap-northeast-1
    steps:
      - checkout
      - run: sudo composer self-update --1
      - restore_cache:
          key: composer-no-dev-v1-{{ checksum "src/composer.lock" }}
      - run:
          command: composer install -n --no-dev --prefer-dist
          working_directory: src
      - save_cache:
          key: composer-no-dev-v1-{{ checksum "src/composer.lock" }}
          paths:
            - vendor
      - restore_cache:
          key: npm-v1-{{ checksum "src/package-lock.json" }}
      - run:
          name: npm ci
          command: |
            if [ ! -d node_modules ]; then
              npm ci
            fi
          working_directory: src
      - save_cache:
          key: npm-v1-{{ checksum "src/package-lock.json" }}
          paths:
            - node_modules
      - run:
          command: npm run prod
          working_directory: src
      - run:
          name: archive artifacts
          command: zip -ryq grfl.zip .
          working_directory: src
      - aws-cli/install
      - run:
          name: upload artifacts to s3
          command: aws s3 cp grfl.zip s3://${AWS_S3_BUCKET_NAME}
          working_directory: src
      - run:
          name: deploy to prod
          command: |
            aws deploy create-deployment \
            --application-name grfl \
            --deployment-group-name grfl \
            --s3-location bucket=${AWS_S3_BUCKET_NAME},key=grfl.zip,bundleType=zip
          working_directory: src

orbs:
  aws-cli: circleci/aws-cli@1.0.0

workflows:
  version: 2
  build_deploy:
    jobs:
      - build
      - deploy:
          requires:
            - build
          filters:
            branches:
              only:
                - master

```

## 2-10. EC2デプロイ先ディレクトリ退避
- 既にCodeDeploy以外の方法でデプロイされているディレクトリにCodeDeployでデプロイすると失敗するケースがあるため
```terminal:ec2-user
$ ssh ec2-user@xx.xxx.xxx.xxx -i ~/.ssh/hoge.pem
$ cd /var/www
$ mv grfl grfl-ssh-deploy
$ mkdir grfl
$ cd /var/www/grfl
$ mkdir src
$ cd /var/www
$ cp grfl-ssh-deploy/src/.env grfl/src/.env
$ ls -l grfl/src
-rwxrwxr-x 1 ec2-user ec2-user 1913 Jun  1 08:33 .env
```

## 2-11. 自動デプロイ(CodeDeploy)
- ローカル開発ブランチでcommit & push
- GitHubでPR作成
- CircleCIの`build`ジョブが完了したら、PRをマージ

### 2-11-1. エラーログ
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/3998b25e-5f18-b290-1a80-fa8f0d4887d3.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/162ad65a-001f-506f-b6bc-b9a2d1b7f0a2.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/bc88de52-9022-36df-e198-6f116cad44ae.png)

### 2-11-12. ヘルスチェック確認
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/1a9532fd-c4d4-69b9-403f-b43baf5a4d92.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/d65b15e4-b6b9-629b-6f7f-7ff2f12c44dc.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/01f8cee9-86f0-42e0-5d1f-1aa2b60a7d3d.png)

### 2-11-3. S3バケット確認
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/f75d283a-2114-b00e-b613-f095b8fd4621.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/6434dad9-3ce7-3556-10c0-355cc1522754.png)


## 2-12. 動作確認
```terminal:ec2-user
$ cd /var/www/grfl/src
$ sudo chmod -R 777 storage
$ sudo chmod -R 777 bootstrap/cache
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/e60dd9ed-8bfe-728f-66b9-f67d2eb877b2.png)


# 補足
## 開発環境（FW/ツールのバージョンなど）
|ツール|バージョン|
|:--|:--|
|Vue.js|2.6.14|
|jQuery|3.4.1|
|PHP|7.4.1|
|Laravel|6.20.43|
|MySQL|5.7.36|
|Nginx|1.18.0|
|Composer|2.0.14|
|npm|6.14.6|
|Git|2.33.1|
|Docker|20.10.11|
|docker-compose|v2.2.1|
|PHPUnit|8.0|
|CircleCI|2.1|
|heroku|7.59.4|
|MacBook Air|M1,2020|
|macOS|Monterey 12.3|
|Homebrew|3.3.8|

## ディレクトリ構造
```
【ルートディレクトリ】
├─ .circleci
│   └─ config.yml
├─ aws / CloudFormation
│   └─ ec2.yml
├─ docker
│   └─ mysql
│   └─ nginx
│   └─ php
│   └─ phpmyadmin
├─ src
│   └─ 【Laravelのパッケージ】
└─ docker-compose.yml
```

# Reference
https://www.techpit.jp/courses/11

https://www.techpit.jp/courses/78

https://qiita.com/stepiko135/items/6ca3b27325fc022719f2

https://qiita.com/hardreggaecafe/items/36ae34c81c79bd65b9b0

https://qiita.com/mejileben/items/3c9dcb67115dbb29fcf0

https://qiita.com/Kiyo_Karl2/items/f799c0ad7d85b7d60f01

https://qiita.com/nasuB7373/items/081f5974e31419a1a844

# 次回記事
- 【AWS】LaravelアプリをEC2デプロイ⑤【SNS / Chatbot編】

https://qiita.com/kazumakishimoto/items/b38c34a1275c4e9cd373

- 【AWS】LaravelアプリをEC2デプロイ⑥【S3編】

https://qiita.com/kazumakishimoto/items/d08894711bf59a4e21df

- 【AWS】LaravelアプリをEC2デプロイ⑦【API編】

https://qiita.com/kazumakishimoto/items/fd4afc02f9490ddb89e5

- 【AWS】お役立ちリンク集【随時更新】

https://qiita.com/kazumakishimoto/items/02e53cdf7b9887252040