Hello, World!!

むずかしいことはかけません

Laravel 8.x + Homestead で仮想環境を作る

昨日* 環境構築手順

  • Homesteadは、Laravel が動作する開発環境を簡単に構築できるツールです。
  • VagrantとはVirtualBoxなどを利用した仮想環境を作る便利なソフトウェアです。
  • Laravel Homesteadは、PHP、Webサーバー、その他のサーバーソフトウェアをローカルマシンにインストールしなくても開発環境を構築することができる公式のVagrant Boxです。
  • Vagrant Boxとは、仮想マシンのテンプレートとなるファイルです。
参考サイト

【Laravel 5.5 or latest】Homestead で mac に Laravel 開発環境を構築 - Qiita

Laravel公式でもHomesteadを使用しての環境構築手順を公開しています。
Laravel Homestead - Laravel - The PHP Framework For Web Artisans

virtualbox, vagrantはインストール済みとして進めます。

ディレクトリ構造
/Users/user_name/
 ├ Homestead/
 └ code/  
      └ project01/

laravel/homestead boxをインストール

laravel/homesteadという名前のboxファイルがインストールされます。

[~]$ vagrant box add laravel/homestead
[~]$ vagrant box list                 
laravel/homestead (virtualbox, 11.4.0)

Homesteadをインストール

ユーザーディレクトリの直下にインストールします。

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

Homestead.yaml を作成します

[~]$ cd ~/Homestead
[~/Homestead]$ bash init.sh
Homestead initialized!

SSHキーの設定はやっているとみて飛ばします。

Homestead.yaml の設定

---
ip: "192.168.30.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: project01.test    <- 書き換えた所
      to: /home/vagrant/code/project01/public    <- 書き換えた所

databases:
    - project01    <- 書き換えた所

# 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.
foldersの設定

mac側と仮想環境側のどのフォルダを共有するか設定します。

  • map

mac 側の共有ディレクトリのことでデフォルトでは ~/code になっており、これはユーザー名直下の code ディレクトリを指しています。
mac側には/codeディレクトリはないので作成しておきましょう。

[~]$ mkdir code

ここにLaravelのソースコードを入れることにします。

  • to

/home/vagrant/code は仮想環境側の共有フォルダのディレクトリになります。

sitesの設定

ホスト名 project01.test でアクセスしたときに to に記載した仮想マシンディレクトリを参照します。
デフォルトでは /home/vagrant/code/public と設定されていますが、Laravel プロジェクト/puroject01のpublicを見て欲しいので
/home/vagrant/code/project01/public
に書き換えます。

hostsファイルの設定

ブラウザからアクセスできるよう設定します。

[~/Homestead]$ sudo vi /etc/hosts

以下を追記

192.168.30.10 project01.test

仮想マシン起動前のプラグインインストール

以下2つのプラグインをインストールします。
参考サイト

起動時に自動的にホスト(VirtualBox)のバージョンに合わせて、ゲスト(Guest Additions)のバージョンを更新してくれるものです。

作成した仮想マシンとホストPCの/etc/hostsを自動で書き換えてくれるプラグインです。

[~/Homestead]$ vagrant plugin update
[~/Homestead]$ vagrant plugin install vagrant-vbguest
[~/Homestead]$ vagrant plugin install vagrant-hostmanager

仮想マシンの起動

[~/Homestead]$ vagrant up

上手くいくと仮想環境に/codeディレクトリが作成されているはずです。

仮想マシンにログイン

[~/Homestead]$ vagrant ssh
Welcome to Ubuntu 20.04.3 LTS (GNU/Linux 5.4.0-88-generic x86_64)

 _                               _                 _
| |                             | |               | |
| |__   ___  _ __ ___   ___  ___| |_ ___  __ _  __| |
| '_ \ / _ \| '_ ` _ \ / _ \/ __| __/ _ \/ _` |/ _` |
| | | | (_) | | | | | |  __/\__ \ ||  __/ (_| | (_| |
|_| |_|\___/|_| |_| |_|\___||___/\__\___|\__,_|\__,_|

* Homestead v12.6.1 | Thanks for using Homestead
* Settler v11.4.0


Last login: Sun Oct 17 09:47:50 2021 from 10.0.2.2
vagrant@homestead:~$

ls で中を見ると/codeディレクトリがありました。

vagrant@homestead:~$ ls
code

仮想マシン上でComposerとLaravel プロジェクトをダウンロードする

vagrant@homestead:~/code$ composer create-project laravel/laravel project01

ローカル環境にもproject01ディレクトリが作成されていれば成功です。

/etc/hosts に記述した、http://project01.test/にアクセスしたらLaravelのwelcomeページが表示されているはずです。

MySQL にログイン

vagrant@homestead:~/code$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26-0ubuntu0.20.04.3 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> use project01
Database changed

無事MySQLにもログインできました。

マイグレーションファイルを流す

envファイルの設定

DBの設定を以下に変更します。

DB_CONNECTION=mysql
DB_HOST=192.168.30.10    <- 書き換えた所
DB_PORT=3306
DB_DATABASE=project01    <- 書き換えた所
DB_USERNAME=homestead    <- 書き換えた所
DB_PASSWORD=secret    <- 書き換えた所

Homestead.yamlのIP を 192.168.30.10 に変更した場合は、192.168.30.10 としてください。
またDB_USERNAME, DB_PASSWORDを上記のように設定しないと以下のようなエラーが出てしまいます。

SQLSTATE[HY000] [1045] Access denied for user 'root'@'homestead' (using password: YES) (SQL: select * from information_schema.tables where table_schema = project01 and table_name = migrations and table_type = 'BASE TABLE')
マイグレーション実行
vagrant@homestead:~/code/project01$ php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (28.45ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (30.07ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (53.51ms)
Migrating: 2019_12_14_000001_create_personal_access_tokens_table
Migrated:  2019_12_14_000001_create_personal_access_tokens_table (35.37ms)

テーブルが作成されました。

mysql> show tables;
+------------------------+
| Tables_in_project01    |
+------------------------+
| failed_jobs            |
| migrations             |
| password_resets        |
| personal_access_tokens |
| users                  |
+------------------------+
5 rows in set (0.00 sec)