本日主題:開發環境建置(使用虛擬機的方式)
首先把以下的東東都下載下來並安裝完成(當然全部都可以跨平台的拉):
Vagrant安裝完成後,開啟命令提示字元(以Windows環境為例),輸入以下:
vagrant box add larael/homestead
之後就進入下載Homestead的virtual box檔案囉(如果你有裝多個VM軟體的話,會出現選單,就選virtualbox就對了),如下圖。
下載的咚咚會在哪裡呢,會在家目錄下的「.vagrant.d」資料夾,如果是Linux或Mac可以用「~」去找到家目錄位置,Windows會在「C:\Users\使用者帳號\」下喔。
下一步:設置開發資料夾
切換到你要的開發目錄下執行以下指令:
git clone https://github.com/laravel/homestead.git Homestead
如此會從github版控下載最新的laravel,並放到Homestead資料夾下,然後在CD到該資料夾下,如果要使用不同版本可以用以下指令。
git checkout v10.9.2
接下來在Homestead資料夾中有init.sh與init.bat執行檔案,當然windows就是跑init.bat囉。主要產生 Homstead.yaml 設定檔,編輯它。
- ip:虛擬機拿到的 ip 位址不需要改。
- memory、cpus:虛擬機資源不需要改。
- provider:因為用virtualbox,也不需要改。
- authorize、keys:需要改以下,通常放在家目錄中,沒有的話用以下指令增加。
ssh-keygen -t rsa -C "your_email@example.com"
- folders:map代表本機的位置,to代表掛載到虛擬機的位置。
- sites:DNS網址,比須修改hosts( C:\Windows\System32\drivers\etc\hosts )對應。
- databases: 不需要改。
然後輸入以指令啟動
vagrant up
其他指令說明如下:
vagrant up 啟動 Homestead
vagrant ssh 登入 Homestead虛擬機
vagrant suspend 暫停
vagrant halt 關機
vagrant reload --provision 重啟
Laravel必備工具composer是放在虛擬機中喔,要用vagrant ssh進去使用。
資料參考來源: https://ithelp.ithome.com.tw/articles/10210529
後話:解果跑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: E_FAIL
VBoxManage.exe: error: Failed to create the host-only adapter
VBoxManage.exe: error: Querying NetCfgInstanceId failed (0x00000002)
VBoxManage.exe: error: Details: code E_FAIL (0x80004005), component HostNetworkInterfaceWrap, interface IHostNetworkInterface
VBoxManage.exe: error: Context: "enum RTEXITCODE __cdecl handleCreate(struct HandlerArg *)" at line 94 of file VBoxManageHostonly.cpp
這是因為VirtualBox,Failed to create the host-only adapter,所以開啟「Oracle VM VirtualBox」程式後,在