LoginSignup
11
11

More than 5 years have passed since last update.

Laravel Homestead のインストール

Posted at

参考ページ

前置き

Laravelでwebアプリ開発を始めるにあたって、Homesteadで環境構築を行った手順をここにまとめておきます。

開発環境

  • マシン:MacBook Pro 2016
  • OS:macOS HighSierra バージョン 10.13.4

Homestead Vagrant Box のインストール

vagrant のバージョンチェック

$ vagrant --version

vagrantにlaravel/homesteadを追加

$ vagrant box add 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) virtualbox
3) vmware_desktop

どのプロバイダーにするか聞かれたので、2)virtualbox を選択します。
これらのプロバイダーの違いについては、よくわかりません。すいません。。。

Enter your choice: 2
==> box: Adding box 'laravel/homestead' (v6.0.0) for provider: virtualbox
The box you're attempting to add already exists. Remove it before
adding it again or add it with the `--force` flag.

Name: laravel/homestead
Provider: virtualbox
Version: 6.0.0

どうやらすでに、インストールした形跡があったようです。
でも、強制的に--forceオプションを指定して、もう一度インストールしてみます.

$vagrant box add --force laravel/homestead

どうやらダウンロードが始まってインストールが行われいるようです。
(10分近く待ちます。)

==> box: Adding box 'laravel/homestead' (v6.0.0) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/laravel/boxes/homestead/versions/6.0.0/providers/virtualbox.box
==> box: Successfully added box 'laravel/homestead' (v6.0.0) for 'virtualbox'!

Homestadのインストール

githubからリポジトリをhomeディレクトリにクローンします。

 $git clone https://github.com/laravel/homestead.git ~/Homestead

You should check out a tagged version of Homestead since the master branch may not always be stable. You can find the latest stable version on the GitHub Release Page:

Master branchではなく, タグがつけらている安定版のbranchを使用するように推奨されています。

2018/06/01 時点ではv7.6.2が最新版のようです。

Homestead $git checkout v7.6.2
Note: checking out 'v7.6.2'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 848a4f6... 💎 🔖 Tagging v7.6.2
Homestead $git branch
* (HEAD detached at v7.6.2)
  master

Homestead.yamlを生成するために、 init.shを実行します。

Homestead $bash init.sh
Homestead initialized!

Homestead.yamlが生成されました。

Homestead $tree -L 1
.
├── CHANGELOG.md
├── Homestead.yaml
├── LICENSE.txt
├── Vagrantfile
├── after.sh
├── aliases
├── bin
├── composer.json
├── composer.lock
├── init.bat
├── init.sh
├── phpunit.xml.dist
├── readme.md
├── resources
├── scripts
├── src
└── tests

Homestead の設定を編集

Homestead.yamlファイルを編集していきます。

Homestead $vim Homestead.yaml

folderプロパティを編集することで、Homestead環境とを共有したいフォルダを設定できます。

共有フォルダの設定

自動で、以下のフォルダが登録されていました。

folders:
    - map: ~/code
      to: /home/vagrant/code

必要に応じて、他のフォルダを追加することができるそうです。

サイト数が少ない場合は、このままで良いのですが、サイトが増えていった場合は、パフォーマンスに問題が発生するので、個々のプロジェクトを各々のVagrantフォルダーにマッピングした方が良いそうです。

folders:
    - map: ~/code/project1
      to: /home/vagrant/code/project1

    - map: ~/code/project2
      to: /home/vagrant/code/project2

Nginx Site の設定

デフォルトでは以下のsiteが登録されています。

sites:
    - map: homestead.test
      to: /home/vagrant/code/public

必要に応じて、さらにsiteを追加することができるそうです。

もしsiteプロパティを修正した場合、vagrant reload --provision によって、仮想マシン上のNginx設定をアップデートする必要があります。

Host ファイルの編集

hostsファイルを編集することで、HomesteadへのreqestをHomestead仮想マシンにリダイレクトしてくれるようになります。

Mac と Linux では、hostsは、通常/etc/hostsに存在します。

Homestead.yamlに記載のあったipアドレス"192.168.10.10"hostsに追加します。

Homestead.yaml
ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox
....
....

Homestead $sudo vim /etc/hosts

以下をhostsに追記します。

/etc/hosts.
192.168.10.10 homestead.test

vagrantの起動

Homesteadディレクトリ内でvagrantを起動します。

Homestead $vagrant up
Bringing machine 'homestead-7' up with 'virtualbox' provider...
==> homestead-7: Importing base box 'laravel/homestead'...
...

ちなみに、仮想マシンを削除するときは、以下のコマンドを叩きます。

Homestead $vagrant destroy --force

この状態で。http://homestead.testにブラウザからアクセスすると、

No input file specified.

と表示されます。

プロジェクトごとにHomesteadをインストール

プロジェクト用のディレクトリを適当なディレクトリに作成します。

work$mkdir homestead_test
work$cd homestead_test/
homestead_test $

プロジェクトにhomesteadをインストールするには、以下のコマンドを実行します。

17:37:55 homestead_test $composer require --dev laravel/homestead
Using version ^7.6 for laravel/homestead
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 6 installs, 0 updates, 0 removals
  - Installing symfony/polyfill-ctype (v1.8.0): Downloading (100%)         
  - Installing symfony/yaml (v4.1.0): Downloading (100%)         
  - Installing symfony/process (v4.1.0): Downloading (100%)         
  - Installing symfony/polyfill-mbstring (v1.8.0): Loading from cache
  - Installing symfony/console (v4.1.0): Downloading (100%)         
  - Installing laravel/homestead (v7.6.2): Downloading (100%)         
symfony/console suggests installing symfony/event-dispatcher ()
symfony/console suggests installing symfony/lock ()
symfony/console suggests installing psr/log-implementation (For using the console logger)
Writing lock file
Generating autoload files

すると、プロジェクトディレクトリ内に以下のフォルダー、ファイルが生成されます。

17:42:07 homestead_test $tree -L 2
.
├── composer.json
├── composer.lock
└── vendor
    ├── autoload.php
    ├── bin
    ├── composer
    ├── laravel
    └── symfony

5 directories, 3 files

Homesteadがインストールされたなら、makeコマンドで、vagrantfileHomestead.yamlファイルをプロジェクトルートに生成します。

homestead_test $php vendor/bin/homestead make
Homestead Installed!
homestead_test $tree -L 1
.
├── Homestead.yaml
├── Vagrantfile
├── after.sh
├── aliases
├── composer.json
├── composer.lock
└── vendor

新たに、Homestead.yaml, Vagrantfile, after.sh, aliasesというファイルが生成されています。

次に、 vagrant upを実行します。

homestead_test $vagrant up
Bringing machine 'homestead-test' up with 'virtualbox' provider...
==> homestead-test: Importing base box 'laravel/homestead'...

通常の使用のために

Homestead にシステムのどこからでもアクセスできるようにするために、
以下のコードを.bash_profileに追加しておきます。

function homestead() {
    ( cd ~/Homestead && vagrant $* )
}

~/Homesteadのパスは実際にHomesteadをインストールしたディレクトリに設定します。

これで、以下のコマンドでHomesteadを起動できるようになります。
bash
23:49:39 homestead_test $homestead up
Bringing machine 'homestead-7' up with 'virtualbox' provider...
==> homestead-7: Importing base box 'laravel/homestead'...

11
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11