话说作为laravel的开发者,你听说Homestead应该很久了,可是官方推荐的开发环境在国内为什么鲜有人用?我这几天试着安装了一下,发现官方的安装教程实在存在着太多的大坑,尤其让刚刚入道的开发者望而却步,在本文我试着手把手教大家填上这个坑,我在腾讯课堂上传了视频教程,欢迎报名学习。如果你觉得视频过于罗嗦,直接参看本教程即可。
第一步 安装必须的软件
1.安装vagrant,点此进入下载页,vagrant属于跨平台应用,我的系统是win10。关于vagrant教程,可以自己看一下,百度一下遍地是。不看也没关系,本文大概只用到其中的几个命令。
2.安装Virtualbox,当然你安装Vmware或parallels(OX系统)也可以,但后面下载的box要注意对应,虚拟机对应的版本不同。
第二步 导入box
vagrant box add laravel/homestead
第一个坑:原本只要如上的命令即可,但由于国内众所周知的网络原因,我们不得不考虑先下载好你需要的box再来添加。
首先在hashicorp中找到合适的版本,再在链接后加上”版本号/providers/虚拟机类型.box”即可获得下载链接.
如我们要下载最新的版本号为1.0.1的virtualbox版的box文件,链接即为:https://atlas.hashicorp.com/laravel/boxes/homestead/versions/1.0.1/providers/virtualbox.box
注意! laravel/homestead box项目地址已经不再是原来的
而已经变更成
下载地址
以下地址下载最新版本(目前为5.1.0)的Homestead Box(可以使用下载工具):
https://app.vagrantup.com/laravel/boxes/homestead/versions/5.1.0/providers/virtualbox.box
https://vagrantcloud.com/laravel/boxes/homestead/versions/5.1.0/providers/virtualbox.box
如果你需要下载不同版本(或最新版本),请将以上地址中versions/后的数字版本号替换成相应的版本号。
为此我还是提供了国内的链接(已更新到5.1.0),方便大家。
链接:http://pan.baidu.com/s/1dEJdHj7
密码:kzlc
不建议采用迅雷离线下载,据说会导致下载的文件损坏!
接下来我新建了一个文件夹名为homestead,然后我将下好的box重命名为homestead.box放入,然后在此文件夹内运行如下命令(这里是按照一些普通的教程来添加,这时候挖了一个坑,后面填上)。
vagrant box add laravel/homestead homestead.box
接着运行
vagrant box listvagrant box list
发现这个box已经添加进来就ok了。
第三步 下载官方homestead配置
可以进入laravel5.3的官方文档
按照文档的说明首先运行(必须本地已经安装了git)
git clone https://github.com/laravel/homestead.git Homestead
接着在已经克隆下来的文件夹内,会发现bash init.sh,init.bat两个文件,这两个分别是将该目录内的Homestead.yaml文件拷贝到你的系统“家”目录下。如果是在win下,又安装了git bash,则运行bash init.sh,init.bat均可。如果是在linux下,则运行bash init.sh。
接着我们就在家目录.homestead文件夹下找到了我们的这个配置文件
第四步 配置Homestead.yaml
设置 IP及Provider
Homestead.yaml文件中的provider键表示使用哪个 Vagrant 提供者:virtualbox、vmware_fushion或者vmware_workstation,你可以将其设置为自己喜欢的提供者:
ip: "192.168.10.10"
provider: virtualbox
配置共享文件夹
Homestead.yaml文件中的folders属性列出了所有主机和 Homestead 虚拟机共享的文件夹,一旦这些目录中的文件有了修改,将会在本地和 Homestead 虚拟机之间保持同步,如果有需要的话,你可以配置多个共享文件夹(一般一个就够了):
folders:
- map: D:/homestead/code #(这是我本地的文件夹)
to: /home/vagrant/Code
配置 Nginx 站点
对 Nginx 不熟?没问题,通过sites属性你可以方便地将“域名”映射到 Homestead 虚拟机的指定目录,Homestead.yaml中默认已经配置了一个示例站点。和共享文件夹一样,你可以配置多个站点:
sites:
- map: liang.app
to: /home/vagrant/Code/Laravel/public
Hosts文件
不要忘记把 Nginx 站点配置中的域名添加到本地机器上的hosts文件中,该文件会将对本地域名的请求重定向到 Homestead 虚拟机,在 Mac 或 Linux上,该文件位于/etc/hosts,在 Windows 上,位于C:\Windows\System32\drivers\etc\hosts,添加方式如下:
192.168.10.10 liang.app
确保 IP 地址和你的Homestead.yaml文件中列出的一致,一旦你将域名放置到hosts文件,就可以在浏览器中通过该域名访问站点了:
http://liang.app
第五步 启动vagrant
在 Homestead 目录下运行vagrant up命令,Vagrant 将会启动虚拟机并自动配置共享文件夹以及 Nginx 站点。官方文档对此描述的如此这般简单,其实这里遇到了第二个大坑
我们输入vagrant up看会发生什么?一堆的错误提示!!!!
我们根据提示,貌似是ssh的key没有生成,于是我们要运行如下命令,这个命令可以在git bash下生成,也可以在cmder等命令行工具完成,但在win下的cmd却无法执行。win 下可考虑powershell。
ssh-keygen
好了,我们欢喜的以为,我们可以顺利运行了。vagrant up。我擦居然提示这个box没有,需要安装。出现了第三个坑
再次出现该box不存在导致该坑的原因,我们看之前的vagrant box list,里面显示laravel/homestead (virtualbox, 0),而homestead要求Box Version: >= 1.0.0,这就不奇怪了。但我们明明下的是这个1.0.1版本啊。
vagrant box list我们有两种方式修复这个大坑
方式一
修改homestead/scripts/homestead.rb的这个文件,将其中的>= 1.0.0改为< 1.0.0即可满足要求。但我并不推荐这种方式,因为实质对满足要求进行了篡改。
方式二
我们在homestead下新建一个名为homestead.json的一个json配置文件
{
"name": "laravel/homestead",
"versions": [{
"version": "1.0.1",
"providers": [{
"name": "virtualbox",
"url": "file://homestead.box"
}]
}]
}
看懂了吗?接着我们运行这个命令
已经添box成功
vagrant box add homestead.json
真的不容易啊,添加成功了
此处因为vagrant升级到2.0版本后又有改变,我们再次进行更正
可以详见https://blog.csdn.net/lgyaxx/article/details/60870819
下载完成后,我们将下载的文件命名为homestead-5.1.0.box。接下来,我们cd到相应的文件夹(假设下载文件目录在Downloads文件夹下,运行以下命令:
eagle@EagleEye:~/Downloads$ vagrant box add laravel/homestead ./homestead-5.1.0.box
会开始安装我们下载的Homestead盒子。
完成后,你会发现自己的用户文件夹里多出了一个.vagrant.d的文件夹(C:\Users\MyUserName.vagrant.d)。
将文件夹中名为0的文件夹改名为我们下载的Homestead的版本号,图片还没有更新,我们应该改成5.1.0。
接下来,非常重要的一个步骤:
我们打开这个文件夹,找到以下路径:
C:\Users\MyUserName\.vagrant.d\boxes\laravel-VAGRANTSLASH-homestead
在该文件夹下新建一个叫metadata_url的空白文件(注意不要有任何文件名后缀,如果你的系统设置了自动隐藏后缀名,请将隐藏去掉,确认文件无任何后缀):
文件的内容里,添加以下链接:
https://app.vagrantup.com/laravel/boxes/homestead
注意,不要留任何空白字符,保存文件。如果不进行这一步会怎样?,如下图
出错提示完成之后,我们就可以开始配置Homestead了。
我们接着运行vagrant up试试
成功运行经过这样一番折腾终于成功运行了。
第六步 登录vagrant并安装laravel
这一步应该是我们的最后一步了,我们期待目标通过本地能够访问到你用vagrant搭建起来的laravel。我们通过vagrant ssh命令即可顺利登录我们的系统了。
由于设置上我们的vagrant虚拟机Code目录和我们的本地主机D:/homestead/Code是同一个目录,因此,我们先移步到该目录。
第四个坑出现了,我们一般是通过composer来进行安装,但出于GFW原因,我们不得不用中国镜像来安装。vagrant虚拟机中运行如下命令
composer config -g repo.packagist composer https://packagist.phpcomposer.com
接着我们通过composer来安装一个5.2版本
composer create-project laravel/laravel=5.2.* --prefer-dist
OK,访问liang.app,出现了Laravel 5的欢迎界面,大功告成。
补坑:对于部分用户,可能出现autoload或boostrap不存在,说明依赖安装不完整,可以在laravel目录下运行如下命令来解决。
composer update--no-scripts
由于直接git clone 官方在github上的东西可能造成与本box版本不一致,建议解决方案参考评论中beyond__devil所用的方法,将版本回滚。或者直接下最新的box或等我的box更新。
受各位启发,准备出一个Docker教程,于是乎发现了有意思的评论
版权声明:本文版权归作者所有,如要转载,请给钱,欢迎私信、评论
欢迎打赏、打赏、打赏
2017.03.27重大更新
1.homestead的box已升级到2.0
2.vagrant版本要求必须是1.9+
3.最最重要的是,homestead在github上的版本已更新到5.1,有一些文件发生了变动。
最主要的变动是当运行init.bat或init.sh时候,几个文件将不再复制到家目录的.homestead文件,而是直接被复制到homestead项目的根目录中(源在homestead项目文件的resources文件夹下)。原先家目录下的.homestead可以直接删掉了!
4.先删除原来box,可以用vagrant box remove laravel/homestead命令,destroy之前的虚拟机,接着重复之前的步骤,安装新的2.0box即可(记得修改添加时的版本号)。
腾讯课堂已经更新,已报名的同学可以观看补充的课程
网友评论
There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.
Command: ["import", "\\\\?\\C:\\Users\\Administrator\\.vagrant.d\\boxes\\laravel-VAGRANTSLASH-homestead\\6.1.0\\virtualbox\\box.ovf", "--vsys", "0", "--vmname", "ubuntu-18.04-amd64_1528981308408_40115", "--vsys", "0", "--unit", "9", "--disk", "C:\\Users\\Administrator\\VirtualBox VMs\\ubuntu-18.04-amd64_1528981308408_40115\\ubuntu-18.04-amd64-disk001.vmdk"]
Stderr: 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Interpreting \\?\C:\Users\Administrator\.vagrant.d\boxes\laravel-VAGRANTSLASH-homestead\6.1.0\virtualbox\box.ovf...
OK.
0%...
Progress state: VBOX_E_FILE_ERROR
VBoxManage.exe: error: Appliance import failed
VBoxManage.exe: error: Could not create the imported medium 'C:\Users\Administrator\VirtualBox VMs\ubuntu-18.04-amd64_1528981308408_40115\ubuntu-18.04-amd64-disk001.vmdk'.
VBoxManage.exe: error: VMDK: Compressed image is corrupted '\\?\C:\Users\Administrator\.vagrant.d\boxes\laravel-VAGRANTSLASH-homestead\6.1.0\virtualbox\ubuntu-18.04-amd64-disk001.vmdk' (VERR_ZIP_CORRUPTED)
VBoxManage.exe: error: Details: code VBOX_E_FILE_ERROR (0x80bb0004), component ApplianceWrap, interface IAppliance
VBoxManage.exe: error: Context: "enum RTEXITCODE __cdecl handleImportAppliance(struct HandlerArg *)" at line 886 of file VBoxManageAppliance.cpp
最后vagrant up 之后就变成这个样子了,麻烦帮看下是什么问题的,谢谢
homestead-7: sed: can't read /etc/php/5.6/fpm/pool.d/www.conf: No such file
or directory
homestead-7: sed: can't read /etc/php/7.0/fpm/pool.d/www.conf: No such file
or directory
homestead-7: sed: can't read /etc/php/7.2/fpm/pool.d/www.conf: No such file
or directory
The SSH command responded with a non-zero exit status. Vagrant
assumes that this means the command failed. The output for this command
should be in the log above. Please read the output to determine what
went wrong.
这步没有的话 就是个坑了
1.安装vagrant、virtualbox
vagrant下载地址:https://www.vagrantup.com/downloads.html
virtualbox下载地址:https://www.virtualbox.org/wiki/Downloads
2.导入box
vagrant box add laravel/homestead
开一个vpn下载1个小时。
vagrant box add laravel/homestead homestead.box
下载homestead.box到本地文件夹,从本地运行
vagrant box list查看
3.运行vagrant box list

4.用户目录下,下载homestead
git clone https://github.com/laravel/homestead.git Homestead
进入homestead目录
运行bash init.sh,创建Homestead.yaml文件
5.配置homestead.yaml

6.修改Hosts文件
vi /etc/hosts

7.生成ssh的key
运行ssh-keygen命令
8.运行vagrant up
9.在~/Code目录下安装lavavel
composer create-project laravel/laravel Laravel
10.访问yiien.com
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Adapter 2: bridged
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
default: /vagrant => D:/develop
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!
chown 900:900 /vagrant
Stdout from the command:
Stderr from the command:
stdin: is not a tty
chown: changing ownership of ‘/vagrant’: Not a directory
最后的报错是什么原因啊
homestead-7: Running: /var/folders/30/s05rkg6d0kvd_88lmld8p0300000gn/T/vagrant-shell20170121-30766-276xpb.sh
==> homestead-7: Running provisioner: shell...
homestead-7: Running: script: Creating Site: homestead.devil.com
==> homestead-7: Running provisioner: shell...
homestead-7: Running: script: Restarting Nginx
==> homestead-7: nginx stop/waiting
==> homestead-7: nginx start/running, process 1908
==> homestead-7: php7.1-fpm: unrecognized service
The SSH command responded with a non-zero exit status. Vagrant
assumes that this means the command failed. The output for this command
should be in the log above. Please read the output to determine what
went wrong.
我也安装是v1.0.1,我是今天git clone 的 laravel/homestead。最新的nginx的php-fpm使用的是:php7.1-fpm。我这边一直搞不定!你当时就是php7.1-fpm吗,一直报这个错!
“homestead-7: php7.1-fpm: unrecognized service” -- 找了各种资料,搞不定。另外,请教下,如何查看下载的homestead.box中预装的php、nginx等的版本
$ vagrant up
Bringing machine 'homestead-7' up with 'virtualbox' provider...
==> homestead-7: Checking if box 'laravel/homestead' is up to date...
==> homestead-7: Clearing any previously set forwarded ports...
==> homestead-7: Clearing any previously set network interfaces...
==> homestead-7: Preparing network interfaces based on configuration...
homestead-7: Adapter 1: nat
homestead-7: Adapter 2: hostonly
==> homestead-7: Forwarding ports...
homestead-7: 80 (guest) => 8000 (host) (adapter 1)
homestead-7: 443 (guest) => 44300 (host) (adapter 1)
homestead-7: 3306 (guest) => 33060 (host) (adapter 1)
homestead-7: 5432 (guest) => 54320 (host) (adapter 1)
homestead-7: 27017 (guest) => 27017 (host) (adapter 1)
homestead-7: 22 (guest) => 2222 (host) (adapter 1)
==> homestead-7: Running 'pre-boot' VM customizations...
==> homestead-7: Booting VM...
==> homestead-7: Waiting for machine to boot. This may take a few minutes...
homestead-7: SSH address: 127.0.0.1:2222
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-7: You are already using composer version 1.6.5 (stable channel).
愁,怎么搞