由浅入深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种,

  1. 是在线安装(在国内此方法太慢,不适用)
  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: 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\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,内容如下
{
    "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 虚拟机
发现画面如下
《由浅入深laravel教程第1课:搭建homestead开发环境》
在这里启动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就可以正常启动虚拟机了

手动打开虚拟机,会发现下面错误提示

《由浅入深laravel教程第1课:搭建homestead开发环境》

打开homestead-7文件夹

《由浅入深laravel教程第1课:搭建homestead开发环境》

错误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并不存在

    原文作者:advance100
    原文地址: https://segmentfault.com/a/1190000012032294
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞