Memo

メモ > 技術 > フレームワーク: Laravel6 > Homestead

■Homestead
Laravel Homestead 6.x Laravel https://readouble.com/laravel/6.x/ja/homestead.html Laravel環境構築 Homestead-Vagrant(Windows10) | -ANDROIDろいど- プログラム開発入門&情報共有ブログ http://androidroid.info/laravel-homestead-install/ Homesteadをインストールするだけで Ubuntu18 + PHP7 + Nginx + MySQL の環境を構築できる 以下、VirtualBox + Vagrant の環境は構築済みとする ■Homesteadのインストール Homesteadをダウンロードする
>cd C:\Users\refirio\Vagrant >vagrant box add laravel/homestead ==> box: Loading metadata for box 'laravel/homestead' box: URL: https://vagrantcloud.com/laravel/homestead This box can work with multiple providers! The providers that it can work with are listed below. Please review the list and choose the provider you will be working with. 1) hyperv 2) parallels 3) virtualbox Enter your choice: 3 … virtualboxで使うので3を入力してEnter ==> box: Adding box 'laravel/homestead' (v11.4.0) for provider: virtualbox box: Downloading: https://vagrantcloud.com/laravel/boxes/homestead/versions/11.4.0/providers/virtualbox.box box: box: Calculating and comparing box checksum... ==> box: Successfully added box 'laravel/homestead' (v11.4.0) for 'virtualbox'! … 5分ほどで完了 >vagrant box list centos/7 (virtualbox, 2004.01) laravel/homestead (virtualbox, 11.4.0) … Homesteadが追加されたことを確認できる
Homestead用の領域を作成して起動する ※これは「案件のためのファイルをインストール」ではなく「Homesteadの仕組みそのもののインストール」なので、 案件ごとの作業領域ではなく、Homestead用の領域としてインストールする方が良さそう Homesteadで複数案件を扱いたければ、Homestead内の設定で対応する(案件ごとの領域を作成する)
>cd C:\Users\refirio\Vagrant >git clone https://github.com/laravel/homestead.git homestead >cd homestead >git checkout release … 最新の安定バージョンは https://github.com/laravel/homestead/releases で確認。もしくは常に最新の安定バージョンを用意しているreleaseブランチにチェックアウト >init.bat … 設定ファイルを作成(Windowsの場合。Mac/LinuxもしくはWindowsgit bash環境なら「bash init.sh」とする)
インストールしたHomesteadのバージョンは、以下に記載されている C:\Users\refirio\Vagrant\homestead\bin\homestead
$app = new Symfony\Component\Console\Application('Laravel Homestead', '12.4.1');
■鍵の作成 gitを使っているなら Git Bash で作成できる WindowsでGitを始めたらまず確認!Git Bashの設定&ショートカット | 株式会社グランフェアズ http://www.granfairs.com/blog/staff/gitbash-setting-shortcut
$ mkdir C:\Users\refirio\.ssh $ cd C:\Users\refirio\.ssh $ ssh-keygen -t rsa … もしくは「ssh-keygen -b 4096」とするか Generating public/private rsa key pair. Enter file in which to save the key (/c/Users/refirio/.ssh/id_rsa): … 空ENTER Enter passphrase (empty for no passphrase): … 空ENTER Enter same passphrase again: … 空ENTER Your identification has been saved in /c/Users/refirio/.ssh/id_rsa. Your public key has been saved in /c/Users/refirio/.ssh/id_rsa.pub. The key fingerprint is: SHA256:vjqdyEjv9TO5Ywmox+PfV7yDoF5ljEqbIR/SRFuknBQ refirio@WorkComputer The key's randomart image is: +---[RSA 4096]----+ | Eoo | | + = | | * | | o o | | o.S . +. | | . .*.*.o o | | . * oB+.+ o . | | o O.+=B o o | | ++*+.+* . | +----[SHA256]-----+
これで C:\Users\ユーザ名\.ssh 内に id_rsa と id_rsa.pub が作成される。ここはHomesteadからみると ~/.ssh/ にあたる ■Homesteadの設定 C:\Users\refirio\Vagrant\homestead\Homestead.yaml を編集する 鍵の設定は以下にあるが、上の手順で作成した場所がデフォルトとなっているため変更は不要 他の方法で鍵を作成した場合、必要に応じて設定を変更する
authorize: C:\Users\refirio\.ssh\id_rsa.pub keys: - C:\Users\refirio\.ssh\id_rsa
また、同ファイルで共有フォルダの設定を調整する C:\Users\refirio\Vagrant\homestead\code\homestead\public はあらかじめ作成しておく
folders: - map: ~/code to: /home/vagrant/code sites: - map: homestead.test to: /home/vagrant/code/public ↓ folders: - map: C:\Users\refirio\Vagrant\homestead\code\homestead to: /home/vagrant/homestead sites: - map: homestead.test to: /home/vagrant/homestead/public
■hostsの設定 http://homestead.test/ でアクセスできるようにする C:\Windows\System32\drivers\etc\hosts
192.168.10.10 homestead.test
■Vagrantの起動 初回起動時、「変更を許可しますか?」の確認ダイアログが2回表示された すべて許可して進めた
>cd C:\Users\refirio\Vagrant\homestead >vagrant up Bringing machine 'homestead' up with 'virtualbox' provider... ==> homestead: Checking if box 'laravel/homestead' version '11.4.0' is up to date... ==> homestead: Clearing any previously set forwarded ports... ==> homestead: Clearing any previously set network interfaces... ==> homestead: Preparing network interfaces based on configuration... homestead: Adapter 1: nat homestead: Adapter 2: hostonly ==> homestead: Forwarding ports... homestead: 80 (guest) => 8000 (host) (adapter 1) homestead: 443 (guest) => 44300 (host) (adapter 1) homestead: 22 (guest) => 2222 (host) (adapter 1) ==> homestead: Running 'pre-boot' VM customizations... ==> homestead: Booting VM... ==> homestead: Waiting for machine to boot. This may take a few minutes... homestead: SSH address: 127.0.0.1:2222 homestead: SSH username: vagrant homestead: SSH auth method: private key ==> homestead: Machine booted and ready! ==> homestead: Checking for guest additions in VM... ==> homestead: Setting hostname... ==> homestead: Configuring and enabling network interfaces... ==> homestead: Mounting shared folders... homestead: /vagrant => C:/Users/refirio/Vagrant/homestead homestead: /home/vagrant/homestead => C:/Users/refirio/Vagrant/homestead/code/homestead ==> homestead: Detected mount owner ID within mount options. (uid: 1000 guestpath: /home/vagrant/homestead) ==> homestead: Detected mount group ID within mount options. (gid: 1000 guestpath: /home/vagrant/homestead) ==> homestead: Machine already provisioned. Run `vagrant provision` or use the `--provision` ==> homestead: flag to force provisioning. Provisioners marked to run always will still run.
■SSHでの接続 コマンドプロンプトから以下を実行して接続する
>vagrant ssh Welcome to Ubuntu 20.04.2 LTS (GNU/Linux 5.4.0-81-generic x86_64) * Homestead v12.4.1 | Thanks for using Homestead * Settler v11.4.0 $ ll homestead/ total 4 drwxrwxrwx 1 vagrant vagrant 0 Aug 21 14:48 ./ drwxr-xr-x 10 vagrant vagrant 4096 Aug 21 15:26 ../ drwxrwxrwx 1 vagrant vagrant 0 Aug 21 14:48 public/
homestead/public/index.php を作成し、以下を入力する
<?php phpinfo() ?>
以下でアクセスできる。PHPは「8.0.9」だった http://homestead.test/ もしくはPoderosaなどで以下に接続する 接続先: 127.0.0.1 ポート: 2222 ユーザ名: vagrant パスワード: vagrant 環境によっては「The negotiation of kex algorithm is failed」というエラーになって接続できないことがあったが、 これはPoderosaのバージョンが古いだけ、かもしれない Poderosaで「The negotiation of encryption algorithm is failed」エラー https://mook.jpn.org/archives/2016/01/post-4505.html 鍵を作成する際に「ssh-keygen -b 4096」ではなく「ssh-keygen -t rsa」とすればいいかと思ったが、これでも解消しなかった もしくはVirtualBoxの「設定」アイコンのある画面で「表示」をクリックするとコンソールが開く ログインを求められるが、 vagrant login: vagrant Password: vagrant でログインできる ■MariaDBを使用する C:\Users\refirio\Vagrant\homestead\Homestead.yaml
features: - mysql: true - mariadb: false - postgresql: false - ohmyzsh: false - webdriver: false ↓ features: - mysql: true - mariadb: true - postgresql: false - ohmyzsh: false - webdriver: false
Laravel Homestead 6.x Laravel https://readouble.com/laravel/6.x/ja/homestead.html#installing-optional-features 「MariaDBを有効にすると、MySQLを削除してMariaDBをインストールします。 MariaDBはMySQLのそのまま置き換え可能な代替機能として動作します。 そのため、アプリケーションのデータベース設定では、mysqlデータベースドライバをそのまま使ってください。」 ■homestead内部に作業領域を追加した例 Vagrantを停止しておく
>vagrant halt
C:\Users\refirio\Vagrant\homestead\code\sample\public を作成 C:\Users\refirio\Vagrant\homestead\Homestead.yaml を編集
folders: - map: C:\Users\refirio\Vagrant\homestead\code\homestead to: /home/vagrant/homestead - map: C:\Users\refirio\Vagrant\homestead\code\sample to: /home/vagrant/sample sites: - map: homestead.test to: /home/vagrant/homestead/public - map: sample.homestead.test to: /home/vagrant/sample/public databases: - homestead - sample
C:\Windows\System32\drivers\etc\hosts を編集
192.168.10.10 sample.homestead.test
以下で実行
>vagrant up --provision
以下でアクセス http://sample.homestead.test/ ■homestead外部に作業領域を追加した例 C:\Users\refirio\Vagrant\homestead\code はHomestead自体のGit管理領域なので、本格的に作業するなら、この外に作る方がいいかもしれない Vagrantを停止しておく
>vagrant halt
C:\Users\refirio\Vagrant\homestead_happy-link\code\public を作成 C:\Users\refirio\Vagrant\homestead\Homestead.yaml を編集
folders: - map: C:\Users\refirio\Vagrant\homestead\code\homestead to: /home/vagrant/homestead - map: C:\Users\refirio\Vagrant\homestead\code\sample to: /home/vagrant/sample - map: C:\Users\refirio\Vagrant\homestead_happy-link\code to: /home/vagrant/happy-link sites: - map: homestead.test to: /home/vagrant/homestead/public - map: sample.homestead.test to: /home/vagrant/sample/public - map: happy-link.local to: /home/vagrant/happy-link/public databases: - homestead - sample - happy-link
C:\Windows\System32\drivers\etc\hosts を編集
192.168.10.10 happy-link.local
以下で実行
>vagrant up --provision
以下でアクセス http://happy-link.local/ ■データベースに接続 Laravel Homestead 6.x Laravel https://readouble.com/laravel/6.x/ja/homestead.html#connecting-to-databases 以下で接続できる rootでは接続できない?要確認
$ mysql -u homestead -p secret
以下でPHPプログラムから接続できる
<?php try { $pdo = new PDO( 'mysql:dbname=sample;host=localhost', 'homestead', 'secret' ); $stmt = $pdo->query('SELECT NOW() AS now;'); $data = $stmt->fetch(PDO::FETCH_ASSOC); echo "<p>" . $data['now'] . "</p>\n"; $pdo = null; } catch (PDOException $e) { exit($e->getMessage()); }
■PHPバージョンを指定 Laravel Homestead 6.x Laravel https://readouble.com/laravel/6.x/ja/homestead.html#daily-usage 「複数のPHPバージョン」 C:\Users\refirio\Vagrant\homestead\Homestead.yaml を編集
sites: - map: homestead.test to: /home/vagrant/homestead/public - map: sample.homestead.test to: /home/vagrant/sample/public php: "7.4"
>vagrant reload --provision
以下のエラーが出るようになってしまった
Timed out while waiting for the machine to boot. This means that Vagrant was unable to communicate with the guest machine within the configured ("config.vm.boot_timeout" value) time period.
削除すると起動できるようになった ただしこれはたまたまで、本当の問題は別にある…かもしれない もしくは、SSHで接続して以下のようにすることでも変更できる
>vagrant ssh $ php74 update-alternatives: using /usr/bin/php7.4 to provide /usr/bin/php (php) in manual mode update-alternatives: using /usr/bin/php-config7.4 to provide /usr/bin/php-config (php-config) in manual mode update-alternatives: using /usr/bin/phpize7.4 to provide /usr/bin/phpize (phpize) in manual mode $ php -v PHP 7.4.22 (cli) (built: Jul 30 2021 13:08:17) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.22, Copyright (c), by Zend Technologies $ exit >vagrant halt >vagrant up

Advertisement