您的位置:首页 > 编程语言 > PHP开发

laravel homestead vagrant box安装使用,问题,及相关命令

2017-07-31 10:29 916 查看
Vagrant is a tool that manages oracle virtual boxes
1.本地下载https://atlas.hashicorp.com/laravel/boxes/homestead/versions/0.2.6/providers/virtualbox.box下载该box作为base,

(https://atlas.hashicorp.com/boxes/search?utf8=%E2%9C%93&sort=&provider=&q=homestead可以用来查询有哪些box)
https://atlas.hashicorp.com/antoniofrignani/boxes/laravel-homestead-settler-32/versions/0.1.1/providers/virtualbox.box
2.本地添加 vagrant box add laravel/homestead (path to downloaded file)(base box above!)

3.
git clone https://github.com/laravel/homestead.git Homestead


4.运行init.sh将生成Homestead.yaml配置文件(实际上在Homestead目录将创建Vagrantfile,Homestead.yaml只是populate这个vagrantfile的脚本,方便配置(相当于执行vagrant init)

5.配置相关参数
http://laravel.com/docs/5.1/homestead
6.vagrant up即可使用了(直接调用virtualbox的api将基于base box的vagrantfile代表的客制化环境(shared folder,network,memroy,forwarded ports etc)运行起来)

 https://phphub.org/topics/2

Homestead 包括以下两个东西

一个 vagrant box 虚拟机, 里面软件什么都下载好了;

Github 代码库, 里面装载着 vagrant 的配置脚本, 用来自动化配置网络, 端口映射, 等一些开发时候用到的配置;

 

 windows下如果出现解压错误,那么可以使用一下方法解决

The executable 'bsdtar' Vagrant is trying to run was not
found in the %PATH% variable. This is an error. Please verify
this software is installed and on the path.


解决方案是从sourceforge.net下载一个bsdtar.exe basic_bsdtar.exe from here   http://sourceforge.net/projects/mingw/files/MinGW/Extension/bsdtar/ and rename it to bsdtar.exe and place it in C:\HashiCorp\Vagrant\embedded\gnuwin32\bin\

在win7中,当vagrant up时出现如下错误:

Unable to load R3 module D:\Program Files\Oracle\VirtualBox/VBoxDD.dll (VBoxDD):GetLastError=1790 (VERR_UNRESOLVED_ERROR)


则可能是因为

本目录中的以下三个文件是原始的未被破解的WIN7 64位系统主题文件:
themeservice.dll
themeui.dll
uxtheme.dll这三个文件被破解所导致的。解决方案是恢复它,参考:http://download.csdn.net/download/friendan/8199047

在windows下面,由于目录名称最长只有260字符,使用node gulp,grunt等模块时,往往出现Cannot mkdir: Protocol error错误

可能的解决方案参考:
http://stackoverflow.com/questions/30090691/configuring-homestead-to-work-for-windows-users/30094439#30094439 https://github.com/npm/npm/issues/3670 https://github.com/mitchellh/vagrant/pull/5495
关于UNC的知识点: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#short_vs._long_names

总结一下homestead vbox的UNC name配置解决方案:

1. comment out ~/.homestead/homestead.yaml中的文件夹共享:
# folders:
#    - map: D:\devenv\code
#      to: /home/vagrant/code
2. 在vagrantfile中将以下内容插入
# this should be inserted right before the "Homestead.configure()" line
config.vm.provider "virtualbox" do |v, override|
if Vagrant::Util::Platform.windows?
override.vm.synced_folder "D:\devenv\code", "/home/vagrant/code", disabled: true

v.customize ["sharedfolder", "add", :id, "--name", "code", "--hostpath", (("//?/D:/devenv/code").gsub("/","\\"))]

override.vm.provision :shell, inline: "mkdir -p /home/vagrant/code"
override.vm.provision :shell, inline: "mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` code /home/vagrant/code", run: "always"
else
override.vm.synced_folder "D:\devenv\code", "/home/vagrant/code"
end

v.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/code", "1"]
end
# this should be inserted right before the "Homestead.configure()" line


 除了上面的使用windows UNC来解决node/bower的长文件名问题外,对于node我们还有一种解决方案,就是使用https://github.com/arifsetiawan/flatten 这个package,来对node_modules目录进行扁平化,用法是在linux或者windows unc下彻底安装node modules后,执行下面的命令:

flatten-packages PROJECT_DIRECTORY


命令完成后,就将老的node_modules全部扁平化,彻底解决长文件名的问题!!!!同时该命令将删除老的垂直安排的module

homestead下node npm团队使用的实践探索

一般情况下,没有必要每个团队成员都完全安装node,npm以及对应的node_modules,可行的措施是将node_modules本身作为一个repo,同时将node.exe文件copy到项目的root folder(就是package.json的所在目录),这样团队成员就不用安装node以及对应的package了,又能正常做build!!

windows下除了文件名长度问题外,默认如果不是以administrator运行virtualbox的话还有无法创建symlink的问题,所以一般如果使用npm,bower自动化你的测试开发流程时,可能会由于无法在windows/virtualbox之间的共享的文件夹中创建symlink而导致一些烦人的问题,一个可行的workaround是把node module, bower module等放到非windows共享文件夹中, bower可以通过.bowerc指定bower 模块安装位置, package.json以及node_modules可以放到非win目录的一个wrapper目录中。当然,也可以通过将执行vagrant up命令的命令行以administrator身份启动也能够解决windows环境下在homestead virtualbox中不能创建symbol link的问题!!!也就是说在win10下是可以创建symlink的!

 

 在windows操作系统中,可能需要再bios中设置intel CPU的虚拟化功能使能。

vagrant up过程中,可能出现: default: Warning: Connection timeout. Retrying..., 这时可能的解决方案是:

在vagrantfile中增加以下内容,使能gui,检查是否是停留在了grub那里:

config.vm.provider "virtualbox" do |v|
v.gui = true
end
还有一种解决方案是修改grub信息,给一个timeout超时。  http://segmentfault.com/a/1190000000266564出现这种错误,另外一个可能是你的vt没有打开,在bios中设置虚拟化有可能解决问题。还有一种方案是选择32bit的系统,具体参考: http://laravel.io/forum/05-18-2014-trouble-getting-homestead-to-start-in-windows?page=1

用 VirtualBox 面板运行该系统,在 GRUB 界面回车登录系统,用户名:
vagrant
,密码:
vagrant
,编辑
/etc/grub.d/00_header
,找到:

if [ "\${recordfail}" = 1 ]; then
set timeout=-1

将 
-1
 改成 
10
 即可:

if [ "\${recordfail}" = 1 ]; then
set timeout=10

再运行 
$ update-grub
 更新 GRUB,关机后再用 
$ vagrant up
 启动就能正常启动了。

 

 

在windows下面如果你做好了配置,hosts文件也正确,但是一直出现no input files这个错误,可以尝试vagrant up --provision,如果确实已经up状态了,

可以只执行: vagrant provision就可以了。注意只要.yaml配置文件修改了,最好都执行vagrant reload --provision

参考: https://github.com/mitchellh/vagrant/issues/3869#issuecomment-48058728 (文件本身在网盘有备份)

当你发现虚拟机开发环境客制化满意后,你可以做的事情是将该虚拟机打包成另外一个box,以便提供给别人使用,提供一套统一的开发环境,确实不错哦:

vagrant package --output vagrant_example.box (在你的vagrantfile所在目录) 或者:
vagrant package xxpackage --output xxpackage.box --vagrantfile Vagrantfile

http://lovelace.blog.51cto.com/1028430/1423343
 browsersync的watch/reload任务监听3000端口,但是我们需要增加一个forwarded port在vagrantfile中,但是有时不工作,那么可以手工在virtualbox中添加端口转发就可以了。

 常用命令:

vagrant box list

vagrant init xxBox :创建vagrantfile,并且将box信息更新为xxBox

vagrant up   (在有vagrantfile的目录执行)

vagrant ssh-config  :在你的vagrantfile所在目录检查该vagrant box的ssh配置信息

$ vagrant ssh-config
Host default
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile "c:/Documents and Settings/zhang/devenvironment/Homestead/.vagrant/machines/default/v
irtualbox/private_key"
IdentitiesOnly yes
LogLevel FATAL


为工作方便创建一个ssh vagrant的alias,该命令等同于在homestead目录下执行vagrant ssh

alias vm="ssh vagrant@127.0.0.1 -p 2222 -i ~/devenvironment/Homestead/.vagrant/machines/default/virt
ualbox/private_key"

同时,一旦登陆到系统后执行一个.postlogin.sh(cd xxx/yyy)目录,这样就很方便了(source调用上述脚本)

在homestead下也可以非常方便地创建新的sites:注意: 如果出现serve command not found错误的话,可以手工 
source ~/.bashrc,也就是说上述serve命令就在.bashrc文件中定义的!!


vagrant@homestead:~$ serve trace.dev /home/vagrant/code/xdebug-trace-gui
dos2unix: converting file /vagrant/scripts/serve.sh to Unix format ...
nginx stop/waiting
nginx start/running, process 2646
php5-fpm stop/waiting
php5-fpm start/running, process 2662


 console下彩色显示

alias ls='ls --color=always'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐