Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Laravelを本番環境にデプロイするまで

yukure
March 18, 2019

 Laravelを本番環境にデプロイするまで

yukure

March 18, 2019
Tweet

More Decks by yukure

Other Decks in Programming

Transcript

  1. LaravelΛ
    ຊ൪؀ڥʹ
    σϓϩΠ͢Δ·Ͱ
    @yukure3

    View full-size slide

  2. ࣗݾ঺հ
    • ໊લɿߚྛ ༏ (@yukure3)
    • ॴଐɿגࣜձࣾ͏ΔΔ
    • ܦྺɿ৽ଔ̍೥໨ΤϯδχΞ
    • ࠷ۙ΍͍ͬͯΔ͜ͱɿLaravel / Vue.js
    • ͓఻͑ͨ͜͠ͱɿࠓճͷLT͕ਓੜॳొஃͰ͢…!!

    View full-size slide

  3. ࣗݾ঺հ
    • ໊લɿߚྛ ༏ (@yukure3)
    • ॴଐɿגࣜձࣾ͏ΔΔ
    • ܦྺɿ৽ଔ̍೥໨ΤϯδχΞ
    • ࠷ۙ΍͍ͬͯΔ͜ͱɿLaravel / Vue.js
    • ͓఻͑ͨ͜͠ͱɿࠓճͷLT͕ਓੜॳొஃͰ͢…!!

    View full-size slide

  4. ͜Μͳํʹ
    • ʮͳΜ͔LaravelΑͦ͞͏͔ͩΒ࢖ͬͯΈ͍ͨͳ͊ɻʯ
    • ʮ։ൃ؀ڥͲ͏্ཱͪ͛Δͷָ͔ͳͷ͔ͳ͊ɻʯ
    • ʮຊ൪ӡ༻΋ߟ͑ͳ͍ͱ͍͚ͳ͍͚ͲָͰ͖ͳ͍͔ͳ͊ɻʯ

    View full-size slide

  5. ͜Μͳ͜ͱΛ఻͍͑ͨ
    Laravel ʹूதͰ͖Δํ๏
    ※Laravelࣗମͷྑ͞͸ଞͷํʑ͕ͨ͘͞Μઆ໌ͯ͘͠Ε·͢ʂʂ

    View full-size slide

  6. ΞδΣϯμ
    • ࣮ݱ͍ͨ͜͠ͱ
    • ։ൃ؀ڥͱӡ༻؀ڥʹखؒΛ͔͚ͣʹߏங͢Δ
    • ඞཁͳ΋ͷ
    • ্هΛ࣮ݱ͢ΔͨΊʹඞཁͳ΋ͷΛ঺հ
    • ·ͱΊ

    View full-size slide

  7. ࣮ݱ͍ͨ͜͠ͱ

    View full-size slide

  8. ࣮ݱ͍ͨ͜͠ͱ

    View full-size slide

  9. ඞཁͳ΋ͷ

    View full-size slide

  10. ϩʔΧϧ։ൃ؀ڥ
    => Laradock
    ຊ൪؀ڥ
    => AWS Elastic Beanstalk
    CI/CDύΠϓϥΠϯ
    => CircleCI
    όʔδϣϯ؅ཧ
    => GitHub
    ඞཁͳ΋ͷ

    View full-size slide

  11. ϩʔΧϧ։ൃ؀ڥ

    View full-size slide

  12. ϩʔΧϧ։ൃ؀ڥ
    ͜͜Λ࡞͍͖ͬͯ·͢ʂ

    View full-size slide

  13. ϩʔΧϧ։ൃ؀ڥ
    # σΟϨΫτϦ࡞੒ & ͦͷσΟϨΫτϦʹҠಈ
    $ mkdir project && cd project
    # laradockΛΫϩʔϯ
    $ git clone https://github.com/Laradock/laradock.git
    # laravel(࠷৽൛)ΛΠϯετʔϧ
    $ composer create-project --prefer-dist laravel/laravel src
    # LaradockσΟϨΫτϦҠಈ & .envΛ࡞੒
    $ cd laradock
    $ cp env-example .env
    # σΟϨΫτϦߏ଄
    project
    !"" laradock ←ΠϚίίʂʂ
    #"" src

    View full-size slide

  14. .env(laradock)ͷमਖ਼
    # nginx ͷport & mysqlͷ version มߋ
    - APP_CODE_PATH_HOST=../
    + APP_CODE_PATH_HOST=../src
    - NGINX_HOST_HTTP_PORT=80
    + NGINX_HOST_HTTP_PORT=8001
    - MYSQL_VERSION=latest
    + MYSQL_VERSION=5.7

    View full-size slide

  15. .env(laravel)ͷमਖ਼
    # ઀ଓઌͷDBมߋ
    - DB_HOST=127.0.0.1
    + DB_HOST=mysql
    - DB_DATABASE=homestead
    + DB_DATABASE=default
    - DB_USERNAME=homestead
    + DB_USERNAME=root
    - DB_PASSWORD=secret
    + DB_PASSWORD=root

    View full-size slide

  16. ϩʔΧϧ։ൃ؀ڥΛ্ཱͪ͛Δ
    # ΠϝʔδΛϏϧυͯ͠ىಈ͢Δ
    docker-compose up -d nginx mysql workspace
    MPDBMIPTUʹΞΫηε

    View full-size slide

  17. ຊ൪؀ڥ
    ͜͜Λ࡞͍͖ͬͯ·͢ʂ

    View full-size slide

  18. ίϯιʔϧͰBeanstalkΛݕࡧ

    View full-size slide

  19. ৽͍͠ΞϓϦέʔγϣϯͷ࡞੒

    View full-size slide

  20. ΞϓϦέʔγϣϯ໊Λઃఆ

    View full-size slide

  21. ΞϓϦέʔγϣϯ಺ʹ؀ڥΛ࡞੒

    View full-size slide

  22. ΢Σϒαʔόʔ؀ڥΛߏங

    View full-size slide

  23. ؀ڥ৘ใͷઃఆ

    View full-size slide

  24. ؀ڥ৘ใͷઃఆ

    View full-size slide

  25. DB(RDS)ͷઃఆ

    View full-size slide

  26. DB(RDS)ͷઃఆ

    View full-size slide

  27. ΢Σϒαʔόʔ؀ڥ࡞੒

    View full-size slide

  28. Ͱ͖·ͨ͠ʂ

    View full-size slide

  29. όʔδϣϯ؅ཧ

    View full-size slide

  30. όʔδϣϯ؅ཧ
    ͜͜Λ࡞͍͖ͬͯ·͢ʂ

    View full-size slide

  31. ϦϞʔτϦϙδτϦ࡞੒

    View full-size slide

  32. ϦϞʔτϦϙδτϦ࡞੒
    ΫϦοϓϘʔυʹίϐʔ

    View full-size slide

  33. ϦϞʔτϦϙδτϦ΁push
    # src΁Ҡಈ
    cd src
    # gitϦϙδτϦ࡞੒
    git init
    # ઌ΄Ͳ࡞੒ͨ͠ϦϞʔτϦϙδτϦͷ௥Ճ
    git remote add upstream [email protected]:yukure/laravel-vue.git
    # first commit ͸͔ΒίϛοτͰʂ
    git commit --allow-empty -m "first commit!!"
    # laravelΛόʔδϣϯ؅ཧͷର৅ͱͯ͠௥Ճ
    git add .
    # ίϛοτ
    git commit -m "add: laravel app"
    # ϦϞʔτϦϙδτϦʹΞοϓϩʔυ
    git push upstream master

    View full-size slide

  34. ϦϞʔτϦϙδτϦ΁push

    View full-size slide

  35. σϑΥϧτϒϥϯνΛมߋ

    View full-size slide

  36. σϑΥϧτϒϥϯνΛมߋ

    View full-size slide

  37. σϑΥϧτϒϥϯνΛมߋ

    View full-size slide

  38. CI/CDύΠϓϥΠϯ

    View full-size slide

  39. CI/CDύΠϓϥΠϯ
    ͜͜Λ࡞͍͖ͬͯ·͢ʂ

    View full-size slide

  40. σϓϩΠ༻ͷIAMͷ࡞੒

    View full-size slide

  41. σϓϩΠ༻ͷIAMͷ࡞੒

    View full-size slide

  42. σϓϩΠ༻ͷIAMͷ࡞੒

    View full-size slide

  43. σϓϩΠ༻ͷIAMͷ࡞੒

    View full-size slide

  44. σϓϩΠ༻ͷIAMͷ࡞੒

    View full-size slide

  45. σϓϩΠ༻ͷIAMͷ࡞੒

    View full-size slide

  46. EB CLIͷΠϯετʔϧ
    # EB CLIͷΠϯετʔϧ
    brew install awsebcli
    # EB CLI͕Πϯετʔϧ͞Ε͍ͯΔ͔֬ೝ
    eb --version

    View full-size slide

  47. # srcσΟϨΫτϦ಺Ͱߦ͍ͬͯͩ͘͞
    # ॳظઃఆ(.elasticbeanstalk/config.ymlͷ࡞੒)
    eb init
    # ͜Μͳײ͡Ͱ౴͍͖͑ͯ·͢
    Select a default region
    1) us-east-1 : US East (N. Virginia)
    2) us-west-1 : US West (N. California)
    3) us-west-2 : US West (Oregon)
    4) eu-west-1 : EU (Ireland)
    5) eu-central-1 : EU (Frankfurt)
    6) ap-south-1 : Asia Pacific (Mumbai)
    7) ap-southeast-1 : Asia Pacific (Singapore)
    8) ap-southeast-2 : Asia Pacific (Sydney)
    9) ap-northeast-1 : Asia Pacific (Tokyo)
    10) ap-northeast-2 : Asia Pacific (Seoul)
    11) sa-east-1 : South America (Sao Paulo)
    12) cn-north-1 : China (Beijing)
    13) cn-northwest-1 : China (Ningxia)
    14) us-east-2 : US East (Ohio)
    15) ca-central-1 : Canada (Central)
    16) eu-west-2 : EU (London)
    17) eu-west-3 : EU (Paris)
    (default is 3): 9
    Select an application to use
    1) laravel-vue
    2) [ Create new Application ]
    (default is 1): 1
    Select the default environment.
    You can change this later by typing "eb use [environment_name]".
    1) laravel-vue
    (default is 1): 1
    Note: Elastic Beanstalk now supports AWS CodeCommit; a fully-managed source control service. To learn
    more, see Docs: https://aws.amazon.com/codecommit/
    Do you wish to continue with CodeCommit? (y/N) (default is n): n
    Ϧʔδϣϯͷબ୒
    ͲͷΞϓϦέʔγϣϯΛ࢖͏͔
    Ͳͷ؀ڥʹσϓϩΠ͢Δ͔
    $PEF$PNNJU࢖͏͔

    View full-size slide

  48. config.global.ymlͷઃఆ
    # ઃఆϑΝΠϧ࡞੒
    vim .elasticbeanstalk/config.global.yml
    # ͜ͷΑ͏ʹઃఆ͢Δ
    global:
    application_name: laravel-vue
    default_region: ap-northeast-1
    ͖͞΄Ͳ࡞੒ͨ͠ͷΞϓϦέʔγϣϯ໊

    View full-size slide

  49. CircleCIͷઃఆ

    View full-size slide

  50. .circleci/config.ymlͷઃఆ
    # ※srcσΟϨΫτϦ಺Ͱ↓Λߦ͍ͬͯͩ͘͞
    # .circleciσΟϨΫτϦ࡞੒ & σΟϨΫτϦʹҠಈ
    mkdir .circleci && cd .circleci
    # ઃఆϑΝΠϧΛ࡞੒
    vim config.yml

    View full-size slide

  51. version: 2
    defaults: &defaults
    working_directory: ~/app
    docker:
    - image: circleci/php:7.1-node-browsers
    - image: circleci/mysql:5.7
    environment:
    - AWS_REGION: ap-northeast-1
    - APP_DEBUG: true
    - APP_ENV: testing
    - APP_KEY: base64:cNWYphm1YWH+RGYFLZpn6MBKj/TRiMm46KbuWA8Ei8c=
    - DB_DATABASE: circle_test
    - DB_USERNAME: root
    - MYSQL_ALLOW_EMPTY_PASSWORD: true
    jobs:
    build:
    <<: *defaults
    steps:
    - checkout
    - run:
    name: install pdo_mysql
    command: |
    sudo apt update
    sudo docker-php-ext-install pdo_mysql
    sudo docker-php-ext-enable pdo_mysql
    # Download and cache dependencies
    - restore_cache:
    keys:
    - v1-dependencies-{{ checksum "composer.json" }}
    # fallback to using the latest cache if no exact match is found
    - v1-dependencies-
    - run: composer install -n --prefer-dist
    - save_cache:
    paths:
    - ./vendor
    key: v1-dependencies-{{ checksum "composer.json" }}
    # run seeding
    - run: php artisan migrate
    - run: php artisan db:seed
    # run test
    - run: ./vendor/bin/phpunit
    deploy:
    <<: *defaults
    steps:
    - checkout
    - run:
    name: Install awsebcli
    command: |
    sudo apt-get -y -qq update
    sudo apt-get install python-pip python-dev build-essential
    sudo pip install awsebcli --upgrade
    - run:
    name: eb deploy
    command: eb deploy laravel-vue
    workflows:
    version: 2
    build_and_deploy:
    jobs:
    - build
    - deploy:
    filters:
    branches:
    only:
    - master
    ίϯςφͷΠϝʔδ΍
    ؀ڥม਺Λఆٛ
    Ϗϧυɾςετͷ࣮ߦ
    8PSLqPX δϣϒͷॱ൪

    ӡ༻؀ڥ΁σϓϩΠ

    View full-size slide

  52. .circleci/config.ymlͷઃఆ
    deploy:
    <<: *defaults
    steps:
    - checkout
    - run:
    name: Install awsebcli
    command: |
    sudo apt-get -y -qq update
    sudo apt-get install python-pip python-dev build-essential
    sudo pip install awsebcli --upgrade
    - run:
    name: eb deploy
    command: eb deploy laravel-vue ͷ؀ڥ໊
    $-*Πϯετʔϧ
    HJUIVC͔ΒίʔυΛνΣοΫΞ΢τ

    View full-size slide

  53. ͳΜͷઃఆΛͨ͠ͷ͔
    • PR͕࡞੒͞ΕͨΒɺϏϧυɾςετΛ࣮ߦɻ
    • masterϒϥϯνʹPR͕Ϛʔδ͞ΕͨΒ 

    eb deployίϚϯυΛ࣮ߦɻ

    View full-size slide

  54. CircleCIͷઃఆ

    View full-size slide

  55. CircleCIͷઃఆ

    View full-size slide

  56. CircleCIͷઃఆ

    View full-size slide

  57. CircleCIͷઃఆ

    View full-size slide

  58. ؀ڥม਺ͷઃఆ

    View full-size slide

  59. ؀ڥม਺ͷઃఆ

    View full-size slide

  60. ؀ڥม਺ͷઃఆ
    $47ʹهࡌͷ͋ΔΩʔΛೖྗ $47ʹهࡌͷ͋Δ஋Λೖྗ

    View full-size slide

  61. σϓϩΠͷςετ
    # ϒϥϯνͷ੾Γସ͑
    git checkout -b edit_readme
    # readme.mdΛฤू͢Δ
    vim readme.md
    # Laravel/Vue.jsษڧձ#8 Φʔϧελʔζ
    **Let's Enjoy Laravel!!!!!!!!!!!!!!!!!!!!!!!!!!**
    readme.md

    View full-size slide

  62. github΁push
    git add .
    git commit -m "edit: readme.md"
    git push upstream edit_readme

    View full-size slide

  63. CircleCIͷδϣϒΛ֬ೝ
    workflow

    View full-size slide

  64. CircleCIͷδϣϒΛ֬ೝ
    workflow

    View full-size slide

  65. ϖʔδΛ֬ೝͯ͠ΈΔ

    View full-size slide

  66. υΩϡϝϯτϧʔτͷઃఆ

    View full-size slide

  67. APP_KEYͷઃఆ
    TSDFOWͷ"11@,&:Λίϐϖ͍ͯͩ͘͠͞

    View full-size slide

  68. ·ͱΊ
    • ։ൃ؀ڥ͸LaradockΛ࢖ͬͯ؆୯ʹߏஙͰ͖Δʂ
    • ӡ༻؀ڥ͸EBΛ࢖ͬͯ؆୯ʹߏஙͰ͖Δ!
    • CIʗCDύΠϓϥΠϯͰ͓ؾܰӡ༻ʂ

    View full-size slide

  69. Let’s Enjoy Laravel Life!!!

    View full-size slide

  70. ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂʂ#

    View full-size slide