0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AWSのインスタンスにLaravel環境を構築する

Last updated at Posted at 2020-09-29

#インスタンスを作成
・EC2で行う(詳細は省略)
image.png

・インバウンドルールでHTTPポートを開放しておく
image.png

#必要なパッケージをインストール

Docker

$ sudo yum update -y
$ sudo yum install docker
$ sudo service docker start
$ sudo usermod -a -G docker ec2-user

Docker-compose

$ sudo curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose

Dockerイメージ(Laradock)

・Laravel環境構築に必要なパッケージが全部含められているLaradockを選択

$ git clone https://github.com/Laradock/laradock.git
$ cd laradock
$ cp env-example .env
# 実行に必要なサービスを実行する
$ docker-compose up -d nginx mysql phpmyadmin redis workspace
# 動いているのか確認
$ docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                                                                                                                            NAMES
ccb046bd18d9        laradock_nginx        "/docker-entrypoint.…"   6 minutes ago       Up 6 minutes        0.0.0.0:80-81->80-81/tcp, 0.0.0.0:443->443/tcp                                                                                   laradock_nginx_1
ebdc1a33503a        laradock_php-fpm      "docker-php-entrypoi…"   6 minutes ago       Up 6 minutes        9000/tcp                                                                                                                         laradock_php-fpm_1
0c3e361c4626        laradock_phpmyadmin   "/docker-entrypoint.…"   6 minutes ago       Up 6 minutes        0.0.0.0:8081->80/tcp                                                                                                             laradock_phpmyadmin_1
361e0a4fa898        laradock_workspace    "/sbin/my_init"          6 minutes ago       Up 6 minutes        0.0.0.0:3000-3001->3000-3001/tcp, 0.0.0.0:4200->4200/tcp, 0.0.0.0:8080->8080/tcp, 0.0.0.0:2222->22/tcp, 0.0.0.0:8001->8000/tcp   laradock_workspace_1
1a5246349a6d        laradock_mysql        "docker-entrypoint.s…"   6 minutes ago       Up 6 minutes        0.0.0.0:3306->3306/tcp, 33060/tcp                                                                                                laradock_mysql_1
09477490bbec        laradock_redis        "docker-entrypoint.s…"   6 minutes ago       Up 6 minutes        0.0.0.0:6379->6379/tcp                                                                                                           laradock_redis_1
53ded2d4fab3        docker:19.03-dind     "dockerd-entrypoint.…"   6 minutes ago       Up 6 minutes        2375-2376/tcp                                                                                                                    laradock_docker-in-docker_1

・ワークスペース内に個人プロジェクトを格納

# クーロン
$ git clone https://github.com/lineapple0601/laravel_vue_practice.git
$ exit

・.envファイルにあるアプリのルートパスを再設定

.env
APP_CODE_PATH_HOST=../laravel_vue_practice

・dockerを再起動して、個人プロジェクトで起動できるようにする

$ docker-compose stop
$ docker-compose up -d nginx mysql phpmyadmin redis workspace

・composerアップデートや他の設定を行う(docker-compose.ymlに入れるのがベストだが、一応手動で行う)

$ docker exec -it 46a935162a22 /bin/bash
$ composer update
$ php artisan key:generate

・ログ・キャッシュ系での権限設定

$ chmod 644 -R storage
$ chmod 644 -R bootstrap/cache
$ chown laradock:laradock -R storage
$ chown laradock:laradock -R bootstrap/cache

・AWS側のパブリックIPからアクセスしてみる
image.png
アクセス成功!

DBサーバーの構築

Laradock内にMysqlが含まれているので、docker-composeの起動で既に構築済み
mysqlコンテナ起動を確認

$ docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                                                                                                                            NAMES
63222479c01a        laradock_nginx        "/docker-entrypoint.…"   20 minutes ago      Up 20 minutes       0.0.0.0:80-81->80-81/tcp, 0.0.0.0:443->443/tcp                                                                                   laradock_nginx_1
a300f914e763        laradock_php-fpm      "docker-php-entrypoi…"   20 minutes ago      Up 20 minutes       9000/tcp                                                                                                                         laradock_php-fpm_1
46a935162a22        laradock_workspace    "/sbin/my_init"          20 minutes ago      Up 20 minutes       0.0.0.0:3000-3001->3000-3001/tcp, 0.0.0.0:4200->4200/tcp, 0.0.0.0:8080->8080/tcp, 0.0.0.0:2222->22/tcp, 0.0.0.0:8001->8000/tcp   laradock_workspace_1
b4f6d0dbb16c        docker:19.03-dind     "dockerd-entrypoint.…"   20 minutes ago      Up 20 minutes       2375-2376/tcp                                                                                                                    laradock_docker-in-docker_1
0c3e361c4626        laradock_phpmyadmin   "/docker-entrypoint.…"   2 hours ago         Up 20 minutes       0.0.0.0:8081->80/tcp                                                                                                             laradock_phpmyadmin_1
1a5246349a6d        laradock_mysql        "docker-entrypoint.s…"   2 hours ago         Up 20 minutes       0.0.0.0:3306->3306/tcp, 33060/tcp                                                                                                laradock_mysql_1
09477490bbec        laradock_redis        "docker-entrypoint.s…"   2 hours ago         Up 20 minutes       0.0.0.0:6379->6379/tcp                                                                                                           laradock_redis_1

下記の情報で外部DBツールからアクセスしてみる

docker-compose.yml
### MYSQL #################################################
MYSQL_VERSION=latest
MYSQL_DATABASE=default
MYSQL_USER=default
MYSQL_PASSWORD=secret
MYSQL_PORT=3306
MYSQL_ROOT_PASSWORD=root
MYSQL_ENTRYPOINT_INITDB=./mysql/docker-entrypoint-initdb.d

image.png
image.png

アクセス成功!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?