Laravel Homestead 介紹

Laravel 為了讓PHP開發整體體驗都能完善,在local開發的環境設定也提供了相關解決方案

也就是這裡要介紹的 Homestead

Laravel Homestead 是官方所維護的 Vagrant box pre-package

讓你可以直接透過這個環境開發,而不必再獨立安裝PHP、Web server…等軟體

並且不必擔心忘記系統設定,因為 Vagrant boxes 是一次性設定安裝完畢,

如果在安裝流程有發現錯誤,隨時都可以重新安裝環境,並且只需要幾分鐘的時間就能完成

Homestead 環境

Homestead 支援 Windows, Mac, or Linux 系統

並且包括了下列應用程式:

Ubuntu 18.04
Git
PHP 7.2
PHP 7.1
PHP 7.0
PHP 5.6
Nginx
Apache (Optional)
MySQL
MariaDB (Optional)
Sqlite3
PostgreSQL
Composer
Node (With Yarn, Bower, Grunt, and Gulp)
Redis
Memcached
Beanstalkd
Mailhog
Elasticsearch (Optional)
ngrok
wp-cli
Zend Z-Ray
Go
Minio

安裝

這裏說明MAC的安裝流程

[安裝 Virtualbox]

前往 Virtualbox 官網下載 OS X hosts

[安裝 Vagrant]

前往 Vagrant 官網下載MacOS

安裝後,記得點擊安裝 vagrant.pkg

[安裝 Homestead Vagrant Box]

透過 vagrant 指令安裝 laravel/homestead 安裝包

vagrant box add laravel/homestead

檔案稍大,會需要等一段時間下載,詳細參考如下:

$ 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) parallels
2) virtualbox
3) vmware_desktop

Enter your choice: 2
==> box: Adding box 'laravel/homestead' (v6.1.0) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/laravel/boxes/homestead/versions/6.1.0/providers/virtualbox.box
==> box: Successfully added box 'laravel/homestead' (v6.1.0) for 'virtualbox'!

[安裝 Homestead]

從github直接下載 Homestead

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

前往目錄

cd ~/Homestead

執行 bash init.sh 來初始化 Homestead

// Mac / Linux...
bash init.sh

接著就會看到 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

# blackfire:

設定 host

sudo vim /etc/hosts

新增 homestead.test Host 設定

192.168.10.10  homestead.test

設定SSH Keygen

執行第一段命令,會有三段確定,可以直接按Enter即可

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
eval "$(ssh-agent -s)"
ssh-add -K ~/.ssh/id_rsa

啟動

vagrant up

如果發生 Check your Homestead.yaml file, the path to your private key does not exist.

請檢查上一段是否正確設定 SSH Keygen

如果發生 vagrant up There was an error while executing VBoxManage, a CLI used by Vagrant for controlling VirtualBox. The command and stderr is shown below. Command: [“hostonlyif”, “create”] Stderr: 0%… Progress state: NS_ERROR_FAILURE VBoxManage: error: Failed to create the host-only adapter VBoxManage: error: VBoxNetAdpCtl: Error while adding new interface: failed to open /dev/vboxnetctl: No such file or directory VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component HostNetworkInterfaceWrap, interface IHostNetworkInterface VBoxManage: error: Context: “RTEXITCODE handleCreate(HandlerArg *)” at line 94 of file VBoxManageHostonly.cpp

則是表示VirtualBox為安裝完成 可以試著前往 系統偏好設定/安全與隱私 查看是否有阻擋 “Orical American Inc.." 若有則勾選允許即可 可參考這篇

如果發生錯誤:A Vagrant environment or target machine is required to run this command. Run vagrant init to create a new Vagrant environment. Or, get an ID of a target machine from vagrant global-status to run this command on. A final option is to change to a directory with a Vagrantfile and to try again.

vagrant global-status

cd ~/Homestead/

bash init.sh

vagrant reload --provision

若這是第一次安裝,目前可以直接前往 http://homestead.test

但由於還未安裝 laravel 檔案,因此可能會顯示找不到檔案的訊息

No input file specified.

可以繼續查看後面透過SSH登入Homestead的部分,以及在虛擬環境下載laravel即可

重新載入設定

vagrant reload --provision

關閉虛擬機

vagrant halt

卸載

vagrant destroy --force

透過SSH登入Homestead

cd Homestead
vagrant ssh

查看資料夾

ll

如果沒有看到 code/ 資料夾,則可以新增

composer create-project --prefer-dist laravel/laravel code

mysql

mysql 預設帳號為:homestead 密碼:secret