はじめに
先日の記事では、VirtualBoxにCentOS7のイメージを入れて、Laravelの開発環境を準備しました。
hrroct.hatenablog.com
しかし、Laravelの公式サイトでは「Homestead」という環境もあるよと教えてくれています。
laravel.com
readouble.com
これを今回は導入してみようと思います。
そもそもHomesteadとはなにか
Laravel Homestead is an official, pre-packaged Vagrant box that provides you a wonderful development environment without requiring you to install PHP, a web server, and any other server software on your local machine.
Laravel Homestead(入植農地、「ホームステード」)はパッケージを事前に済ませた、Laravel公式の"box"で、PHPやWebサーバ、その他のサーバソフトウェアをローカルマシンにインストールする必要なく、素晴らしい開発環境を準備できます。
公式サイトの文言を借りるとこんな感じ。
Vagrantで提供している環境のひとつですね。
いざ導入
仮想化ソフトのインストール(必要なら)
Before launching your Homestead environment, you must install VirtualBox, VMWare, Parallels or Hyper-V as well as Vagrant.
Homestead環境を起動する前にVagrantと共に、VirtualBoxか、VMWare、Parallels、Hyper-Vをインストールする必要があります。
Windows 10 Pro だとHyper-Vが使えるのですが、私のPCはHomeなので、VirtualBoxを使いました。
どのソフトを利用しても、Vagrantがラップしてくれる関係で、あまり意識することはないと思います。
ここのインストールについては省略します。
www.vagrantup.com
私の場合は、Windowsの64bit版をインストールしました。
msiファイルが229MBありました。
インストールするにあたって、基本的には「Next」で進めていくだけなので、ここも省略します。
Homestead Vagrant Boxのインストール
ここからはコマンドでの操作になります。
Powershellでコマンド実行が可能ですが、私はいつも使っているCygwinで実行しました。
実行コマンドに差異はないはず。
公式のドキュメントにある通り、コマンドを実行していきます。
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
4) vmware_desktop
Enter your choice: 3
プロバイダーと書いてありますが、ここで利用する仮想化ソフトを選択します。
私の場合はVirtualBoxなので、「3」です。
Enter your choice: 3
==> box: Adding box 'laravel/homestead' (v7.1.0) for provider: virtualbox
box: Downloading: https://vagrantcloud.com/laravel/boxes/homestead/versions/7.1.0/providers/virtualbox.box
box: Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com
box: Progress: 100% (Rate: 2568k/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'laravel/homestead' (v7.1.0) for 'virtualbox'!
「Successfully」とのことで無事成功のようです。
Homesteadのインストール
いよいよHomesteadのインストールです。
homeディレクトリにHomesteadディレクトリを作成することになります。
最初に言い忘れていましたが、Gitが必要になります。
$ git clone https://github.com/laravel/homestead.git ~/Homestead
Cloning into '/home/${USER}/Homestead'...
remote: Enumerating objects: 3314, done.
remote: Total 3314 (delta 0), reused 0 (delta 0), pack-reused 3314
Receiving objects: 100% (3314/3314), 703.94 KiB | 1.02 MiB/s, done.
Resolving deltas: 100% (2004/2004), done.
masterブランチは安定しているわけではないので、特定のブランチをcheckoutすることを推奨されています。
GitHubのサイトを見に行きましょう。
github.com
私の場合は最新の「v8.2.0」を指定しました。
$ cd Homestead/
~/Homestead$
~/Homestead$ git checkout -b v8.2.0
次に、Homestead.yamlという設定ファイルを生成するために、下記コマンドを実行します。
Mac/Linux と Windows とで違うところですね。
// Mac / Linux...
bash init.sh
// Windows...
init.bat
私はLinux組なので、init.shを実行。
$ bash init.sh
Homestead initialized!
Homesteadの設定
設定ファイルの初期値は下記のとおりです。
$ cat Homestead.yaml
---
ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub
keys:
- ~/.ssh/id_rsa
folders:
- map: ~/code
to: /home/vagrant/code
sites:
- map: homestead.test
to: /home/vagrant/code/public
databases:
- homestead
# ports:
# - send: 50000
# to: 5000
# - send: 7777
# to: 777
# protocol: udp
# blackfire:
# - id: foo
# token: bar
# client-id: foo
# client-token: bar
# zray:
# If you've already freely registered Z-Ray, you can place the token here.
# - email: foo@bar.com
# token: foo
# Don't forget to ensure that you have 'zray: "true"' for your site.
慣れてきたらいろいろと変更が必要になると思いますが、まず動かしてみることを考えるのであれば、一旦このままでもいいと思います。
ただし、いくつかこの通りの設定になっているかどうかを確認しないといけませんが。
例えば。
provider: virtualbox
利用する仮想化ソフトは合っているか。
authorize: ~/.ssh/id_rsa.pub
keys:
- ~/.ssh/id_rsa
sshの公開鍵、秘密鍵の場所は合っているか。
sites:
- map: homestead.test
to: /home/vagrant/code/public
http://homestead.test で仮想環境にアクセスするようにしているので、必要に応じてhostsファイルを書き換える必要があります。
Windowsの場合は"C:\Windows\System32\drivers\etc\hosts"にありますので、メモ帳を「管理者権限で実行」で実行した上で、ファイルを開いて下記を追加してください。
192.168.10.10 homestead.test
~/Homestead$ vagrant up
これで実行できます。あとはブラウザからアクセスしてみたいのですが…
http:// homestead.test
http://192.168.10.10
↓
No input file specified.
と表示されるのではないでしょうか。
これは、Homestead.yaml に設定したドキュメントルートに、ファイルが存在しないために発生しています。
なので、例えば
# 仮想環境に接続
$ vagrant ssh
vagrant@homestead:~$ mkdir -p /home/vagrant/code/public/
vagrant@homestead:~$ vim /home/vagrant/code/public/index.php
<?php
echo "Hello, Homestead!";
とディレクトリとファイルを作成してあげれば、
Hello, Homestead!
と、きちんと意図した動作をすることが確認できると思います。
コマンドの定義
どこからでも vagrant up コマンドを実行できるように、bash関数を作成しておくと便利です。
これもマニュアルにあることですが。
$ vim ~/.bash_profile
function homestead() {
( cd ~/Homestead && vagrant $* )
}
Windowsの場合は、Homestead.bat ファイルを下記の中身で作成して、PATHにファイルの場所を指定すればOKとのこと。
@echo off
set cwd=%cd%
set homesteadVagrant=C:\Homestead
cd /d %homesteadVagrant% && vagrant %*
cd /d %cwd%
set cwd=
set homesteadVagrant=
こうすれば今後、例えば"homestead ssh"と打てば、どこのディレクトリにいても"vagrant ssh"が実行可能になります。
その他
開発に必要なファイルの場所やコマンドなど。
起動した仮想環境にsshで接続
先に登場させましたが、
vagrant ssh
で接続が可能です。
$ vagrant ssh
Welcome to Ubuntu 18.04.1 LTS (GNU/Linux 4.15.0-45-generic x86_64)
_ _ _
| | | | | |
| |__ ___ _ __ ___ ___ ___| |_ ___ __ _ __| |
| '_ \ / _ \| '_ ` _ \ / _ \/ __| __/ _ \/ _` |/ _` |
| | | | (_) | | | | | | __/\__ \ || __/ (_| | (_| |
|_| |_|\___/|_| |_| |_|\___||___/\__\___|\__,_|\__,_|
* Homestead 8.0.0 released! PHP 7.3 is now the default!
* Settler v7.0.0 released! Make sure you update
* Need PHP 5.6 or 7.0? Homestead 7.x Settler 6.4.0
0 packages can be updated.
0 updates are security updates.
Last login: Sun Mar 10 13:48:36 2019 from 10.0.2.2
vagrant@homestead:~$
user, group
vagrant@homestead:~$ id
uid=1000(vagrant) gid=1000(vagrant) groups=1000(vagrant),4(adm),24(cdrom),27(sudo),30(dip),33(www-data),46(plugdev),108(lxd),113(lpadmin),114(sambashare)
nginx
vagrant@homestead:~$ ls -l /var/log/nginx/
total 4
-rw-r----- 1 www-data adm 0 Feb 4 23:16 access.log
-rw-r----- 1 www-data adm 0 Feb 4 23:16 error.log
-rw-r--r-- 1 root root 3586 Mar 10 13:11 homestead.test-error.log
上記の
No input file specified.
の件も、ここのログを見ると書いてあります。
vagrant@homestead:~$ mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.25-0ubuntu0.18.04.2 (Ubuntu)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql> SELECT user, host FROM mysql.user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| homestead | % |
| homestead | 0.0.0.0 |
| root | 0.0.0.0 |
| debian-sys-maint | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
7 rows in set (0.00 sec)
まだあると思うので、別途追記します。
最後に
まだ本当に下準備だけなので、これからLaravelプロジェクトを載せたりする必要があります。
とはいえ、上記の設定だけですでにここまでの準備が済むのはとっても便利です。
今後、積極的に使っていきたいと思います。
以上でした!