はじめに
5/17 追記:Dockerの学習を通じてV-Hyperを切ることでVagrantが起動できるようになったので再挑戦しました。
✅環境
Windows10 Pro
✅参考
📚PHPフレームワークLaravel Webアプリケーション開発
Kindleだと半額になってる!!
導入
✅Homesteadとは
Laravelによる開発に必要な基本構成がひとまとめになったLaravel公式による仮想環境(VM)用のパッケージ
※PHP/MySQL/PostgreSQL/nginxなどやミドルウェアが設定されたLinux OS環境のパッケージ。
✅ホストとゲスト
仮想環境を構築するコンピュータ自体をホスト、仮想環境側をゲストと呼ぶ。
Vagrantコマンドはホスト側、composerやnpmコマンドはゲスト側で行う(npmコマンドはどちらかに任意で統一する)。
ソースコードは共有フォルダ内の作業であれば一方の編集がもう一方へ反映されるのでホスト側でもOK。
Windows10の環境構築
①Gitの確認
git version >> git version 2.24.0.windows.2
②Vagrantの確認
vagrant --version >> Vagrant 2.2.6
③Homesteadのダウンロード
boxというHomesteadを実行するために必要なOSやソフトウェアがあらかじめ設定された仮想環境のセットをダウンロード。
vagrant box add laravel/homestead // VirtualBoxを選択。時間かかる。 git clone https://github.com/laravel/homestead.git Homestead // Homesteadディレクトリのダウンロード
④Homesteadの初期化
cd homestead ./init.bat >> Homestead.yaml を上書きしますか? (Yes/No/All): Yes 1 個のファイルをコピーしました。 after.sh を上書きしますか? (Yes/No/All): yes 1 個のファイルをコピーしました。 aliases を上書きしますか? (Yes/No/All): yes 1 個のファイルをコピーしました。 Homestead initialized!
⑤Homestead.yamlの編集
provider: virtualbox // プロバイダの設定 ~~~~ folders: // 共有ディレクトリの設定 - map: ~/code // ホスト側 to: /home/vagrant/code // ゲスト側 ~~~~ sites: // サイトの設定 - map: homestead.test to: /home/vagrant/code/sampleapp/public
⑥共有ディレクトリの作成
cd mkdir code
⑦hosts定義の追加
C:\Windows\System32\drivers\etc\hosts
192.168.10.10 homestead.test # vagrant homestead用
⑧Vagrantの起動
cd homestead vagrant up
http://homestead.testで確認(※現時点で表示するファイルがないことを示してる。)

⑨仮想環境へのログイン
vagrant ssh >> Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-74-generic x86_64) Thanks for using _ _ _ | | | | | | | |__ ___ _ __ ___ ___ ___| |_ ___ __ _ __| | | '_ \ / _ \| '_ ` _ \ / _ \/ __| __/ _ \/ _` |/ _` | | | | | (_) | | | | | | __/\__ \ || __/ (_| | (_| | |_| |_|\___/|_| |_| |_|\___||___/\__\___|\__,_|\__,_| * Homestead v10.8.0 * Settler v9.5.1 (Virtualbox, Parallels, Hyper-V, VMware) 0 packages can be updated. 0 updates are security updates.
⑩Laravelプロジェクトの作成
※sshでvagrant/homesteadにログインしている状態で。
cd code laravel new sampleapp exit // sshから離脱
確認

⑪Vagrantの終了
vagrant halt // シャットダウン vagrant suspend // 一時停止
⑫プロジェクトの編集
Homestead.yamlで設定したホスト側のディレクトリに作成されているプロジェクトをVSCodeで直接弄って大丈夫っぽい。
~/code/sampleapp
データベースのセットアップ
✅MySQL編
①.envを編集
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=homestead DB_USERNAME=homestead DB_PASSWORD=secret
②ゲスト側からphp artisan migrate
vagrant@homestead:~/code/sampleapp$ php artisan migrate >> Migration table created successfully. Migrating: 2014_10_12_000000_create_users_table Migrated: 2014_10_12_000000_create_users_table (0.09 seconds) Migrating: 2019_08_19_000000_create_failed_jobs_table Migrated: 2019_08_19_000000_create_failed_jobs_table (0.05 seconds) mysql -uroot // ログイン
✅PostgreSQL編
参考:LaravelのHomestead環境でPostgreSQL初期設定を行う
①.envを編集
DB_CONNECTION=pgsql DB_HOST=127.0.0.1 DB_PORT=5432 DB_DATABASE=homestead DB_USERNAME=homestead DB_PASSWORD=secret
②ゲスト側からphp artisan migrate
vagrant@homestead:~/code/sampleapp$ php artisan migrate >> Migration table created successfully. Migrating: 2014_10_12_000000_create_users_table Migrated: 2014_10_12_000000_create_users_table (0.09 seconds) Migrating: 2019_08_19_000000_create_failed_jobs_table Migrated: 2019_08_19_000000_create_failed_jobs_table (0.05 seconds) psql -U homestead -h localhost // ログイン >> パスワードのデフォルトはsecret
躓いた点
✅Homestead.yamlの変更が適用されない。
解決:Vagrant起動した後は変更適用のコマンドを打つ必要がある
vagrant reload --provision // 再起動 vagrant up --provision // 停止している状態からの起動
おわりに
(5/17追記)
ずっと悩まされ続けたVagrantでの環境構築ができた。幸せです。
少しずつXamppから移行していこうと思います。
✅参考記事
Windows10でLaravel Homestead環境構築
【Laravel超入門】開発環境の構築(VirtualBox + Vagrant + Homestead + Composer)
Laravel 5.7 Laravel Homestead
【まとめ】Vagrant コマンド一覧
Laravel開発のはじめかた Windows編
✅その他参考
Windows Power Shell をアップデート!Windwos7 に Power Shell 5.1をインストール
秘密鍵・公開鍵を作成する(Win)
備忘録
※3/24時点
✅Windows7の環境構築 ※失敗
Windows7だとDockerは対応していないとのこと。
Dockerの挑戦をしたいところでしたがデスクトップPCが未だWindows7なのでVagrant環境にチャレンジしてみました。
PowerShellのバージョンを2から5.1に変更したのち以下コマンド。1時間以上待機してエラー。。。泣
vagrant box add laravel/homestead

デスク容量が少ないことを思い出して少し整理。2GBから17GBまで空けてから再挑戦。(DTMは要領食う)


success!!容量が足りなかっただけっぽい。
Homesteadをgitコマンドでダウンロード。Homesteadの初期化。
git clone https://github.com/laradock/laradock.git cd homestead ./init.bat
Homestead.yamlを編集。
sites: - map: homestead.test to: /home/vagrant/code/sampleapp/public //Laravelプロジェクトの名前を追記
Homestead.yamlで記載されていた下記のとおりにディレクトリを作成する。
folders: - map: ~/code to: /home/vagrant/code
cd $userprofile mkdir code
Homestead.yamlで記載されていた下記のとおりにC:\Windows\System32\drivers\etc上にあるhostsファイルに追記。
sites: - map: homestead.test to: /home/vagrant/code/sampleapp/public //Laravelプロジェクトの名前を追記
192.168.10.10 homestead.test
Vagrantを起動する。以下のエラー。
cd homestead vagrant up Check your Homestead.yaml (or Homestead.json) file, the path to your private key does not exist.
秘密鍵・公開鍵を作成(PowerShellだと実行できないのでGit Bashを使用する。)
mkdir .ssh cd .ssh ssh-keygen
Vagrantを再起動。何度か変更を許可しますかのポップが表示されるので許可。成功したと思いきやエラー。。。

Vagrantが使用するCLIである `VBoxManage`の実行中にエラーが発生。ネイティブAPI dllが見つかりませんでした。VT-xはすべてのCPUモードのBIOSで無効です。とのこと。
コメントを残す