わすれっぽいきみえ

みらいのじぶんにやさしくしてやる

3日目: Laravel Homestead導入

昨日の続き。

ドキュメントは以下。

laravel.com

ちなみに昨日の記事で書いたように php artisan serve でもサーバは立ち上げられるので、Homesteadは必須ではない。以下の記事はとりあえずHomesteadどうやって立ち上げるのかを試してみたという内容になることをことわっておく。

一旦 brew cask で入れていたvagrantとVirtual Boxをお掃除する。

brew cask list
Warning: The default Caskroom location has moved to /usr/local/Caskroom.

Please migrate your Casks to the new location and delete /opt/homebrew-cask/Caskroom,
or if you would like to keep your Caskroom at /opt/homebrew-cask/Caskroom, add the
following to your HOMEBREW_CASK_OPTS:

  --caskroom=/opt/homebrew-cask/Caskroom

For more details on each of those options, see https://github.com/caskroom/homebrew-cask/issues/21913.
alfred                          evernote                        iterm2                          sublime-text                    vagrant                         xquartz
dropbox                         google-chrome                   skitch                          unity-web-player                virtualbox43691406 (!)

brew cask remove virtualbox43691406
brew cask remove vagrant

vagrantだけ Error: vagrant is not installed というエラーが出たので、以下を参考に手で消した。

qiita.com

あとはそれぞれインストールし直して

にした。

この状態で

vagrant box add laravel/homestead
==> box: Loading metadata for box 'laravel/homestead'
    box: URL: https://atlas.hashicorp.com/laravel/homestead
==> box: Adding box 'laravel/homestead' (v0.5.0) for provider: virtualbox
    box: Downloading: https://atlas.hashicorp.com/laravel/boxes/homestead/versions/0.5.0/providers/virtualbox.box
==> box: Successfully added box 'laravel/homestead' (v0.5.0) for 'virtualbox'!

今回はかまってちゃんのためだけに環境を用意したいので Per Project Installation を利用する。

composer require laravel/homestead --dev

これで php vendor/bin/homestead make が実行できるようになるのでおもむろに叩くと Vagrantfile が自動生成される。

require 'json'
require 'yaml'

VAGRANTFILE_API_VERSION ||= "2"
confDir = $confDir ||= File.expand_path("vendor/laravel/homestead", File.dirname(__FILE__))

homesteadYamlPath = "Homestead.yaml"
homesteadJsonPath = "Homestead.json"
afterScriptPath = "after.sh"
aliasesPath = "aliases"

require File.expand_path(confDir + '/scripts/homestead.rb')

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    if File.exists? aliasesPath then
        config.vm.provision "file", source: aliasesPath, destination: "~/.bash_aliases"
    end

    if File.exists? homesteadYamlPath then
        Homestead.configure(config, YAML::load(File.read(homesteadYamlPath)))
    elsif File.exists? homesteadJsonPath then
        Homestead.configure(config, JSON.parse(File.read(homesteadJsonPath)))
    end

    if File.exists? afterScriptPath then
        config.vm.provision "shell", path: afterScriptPath
    end
end

php vendor/bin/homestead make の実行時点でHomestead.yamlファイルは生成されてるので、一旦そのまま使うと良い。
ただしHomestead.yamlはデフォルトで設定されている公開鍵/秘密鍵が id_rsa.pub / id_rsa になってしまっているので、適宜 ~/.vagrant.d/insecure_private_key に置き換えてから vagrant up を実行してほしい。

vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'laravel/homestead' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 80 (guest) => 8000 (host) (adapter 1)
    default: 443 (guest) => 44300 (host) (adapter 1)
    default: 3306 (guest) => 33060 (host) (adapter 1)
    default: 5432 (guest) => 54320 (host) (adapter 1)
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Machine booted and ready!
GuestAdditions 5.0.28 running --- OK.
==> default: Checking for guest additions in VM...
==> default: Setting hostname...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => /Users/kimikimi714/Documents/open-kamattechan-php
    default: /home/vagrant/open-kamattechan-php => /Users/kimikimi714/Documents/open-kamattechan-php
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

vagrant ssh
Welcome to Ubuntu 16.04 LTS (GNU/Linux 4.4.0-22-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
Last login: Sun Nov 20 09:16:30 2016 from 10.0.2.2
vagrant@open-kamattechan-php:~$ ll
total 4260
drwxr-xr-x 6 vagrant vagrant    4096 Nov 20 09:16 ./
drwxr-xr-x 3 root    root       4096 May 23 20:35 ../
-rw------- 1 vagrant vagrant       8 Nov 20 09:16 .bash_history
-rw-r--r-- 1 vagrant vagrant     220 May 23 20:35 .bash_logout
-rw-r--r-- 1 vagrant vagrant    3771 May 23 20:35 .bashrc
drwx------ 2 vagrant vagrant    4096 May 23 20:36 .cache/
drwxrwxr-x 4 vagrant vagrant    4096 Jun 27 16:54 .composer/
-rw-r--r-- 1 vagrant vagrant 4315136 Jun 27 16:55 .hhvm.hhbc
drwxr-xr-x 1 vagrant vagrant    1020 Nov 20 09:17 open-kamattechan-php/
-rw-r--r-- 1 vagrant vagrant     724 Nov 20 08:44 .profile
drwx------ 2 vagrant root       4096 Nov 20 08:44 .ssh/
-rw-r--r-- 1 vagrant vagrant       0 May 23 20:36 .sudo_as_admin_successful
-rw-r--r-- 1 vagrant vagrant       6 May 23 20:36 .vbox_version
-rw-r--r-- 1 root    root        182 May 23 20:38 .wget-hsts
vagrant@open-kamattechan-php:~$ pwd
/home/vagrant

デフォルトでただ立ち上げただけで http://192.168.10.10/ でデモ画面が見れるようになる。

今日はここまで。