由浅入深laravel教程第1课:搭建homestead开发环境
本教程有免費配套視頻,現在欣賞免費配套視頻
laravel官方推薦的Homestead工作環境,安裝簡單,使用方便,推薦給使用laravel開發項目的程序猿。本機操作系統:64位Win10
一:前期工作:軟件準備(下載)
1:虛擬機,推薦免費的 VirtualBox 5.1,官網下載地址 | 百度網盤(密碼24es)
2: Vagrant,官網下載地址 | 百度網盤(密碼uak6)
3: git, 官網下載地址 | 百度網盤(密碼)
4: Homestead,代碼托管在 https://github.com/laravel/ho...
5: vagrant的一個盒子叫做 homestead box,官網中搜索Homestead,最新版本是4.0.0,盒子下載地址
說明1: Homestead不是一個軟件,是laravel基于php,c,ruby.,js開發的項目,其安裝方式有2 種:
- 1.是直接在github下載zip后解壓到合適的位置
- 2.是git克隆
說明2: box安裝方式2種,
二:homestead安裝和配置
說明1:homestead是laravel開發的一個項目,托管在https://github.com/laravel/ho...,可以安裝在任何位置,我喜歡安裝在D盤的根目錄下,可以下載zip文件解壓也可以直接git clone安裝說明2: homestead的版本必須與下載的box版本一樣,當前homestead的版本與box的版本是4.0.0
說明3: homestead不要放在有 “下劃線或空格" 的文件夾下面,虛擬機在加載共享文件夾時會忽略
1:安裝homestead
D:\03www2018>git clone https://github.com/laravel/homestead.git
2:配置homestead
將homestead配置文件從D:\03www2018\homestead\resources\Homestead.yaml拷貝到D:\www2018\homestead,也可執行init.bat自動拷貝D:\www2018\homestead>init
編輯配置文件Homestead.yaml
--- ip: "192.168.10.10" memory: 2048 cpus: 1 provider: virtualboxauthorize: ~/.ssh/id_rsa.pubkeys:- ~/.ssh/id_rsafolders:- map: D:/03www2018to: /home/vagrant/abcdesites:- map: homestead.testto: /home/vagrant/code/public- map: myblog.appto: /home/vagrant/abcde/study/myblog/public- map: tutorial.appto: /home/vagrant/abcde/study/tutorial/public databases:- homesteadvariables:- key: 'APP_ENV'value: 'home'- key: 'APP_DEBUG'value: 'true'3. 定義虛擬主機
編輯文件C:\Windows\System32\drivers\etc\hosts
192.168.10.10 www.myblog.app 192.168.10.10 bbs.myblog.app 192.168.10.10 cms.myblog.app 192.168.10.10 www.tutorial.app三:盒子安裝
1: 安裝 VirtualBox, vagrant, git(如果已安裝過,略過)
2: 安裝盒子
- a: 新建安裝配置文件0000.json,內容如下
- b: 將盒子安裝配置文件0000.json和盒子04_homesteadbox400,放在同一個文件夾下(文件夾名不用漢字),如放在E:/abc def/下(文件夾有空格也沒關系)
說明2:04_homesteadbox400是box文件,名字也可以改的
說明3: 配置文件和盒子文件最好放在同一個文件夾中,我放在D:\03www2018\homestead中,這兩個文件在安裝完后是可以刪除的,但最好保留,在出問題時用得著
說明4:盒子安裝在用戶目錄C:\Users\用戶名\.vagrant.d\boxes中,我的用戶名是 Administrator
說明5:顯示安裝了哪些 >vagrant box list,刪除某個box命令是 >vagrant box remove laravel/homestead
說明6:如果不知道哪里下載box,請先用 vagrant box add laravel/homestead,會顯示從哪里下載,因為在線下載實在太慢,可改用迅雷下載下來后改名為配置文件中指定的名字
D:\03www2018\homestead>vagrant box add 0000.json
四:vagrant 啟動
vagrant在啟動時要讀取它相應的配置文件 Vagrantfile,查找配置文件的順序官網這樣說: https://www.vagrantup.com/doc... ,首先在當前項目中找,當前項目中沒有就到父目錄中找。但laravel/homestead項目中已經定義好了Vagrantfile了,是用ruby寫的,這個文件會讀取script中的文件,所以建議,Vagrantfile在哪個目錄,該目錄就為vagrant工作目錄啟動身份,由于win10每個登錄用戶有自己的文件夾,如果以不同的身份通過vagrant啟動虛擬機,分別啟動 是自己的虛擬機,如我以daqi的名字登錄win10的,并以daqi的名字啟動虛擬機,實際上使用的是C:\Users\daqi.DESKTOP-GVP6V4L\VirtualBox VMs\homestead-7這個虛擬機
建議單獨打開虛擬機"D:\Program Files\Oracle\VirtualBox\VirtualBox.exe",通過vagrant管理時有什么變化,不要在virtualbox中直接刪除虛擬機
另外要注意的是,一定要保證C盤有足夠的空間
D:\www2018\homestead>vagrant up
第一次啟動,會生homestead-7這個主機對應的文件夾 C:\Users\Administrator\VirtualBox VMs\homestead-7,下面的box-disk001就是主機對應的文件有3.2G五: 常見錯誤
錯誤1:Check your Homestead.yaml file, the path to your private key does not exist.
說明你電腦沒有生成ssh密鑰,要生成密鑰,前提是你電腦安裝了git,安裝后在Git安裝目錄\usr\bin(我電腦上是D:\Program Files\Git\usr\bin)下有一個ssh-keygen.exe文件,執行這個文件就可以,
D:\Program Files\Git\usr\bin>ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/c/Users/qianjin.DESKTOP-GVP6V4L/.ssh/id_rsa): Created directory '/c/Users/qianjin.DESKTOP-GVP6V4L/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /c/Users/qianjin.DESKTOP-GVP6V4L/.ssh/id_rsa. Your public key has been saved in /c/Users/qianjin.DESKTOP-GVP6V4L/.ssh/id_rsa.pub. The key fingerprint is: SHA256:6rY8ncNu8m2V8RRPfYrezEqsIG+Lsi3H0EVfXMsBIDk qianjin@DESKTOP-GVP6V4L The key's randomart image is: +---[RSA 2048]----+ | ...o.oo .| | E. o..o+| | . o . .o=.| | . . o o .| | . .S o O | | . o.. * = | | o.= o + . | | o+*.O.o . | | .**O++. | +----[SHA256]-----+會在當前用戶目錄/c/Users/today.DESKTOP-GVP6V4L/.ssh/(如果管理員登錄是C:UsersAdministrator.ssh)下面生成2個文件id_rsa和id_rsa.pub
錯誤2:homestead-7這個虛擬機已經存在
A VirtualBox machine with the name 'homestead-7' already exists.Please use another name or delete the machine with the existing name, and try again.
這種情況一般出現在,原來使用非管理員賬號登錄windows系統后,啟動過vagrant,后又使用管理員登錄系統,當c盤空間出現不足時,會出現這個問題
方法一:
慎重:原來安裝的數據庫等會一并刪除了
先使用vagrant box remove xxx 刪除當前盒子,再vagrant global-status看有哪個虛擬盒子存在,使用vagrant destroy xx刪除
如果上面處理還是報錯,使用VBoxManage來刪除
D:\Program Files\Oracle\VirtualBox>VBoxManage list vms "homestead-7" {84452234-fb51-48a5-bedd-72fda9e99664} "virtualbox_default_1508157323631_85955_1509326648170_26616" {a12e995a-6193-4370-b06f-0f01b6a2fe08}D:\Program Files\Oracle\VirtualBox>VBoxManage unregistervm homestead-7 --delete 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%再使用vagrant box add 0000.json的方式重新安裝盒子
方法二:
打開Oracle VM VirtualBox 虛擬機
發現畫面如下
在這里啟動homestead-7是沒有用的,得用vagrant up啟動
最終解決辦法是:在Oracle VM VirtualBox中刪除virtualbox_default_1508157323631_85955_1517058798354_56690這個主機
并刪除C:UsersAdministratorVirtualBox VMsvirtualbox_default_1508157323631_85955_1517058798354_56690這個空文件夾
這種情況出現在c盤空間不足的情況下,因為不能在virtualbox_default_1508157323631_85955_1517271357511_21797下拷貝box-disk001.vmdk
錯誤3: 虛擬機不可訪問
Bringing machine 'homestead-7' up with 'virtualbox' provider...==> homestead-7: Checking if box 'laravel/homestead' is up to date...
Your VM has become "inaccessible." Unfortunately, this is a critical error with VirtualBox that Vagrant can not cleanly recover from. Please open VirtualBox and clear out your inaccessible virtual machines or find a way to fix them.
上面翻譯后的意思是: 你的虛擬機不能訪問了,這是一個嚴重的錯誤,并且vagrant也不能修復它了,請打開VirtualBox并且清除不可訪問的虛擬主機,或想辦法修復它
解決辦法: 虛擬機啟動時要讀homestead-7.vbox這個文件,因為異常退出這個文件生成了一個臨時文件,現將臨時文件homestead-7.vbox-tmp改名為homestead-7.vbox后將虛擬機刷新下,再在vagrant up就可以正常啟動虛擬機了
手動打開虛擬機,會發現下面錯誤提示打開homestead-7文件夾
錯誤4: 連接超時
D:\www\homestead>vagrant up ...省略 homestead-7: SSH username: vagrant homestead-7: SSH auth method: private key **卡在這里不動**Timed out while waiting for the machine to boot. This means that Vagrant was unable to > communicate with the guest machine within the configured ("config.vm.boot_timeout" value) time > period.
If you look above, you should be able to see the error(s) that Vagrant had when attempting to connect to the machine. These errors are usually good hints as to what may be wrong.
If you're using a custom box, make sure that networking is properly working and you're able to connect to the machine. It is a common problem that networking isn't setup properly in these boxes. Verify that authentication configurations are also setup properly,as well.
If the box appears to be booting properly, you may want to increase the timeout ("config.vm.boot_timeout") value.
猜測:可能是homestead項目與盒子的版本不一致
1. 查看當前盒子的狀態
D:\www\homestead>vagrant status
Current machine states:homestead-7 running (virtualbox)
The VM is running. To stop this VM, you can run vagrant halt to
shut it down forcefully, or you can run vagrant suspend to simply
suspend the virtual machine. In either case, to restart it again,
simply run vagrant up.
發現盒子是正常啟動
2. 查看當前所有盒子列表
D:\www\homestead>vagrant box list
3. 刪除當前盒子,重新安裝
D:\www\homestead>vagrant halt 可以打開Oracle VM VitualBox關閉了
D:\www\homestead>vagrant destroy,看下3個文件夾C:\Users\Administrator\.vagrant.d|.VirtualBox|VirtualBox VMs有什么變化,VMs下面的homestead-7這個文件夾刪除掉了,
D:\www\homestead>vagrant list 看下盒子
D:\www\homestead>vagrant box remove laravel/homestead 看下盒子
4. 重新安裝盒子
D:\www\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) hyperv 2) parallels 3) virtualbox 4) vmware_desktopEnter your choice: 3 ==> box: Adding box 'laravel/homestead' (v5.1.0) for provider: virtualboxbox: Downloading: https://vagrantcloud.com/laravel/boxes/homestead/versions/ 5.1.0/providers/virtualbox.boxbox: Progress: 0% (Rate: 47995/s, Estimated time remaining: 15:21:08)我今天網速不錯,直接在線安裝成功,如果網速慢,從上面的`https://vagrantcloud.com/lara...
5.1.0/providers/virtualbox.box`中直接用迅雷下載后離線安裝
問題依舊,在stackoverflow上找,發現這是個老問題,解決辦法是,夠簡單吧!!!
enable the Hardware virtualization in BIOS (VT-x),也就是要在BIOS中開啟虛擬化問題5:索引錯誤
PS E:wwwhomestead> vagrant up --provision
The machine index which stores all required information about
running Vagrant environments has become corrupt. This is usually
caused by external tampering of the Vagrant data folder.
Vagrant cannot manage any Vagrant environments if the index is
corrupt. Please attempt to manually correct it. If you are unable
to manually correct it, then remove the data file at the path below.
This will leave all existing Vagrant environments "orphaned" and
they'll have to be destroyed manually.
Path: C:/Users/daqi/.vagrant.d/data/machine-index/index
原因:暫不明
解決辦法: 打開軟件Oracle VM VirtualBox,當前狀態是關閉的,可以正常啟動,刪除或更名文件夾 C:Usersdaqi.vagrant.ddata后 vagrant up就可以了
正常情況下 C:/Users/daqi/.vagrant.d/data/machine-index/index并不存在
總結
以上是生活随笔為你收集整理的由浅入深laravel教程第1课:搭建homestead开发环境的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 聊一聊Serverless
- 下一篇: microsoft office vis