LoginSignup
33
25

More than 3 years have passed since last update.

MacでLaravel5.6の開発環境をLaradockで構築してHerokuにデプロイするまで

Last updated at Posted at 2018-07-15

概要

  • Laravel5.6の開発環境をLaradockで構築
  • herokuにデプロイするようにする

手元の環境

>> sw_vers
ProductName:    Mac OS X
ProductVersion: 10.14
BuildVersion:   18A326h

構築後のVerなど

docker

> docker --version
Docker version 18.03.1-ce, build 9ee9f40
> docker-compose --version
docker-compose version 1.21.1, build 5a3f1a3
> docker-machine -version
docker-machine version 0.14.0, build 89b8332

docker: MySQL

> docker-compose exec mysql mysql --version
mysql  Ver 14.14 Distrib 5.7.22, for Linux (x86_64) using  EditLine wrapper

docker: Laravel

> docker-compose exec workspace php artisan --version 
Laravel Framework 5.6.27

heroku cli

> heroku --version
heroku-cli/6.99.0-ec9edad (darwin-x64) node-v9.11.1

heroku PostgreSQL

> heroku run "psql --version"
Running psql --version on ⬢ xxxxx... up, run.3720 (Free)
psql (PostgreSQL) 10.4 (Ubuntu 10.4-2.pgdg16.04+1)

手順

以下フォルダは任意で設定

  • ~/dev: それぞれの開発用フォルダ
  • project: それぞれのプロジェクトフォルダ

プロジェクト用のフォルダ作成

> cd ~/dev
> mkfir project
> cd project
> git init

Laradockのインストール

> git submodule add https://github.com/Laradock/laradock.git
> cd laradock
> cp env-example .env
# プロジェクトフォルダ直下にLaravelがインストールできないのでPATHを編集
> vi .env
- APP_CODE_PATH_HOST=../
+ APP_CODE_PATH_HOST=../src 

ワークスペースを起動

> docker-compose up -d workspace
> docker-compose ps
        Name              Command      State          Ports
-------------------------------------------------------------------
laradock_workspace_1   /sbin/my_init   Up      0.0.0.0:2222->22/tcp

ワークスペースに入ってLaravelのインストール

> docker-compose exec workspace bash
# composer create-project laravel/laravel . --prefer-dist

srcフォルダ以下にLaravelのファイルが展開される。

# ll
total 192
drwxr-xr-x 24 root     root        768 Jul 14 04:31 ./
drwxr-xr-x  1 root     root       4096 Jul 14 04:16 ../
drwxr-xr-x  7 root     root        224 Jul 14 04:30 app/
-rw-r--r--  1 root     root       1686 Jul 14 04:30 artisan
drwxr-xr-x  4 root     root        128 Jul 14 04:30 bootstrap/
-rw-r--r--  1 root     root       1477 Jul 14 04:30 composer.json
-rw-r--r--  1 root     root     144157 Jul 14 04:31 composer.lock
drwxr-xr-x 15 root     root        480 Jul 14 04:30 config/
drwxr-xr-x  6 root     root        192 Jul 14 04:30 database/
-rw-r--r--  1 laradock laradock    702 Jul 14 04:31 .env
-rw-r--r--  1 root     root        651 Jul 14 04:30 .env.example
-rw-r--r--  1 root     root        111 Jul 14 04:30 .gitattributes
-rw-r--r--  1 root     root        155 Jul 14 04:30 .gitignore
-rw-r--r--  1 root     root       1022 Jul 14 04:30 package.json
-rw-r--r--  1 root     root       1134 Jul 14 04:30 phpunit.xml
drwxr-xr-x  9 root     root        288 Jul 14 04:30 public/
-rw-r--r--  1 root     root       3675 Jul 14 04:30 readme.md
drwxr-xr-x  5 root     root        160 Jul 14 04:30 resources/
drwxr-xr-x  6 root     root        192 Jul 14 04:30 routes/
-rw-r--r--  1 root     root        563 Jul 14 04:30 server.php
drwxr-xr-x  5 root     root        160 Jul 14 04:30 storage/
drwxr-xr-x  6 root     root        192 Jul 14 04:30 tests/
drwxr-xr-x 38 root     root       1216 Jul 14 04:32 vendor/
-rw-r--r--  1 root     root        549 Jul 14 04:30 webpack.mix.js

ホスト側でnginxを起動する

> docker-compose up -d nginx
> docker-compose ps
           Name                          Command              State                    Ports
--------------------------------------------------------------------------------------------------------------
laradock_docker-in-docker_1   dockerd-entrypoint.sh           Up      2375/tcp
laradock_nginx_1              nginx                           Up      0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
laradock_php-fpm_1            docker-php-entrypoint php-fpm   Up      9000/tcp
laradock_workspace_1          /sbin/my_init                   Up      0.0.0.0:2222->22/tcp

これで、ホスト端末でhttp://localhostにアクセスするとwelcome画面が表示される。
やっほーい^^


MySQLのインストール

MySQLの最新バージョンをインストールすると
設定をゴニョゴニョしないとだめなので、今回は5.7を利用する。(重要

> vi .env
- MYSQL_VERSION=latest
+ MYSQL_VERSION=5.7
> docker-compose up -d mysql

docker経由でmysqlへログインできることを確認する。

> docker-compose exec mysql mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

ホスト端末からもアクセスできることを確認する。
-h 127.0.0.1を指定しないと接続できないかもしれない。

> mysql -uroot -proot default -h 127.0.0.1
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Laravelのマイグレーション

laradockの.envを参考にしてlaravelの.envの設定を変更する。(1敗)

> vi ../src/.env
- DB_HOST=127.0.0.1
- DB_DATABASE=homestead
- DB_USERNAME=homestead
+ DB_HOST=mysql
+ DB_DATABASE=default
+ DB_USERNAME=default
> docker-compose exec workspace bash
# php artisan migrate:install
Migration table created successfully.

ここまでで開発環境の構築はおわり。
開発用のgit リポジトリがあればremote addしてcommit&pushしておこう。


herokuの利用開始

herokuでユーザ登録はこちらから。


heroku toolbeltのインストールとログイン

> brew install heroku/brew/heroku
> heroku auth:login
Enter your Heroku credentials:
Email: [ユーザ登録したときのメールアドレス]
Password: [ユーザ登録したときのパスワード]
Logged in as [ユーザ登録したときのメールアドレス]

herokuでアプリケーション作成と設定

アプリケーション名は他のユーザと被らないように。

> cd ~/dev/project
> heroku create [アプリケーション名] --buildpack heroku/php
Creating ⬢ [アプリケーション名]... done
https://[アプリケーション名].herokuapp.com/ | https://git.heroku.com/[アプリケーション名].git
> echo web: vendor/bin/heroku-php-apache2 public/ > Procfile
> git add .
> git commit -m 'add Procfile'

git のリモートリポジトリに追加

> git remote add heroku https://git.heroku.com/[アプリケーション名].git

herokuの設定にAPP_KEYを追加

docker-composeとherokuコマンドは実行ディレクトリが異なるので注意(1敗)

> cd ~/dev/project/laradock
> docker-compose exec workspace php artisan --no-ansi key:generate --show
base64:xxxxx
> cd ~/dev/project
> heroku config:set APP_KEY=xxxxx
Setting APP_KEY and restarting ⬢ [アプリケーション名]... done, v4
APP_KEY: base64:xxxxx

herokuへデプロイ

git push heroku masterとすると
- masterブランチをpush
- リポジトリのルートフォルダからpush

してしまうので、git push subtree xxxにすること(2敗)

> cd ~/dev/project
> git subtree push --prefix src/ heroku master

アプリケーションの起動

> heroku open

laravelのwencome画面が表示されたらOK。


DBの追加とマイグレーション

herokuだとMySQL環境構築に無償プランでも
クレジットカードの登録が必要なので、
PostgreSQLにするのが、おすすめ。

> heroku addons:add heroku-postgresql
Creating heroku-postgresql on ⬢ [アプリケーション名]... free
Database has been created and is available
 ! This database is empty. If upgrading, you can transfer
 ! data from another database with pg:copy
Created postgresql-cylindrical-99644 as DATABASE_URL
Use heroku addons:docs heroku-postgresql to view documentation

herokuの環境変数にDB接続情報を設定する

DB接続URL[DATABASE_URL]を取得する。

> cd ~/dev/project
> heroku config
=== [アプリケーション名] Config Vars
APP_KEY:      base64:xxxxx
DATABASE_URL: postgres://[ユーザ名]:[パスワード]@[ホスト名]:[ポート]/[データベース名]

herokuの環境変数に設定
DB_CONNECTIONは「postgres」じゃなくて「pgsql」(1敗)

> heroku config:set DB_CONNECTION=pgsql
> heroku config:set DB_HOST=[ホスト名]
> heroku config:set DB_PORT=[ポート]
> heroku config:set DB_DATABASE=[データベース名]
> heroku config:set DB_USERNAME=[ユーザ名]
> heroku config:set DB_PASSWORD=[パスワード]

マイグレーションしてみる。

> heroku run "php artisan migrate:install"
Running php artisan migrate:install on ⬢ [アプリケーション名]... up, run.6239 (Free)
Migration table created successfully.
> heroku run "php artisan migrate:status"
▸    heroku-cli: update available from xxxxx to xxxxx
Running php artisan migrate:status on ⬢ [アプリケーション名]... up, run.5021 (Free)
+------+------------------------------------------------+-------+
| Ran? | Migration                                      | Batch |
+------+------------------------------------------------+-------+
| N    | 2014_10_12_000000_create_users_table           |       |
| N    | 2014_10_12_100000_create_password_resets_table |       |
+------+------------------------------------------------+-------+

ドハマリしたらご参考に上げてる記事をご参考に。
ではでは、良きLaravel開発ライフを^^


ご参考

Laradockを使ったlaravel開発環境
https://qiita.com/akokubu/items/c79bb7128d7b16a20a22

Laravelアプリをherokuで動かすチュートリアル
https://qiita.com/kamukiriri/items/73ff35fc7f9083ca68c1

The Heroku CLI
https://devcenter.heroku.com/articles/heroku-cli

[Docker] Docker for Macでmysqlを起動〜接続
https://qiita.com/jshimazu/items/de7a206c4bafecefd359

Heroku初心者がHello, Herokuをしてみる
https://qiita.com/Arashi/items/b2f2e01259238235e187

33
25
1

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
33
25