Memo

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

■Homestead
※2019年ごろに検証した内容。2021年に改めて検証した内容は Laravel6.txt に記載している Homesteadをインストールするだけで Ubuntu16 + PHP7 + Nginx + MySQL の環境を構築できる ※Dockerによる開発環境構築は、Docker.txt の「その他の環境構築例」と「Laradockによる開発環境構築」を参照 Homesteadは、1つのPCに複数インストールできない(無理矢理インストールすることは不可能では無いようだが) よって案件名をもとにしたフォルダ内にHomesteadをインストールせず、「homestead」という場所にインストールして、 1つのHomesteadで複数のプロジェクトを管理する方が無難 詳細は「環境構築のトラブル事例」を参照 Laravel Homestead 5.5 Laravel https://readouble.com/laravel/5.5/ja/homestead.html ↑インストール方法など詳しく書かれている 【Laravel超入門】開発環境の構築(VirtualBox + Vagrant + Homestead + Composer) - Qiita http://qiita.com/7968/items/97dd634608f37892b18a Laravel HomesteadでLaravel5.4の環境を作りながら、レンタルサーバにwebアプリケーションを公開する(追記あり) - Qiita http://qiita.com/Fendo181/items/a6b9017f6ef490995aba Windows10でLaravel Homestead環境構築 - Qiita http://qiita.com/ricoirico/items/9745160bcf9983fa30ad Laravel Homestead - Laravel - The PHP Framework For Web Artisans https://laravel.com/docs/5.5/homestead 以下、VirtualBox + Vagrant の環境は構築済みとする ■Homestead Vagrant Box のインストール
>cd C:\vagrant >vagrant box add laravel/homestead ==> box: Loading metadata for box 'laravel/homestead' box: URL: https://atlas.hashicorp.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) parallels 2) virtualbox 3) vmware_desktop Enter your choice: … virtualboxで使うので2を選択 ==> box: Adding box 'laravel/homestead' (v3.0.0) for provider: virtualbox box: Downloading: https://vagrantcloud.com/laravel/boxes/homestead/versions/3.0.0/providers/virtualbox.box box: Progress: 100% (Rate: 3645k/s, Estimated time remaining: --:--:--) ==> box: Successfully added box 'laravel/homestead' (v3.0.0) for 'virtualbox'! … 5分ほどで完了
■Homesteadのインストール ここでは C:\vagrant\homestead にインストールするものとする ※作業ファイルではなくHomesteadの仕組みそのもののインストールなので、 案件ごとの作業領域ではなく C:\vagrant\homestead などHomestead用の領域にインストールする方が良さそう
>cd C:\vagrant\homestead >git clone https://github.com/laravel/homestead.git Homestead >cd Homestead >git checkout v6.2.2 … 最新の安定バージョンは https://github.com/laravel/homestead/releases で確認。v6は「v6.6.0」が最終版 >init.bat … Windowsの場合。Mac/LinuxもしくはWindowsgit bash環境なら「bash init.sh」とする
インストールしたHomesteadのバージョンは、以下に記載されている C:\vagrant\homestead\Homestead\bin\homestead $app = new Symfony\Component\Console\Application('Laravel Homestead', '6.2.2'); ■鍵の作成 gitを使っているなら Git Bash で作成できる 恐らくPoderosaなどを使っても大丈夫と思われる WindowsでGitを始めたらまず確認!Git Bashの設定&ショートカット | 株式会社グランフェアズ http://www.granfairs.com/blog/staff/gitbash-setting-shortcut
$ mkdir ~/.ssh $ cd ~/.ssh $ ssh-keygen -t rsa 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:750v6UHSVnX/ymH37ZbFIQTM94y3Tlhad4WPXfpCIS8 refirio@DESKTOP The key's randomart image is: +---[RSA 2048]----+ | o.. .o| | o.oo.=| | oo=*+| | .E+=B*| | S . +oXoB| | . + =.**| | . ..=.=| | . .oo +.| | ..+o...| +----[SHA256]-----+
これで C:\Users\ユーザ名\.ssh 内に鍵ファイルが作成される。ここはHomesteadからみると ~/.ssh/ にあたる ■Homesteadの設定 C:\vagrant\homestead\Homestead\Homestead.yaml を編集する 鍵の設定は以下にあるが、上の手順で作成した場所がデフォルトとなっているため変更は不要 他の方法で鍵を作成した場合、必要に応じて設定を変更する
authorize: ~/.ssh/id_rsa.pub keys: - ~/.ssh/id_rsa
■hostsの設定 http://homestead.test/ でアクセスできるようにする C:\Windows\System32\drivers\etc\hosts
192.168.10.10 homestead.test
■Vagrantの起動
cd C:\vagrant\homestead\Homestead vagrant up
しばらく待つ…がエラーが表示された Windows10環境での不具合で、最新版のVirtualBoxでは修正されているらしい
Progress state: E_INVALIDARG VBoxManage.exe: error: Failed to create the host-only adapter VBoxManage.exe: error: Assertion failed: [!aInterfaceName.isEmpty()] at 'F:\tinderbox\win-5.1\src\VBox\Main\src-server\HostNetworkInterfaceImpl.cpp' (74) in long __cdecl HostNetworkInterface::init(class com::Bstr,class com::Bstr,class com::Guid,enum __MIDL___MIDL_itf_VirtualBox_0000_0000_0038). VBoxManage.exe: error: Please contact the product vendor! VBoxManage.exe: error: Details: code E_FAIL (0x80004005), component HostNetworkInterfaceWrap, interface IHostNetworkInterface VBoxManage.exe: error: Context: "enum RTEXITCODE __cdecl handleCreate(struct HandlerArg *)" at line 94 of file VBoxManageHostonly.cpp
Windows8.1 から Windows10 にUpgrade して発生したvagrantのエラー - Qiita https://qiita.com/joniyjoniy/items/8176ece2b57c5a40121e https://www.virtualbox.org/ticket/14040 から VBox-Win10-fix-14040.exe をダウンロードし、管理者として実行。再度起動コマンドを試すとVagrantが起動した その後 http://homestead.test/ にアクセスすると、「No input file specified.」とだけ表示される いったん成功 Vagrantを終了する場合は以下のコマンド
vagrant halt
■SSHで接続 「vagrant ssh」コマンドを使うと、SSHで接続するための情報が表示される Poderosaを使って以下の情報で接続する
>vagrant ssh `ssh` executable not found in any directories in the %PATH% variable. Is an SSH client installed? Try installing Cygwin, MinGW or Git, all of which contain an SSH client. Or use your favorite SSH client with the following authentication information shown below: Host: 127.0.0.1 Port: 2222 Username: vagrant Private key: C:/localhost/home/homestead/public_html/Homestead/.vagrant/machines/homestead-7/virtualbox/private_key
以下を参考に時差の調整を行っておくといい Laravel Homesteadの日本時間設定をする - Qiita https://qiita.com/shalman/items/42fcd4506b1f523c5553 ■プロジェクトを作成 ※最初は案件用ではなく、動作確認用のプロジェクトを作成する方がいいかも その後、別途サブドメインと作業フォルダを作る方がいいかも C:\vagrant\homestead\code を作成 C:\vagrant\homestead\Homestead\Homestead.yaml を編集(folders の map のみ)
folders: - map: C:\vagrant\homestead\code to: /home/vagrant/code
この状態でVagrantを再起動 C:\vagrant\homestead\code\public\index.html を作成すると、これが http://homestead.test/ に表示される(SSH経由で /home/vagrant/code/public/index.html を作成しても同じ) このディレクトリの内容は C:\vagrant\homestead\code と同期している ここまで来れば最低限、Homesteadを開発環境として使用できる 以降で引き続き、Laravelのインストールを試す ■Laravelインストール いったん /home/vagrant/code/public/ を削除しておく
$ cd /home/vagrant/code $ sudo composer self-update $ composer create-project laravel/laravel laravel --prefer-dist … とても時間がかかる Installing laravel/laravel (v5.5.0) - Installing laravel/laravel (v5.5.0): Downloading (100%) Created project in Laravel 〜略〜 Application key [base64:WHQKmMRLnXNH9uw3CQRl+fgcwPdwhx9CftI0vBLdgFE=] set successfully.
上でインストールしたLaravelの公開ディレクトリは /home/vagrant/code/laravel/public なので変更する
sites: - map: homestead.test to: /home/vagrant/code/public ↓ sites: - map: homestead.test to: /home/vagrant/code/laravel/public
Vagrantを再起動するも、設定は反映されない Homestead.yaml の sites 設定は「初回起動時にsitesの内容をもとに、Nginxの設定ファイルを調整する」という動作みたい つまり2回目以降、上記設定は無視される Laravel : Homesteadで開発環境を構築 | DN-Web64 http://www.dn-web64.com/archives/web/homestead/ 別途Nginxの設定ファイルを編集し、Nginxを再起動する
$ sudo vi /etc/nginx/sites-available/homestead.test … バージョンによっては「homestead.app」という名前になっている?
server { listen 80; listen 443 ssl http2; server_name homestead.test; #root "/home/vagrant/code/public"; root "/home/vagrant/code/laravel/public"; … 公開ディレクトリを変更
$ sudo service nginx restart
以下のURLでLaravelの画面が表示される http://homestead.test/ ■Laravel動作確認
$ cd /home/vagrant/code/laravel/ $ php artisan --version Laravel Framework 5.5.13
引き続き、Composerやデータベース接続などを試す 以下のページも参考になりそう Laravel : Homesteadで開発環境を構築 | DN-Web64 http://www.dn-web64.com/archives/web/homestead/ 実際の作業環境追加は「Homesteadにサブドメインと作業ディレクトリを追加」の項目を参照 ■環境構築のトラブル事例1 vagrant up 後「Warning: Authentication failute. Retrying...」が出続けてタイムアウトエラーが出る (vagrant 自体は起動するがなんか気持ち悪い) [原因] ssh の秘密鍵とゲストOS で設定されている公開鍵が合っていない為発生。 [解決方法] http://kiraba.jp/vagrant-error-authentication-failure-retrying/ こちらのサイトの通りにやることで Warning は出ずに起動するようになった。 ■環境構築のトラブル事例2 vagrant が起動した後、サイトにつながらない (http://homestead.test/ が見れない) [原因] 一度不完全な状態で起動した(上記の Warning など)為、nginx の設定ファイルが正常に作成されていない為。 [解決方法] vagrant halt で一旦停止後、
vagrant up --provision
で起動する。 --provision を付与することで再度 Homestead.yaml に沿って設定ファイルを作成してくれる。 これで http://homestead.test/ にアクセスすると「No input file specified.」が表示されるようになった。 vagrant up --provision すれば鍵の不一致も解決するかも(?)。 試していないので断定はできないが、あとで見ると鍵がコピーされていた。 ■環境構築のトラブル事例3 複数のHomesteadを起動できない
C:\localhost\home\test\public_html\Homestead>vagrant up Bringing machine 'homestead-7' up with 'virtualbox' provider... ==> homestead-7: Box 'laravel/homestead' could not be found. Attempting to find and install... homestead-7: Box Provider: virtualbox homestead-7: Box Version: >= 4.0.0 ==> homestead-7: Loading metadata for box 'laravel/homestead' homestead-7: URL: https://atlas.hashicorp.com/laravel/homestead ==> homestead-7: Adding box 'laravel/homestead' (v4.0.0) for provider: virtualbox homestead-7: Downloading: https://vagrantcloud.com/laravel/boxes/homestead/versions/4.0.0/providers/virtualbox.box ==> homestead-7: Box download is resuming from prior download progress homestead-7: Progress: 100% (Rate: 101k/s, Estimated time remaining: --:--:--) ==> homestead-7: Successfully added box 'laravel/homestead' (v4.0.0) for 'virtualbox'! ==> homestead-7: Importing base box 'laravel/homestead'... ==> homestead-7: Matching MAC address for NAT networking... ==> homestead-7: Checking if box 'laravel/homestead' is up to date... A VirtualBox machine with the name 'homestead-7' already exists. Please use another name or delete the machine with the existing
「homestead-7」はすでに存在すると言われた Oracle VM VirtualBox(GUIツール)を立ち上げて、以前作成した「homestead-7」を削除して、再度「vagrant up」を実行した Homesteadはそのままでは複数起動できないらしい box名を変更するなどの対応が必要らしい 1つのboxで複数のプロジェクトを管理する方が無難かも 同一のboxだとPHPのバージョンを変更したりが難しいが、 Homesteadは「特定の案件に合わせた環境を構築する」というより「最新版Laravelの開発環境を構築する」ものなので、 以下の方法でディレクトリを分ける程度の方がいいかも Homesteadを使って複数のLaravelプロジェクトを作成する簡単な方法 - Qiita https://qiita.com/Yorinton/items/08ec8fefcbec71513399 複数のLaravel環境をHomestead上で動かす - Qiita https://qiita.com/miutex/items/ebc13fc78da4a19a3da4 以下のような方法も紹介されているが、Homesteadがバージョンアップした場合に対応できなるなる可能性がありそう また、「何故か一部だけ動作しない」になったときに解決が難しいかも Laravel5.2のHomesteadを複数たててみた - Qiita https://qiita.com/you-me/items/b886dd0fc8e5047c4bc6 Homesteadを複数作りたい場合にやること - めものようなもの http://fumikony.hatenablog.com/entry/2015/04/22/145639 現状以下が良さそうかも ・Homestead環境は一つで、サブドメインで分岐させる ・例えば C:\localhost\home\homestead にHomesteadを配置して /home/vagrant/code と同期して、 /home/vagrant/code/laravel/public で最初のLaravelの動作確認をして、 /home/vagrant/code/test1/public /home/vagrant/code/test2/public などにプロジェクトを配置して laravel.homestead.test test1.homestead.test test2.homestead.test などでアクセスできるようにする。これなら過去案件への影響も小さい ・データベースも分ける ■環境構築のトラブル事例4 AWSのAPIを呼び出すと、以下のようなエラーになる
sns signaturedoesnotmatch:Signature expired: 20190117T041039Z is now earlier than 20190117T041213Z (20190117T042713Z - 15 min.)
サーバ時間のズレが影響しているようなので設定する 時差を調整してもエラーのままだったことがあるが、WindowsとVagrant自体を再起動するとエラーにならなくなった Laravel Homesteadの日本時間設定をする - Qiita https://qiita.com/shalman/items/42fcd4506b1f523c5553 Aws::RDS::Errors::SignatureDoesNotMatch というエラーが出た際の対処 | cloudpack.media https://cloudpack.media/20686 ■環境構築のトラブル事例5 EC2でLaravelのインストールに失敗する エラーメッセージにあるように、メモリ不足が原因 スワップを作成するか、マシンスペックを上げる
$ composer create-project --prefer-dist "laravel/laravel=6.0.*" logisquare Installing laravel/laravel (v6.0.2) - Installing laravel/laravel (v6.0.2): Downloading (100%) Created project in logisquare > @php -r "file_exists('.env') || copy('.env.example', '.env');" Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 84 installs, 0 updates, 0 removals - Installing symfony/polyfill-ctype (v1.12.0): Downloading (100%) The following exception is caused by a lack of memory or swap, or not having swap configured Check https://getcomposer.org/doc/articles/troubleshooting.md#proc-open-fork-failed-errors for details PHP Warning: proc_open(): fork failed - Cannot allocate memory in phar:///usr/local/bin/composer/vendor/symfony/console/Application.php on line 952 Warning: proc_open(): fork failed - Cannot allocate memory in phar:///usr/local/bin/composer/vendor/symfony/console/Application.php on line 952 [ErrorException] proc_open(): fork failed - Cannot allocate memory create-project [-s|--stability STABILITY] [--prefer-source] [--prefer-dist] [--repository REPOSITORY] [--repository-url REPOSITORY-URL] [--dev] [--no-dev] [--no-custom-installers] [--no-scripts] [--no-progress] [--no-secure-http] [--keep-vcs] [--remove-vcs] [--no-install] [--ignore-platform-reqs] [--] [<package>] [<directory>] [<version>]

Advertisement