由淺入深laravel教程第1課:搭建homestead開發環境

本教程有免費配套視頻,如今欣賞免費配套視頻

laravel官方推薦的Homestead工做環境,安裝簡單,使用方便,推薦給使用laravel開發項目的程序猿。

本機操做系統:64位Win10php

一:前期工做:軟件準備(下載)

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,盒子下載地址html

說明1: Homestead不是一個軟件,是laravel基於php,c,ruby.,js開發的項目,其安裝方式有2 種:laravel

  • 1.是直接在github下載zip後解壓到合適的位置
  • 2.是git克隆

說明2: box安裝方式2種,git

  1. 是在線安裝(在國內此方法太慢,不適用)
  2. 是下載盒子後離線安裝(推薦)

二:homestead安裝和配置

說明1:homestead是laravel開發的一個項目,託管在 https://github.com/laravel/ho...,能夠安裝在任何位置,我喜歡安裝在D盤的根目錄下,能夠下載zip文件解壓也能夠直接git clone安裝

說明2: homestead的版本必須與下載的box版本同樣,當前homestead的版本與box的版本是4.0.0github

說明3: homestead不要放在有 「下劃線或空格" 的文件夾下面,虛擬機在加載共享文件夾時會忽略數據庫

1:安裝homestead

D:\03www2018>git clone https://github.com/laravel/homestead.gitjson

2:配置homestead

將homestead配置文件從D:\03www2018\homestead\resources\Homestead.yaml拷貝到D:\www2018\homestead,也可執行init.bat自動拷貝

D:\www2018\homestead>initwindows

編輯配置文件Homestead.yamlruby

---
ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

folders:
    - map: D:/03www2018
      to: /home/vagrant/abcde

sites:
    - map: homestead.test
      to: /home/vagrant/code/public
    - map: myblog.app
      to: /home/vagrant/abcde/study/myblog/public
    - map: tutorial.app
      to: /home/vagrant/abcde/study/tutorial/public 
databases:
    - homestead

variables:
    - key: 'APP_ENV'
      value: 'home'
    - key: 'APP_DEBUG'
      value: 'true'

3. 定義虛擬主機

編輯文件C:\Windows\System32\drivers\etc\hostsapp

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,內容以下
{
    "name": "laravel/homestead",
    "versions": [{
        "version": "4.0.0",
        "providers": [{
            "name": "virtualbox",
            "url": "04_homesteadbox400"
        }]
    }]
}
  • b: 將盒子安裝配置文件0000.json和盒子04_homesteadbox400,放在同一個文件夾下(文件夾名不用漢字),如放在E:/abc def/下(文件夾有空格也不要緊)
說明1:0000.json是安裝盒子時讀取的盒子安裝配置文件,名字任取,只要格式是json的就好了。
說明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盤空間出現不足時,會出現這個問題

方法一:
慎重:原來安裝的數據庫等會一併刪除了

D:\03www2018\homestead>vagrant global-status
id      name        provider  state    directory
-----------------------------------------------------------------------------
900a608  homestead-7 virtualbox running  D:/03_www2018/homestead
48f672c  homestead-7 virtualbox poweroff D:/03www2018/homestead
The above shows information about all known Vagrant environments
on this machine. This data is cached and may not be completely
up-to-date. To interact with any of the machines, you can go to
that directory and run Vagrant, or you can use the ID directly
with Vagrant commands from any directory. For example:
"vagrant destroy 1a2b3c4d"

先使用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_desktop

Enter your choice: 3
==> box: Adding box 'laravel/homestead' (v5.1.0) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/laravel/boxes/homestead/versions/
5.1.0/providers/virtualbox.box
    box: 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並不存在

相關文章
相關標籤/搜索