0

0

Windows上使用Vagrant打造Laravel Homestead可协同跨平台开发环境

php中文网

php中文网

发布时间:2016-06-23 13:30:36

|

1274人浏览过

|

来源于php中文网

原创

前言

大家对VMware或者VirtualBox一定不会陌生,虚拟化的好处自然深入人心,而现在我们可以通过Vagrant搭建一套类似Laravel Homestead完整开发环境,这样极大的减少了架设开发环境的时间,同时还支持在Windows/Mac/Linux不同平台上分享定制包,统一团队之间的开发环境提高工作效率,而Docker的出现也让未来更值得期待。

用Vagrant为自己打造一个奇妙的跨平台开发环境

更新历史

2015年07月18日 - 初稿

阅读原文 - http://wsgzao.github.io/post/vagrant/

扩展阅读

Vagrant - https://www.vagrantup.com/
Laravel Homestead - http://laravel.com/docs/5.1/homestead
在windows下进行linux开发:利用Vagrant+virtualbox - http://blog.star7th.com/2015/06/1538.html
在 Mac/win7 下上使用 Vagrant 打造本地开发环境 - http://segmentfault.com/a/1190000002645737

环境准备

  1. Git(非必需)
  2. PHP(非必需)
  3. Laravel(非必需)
  4. Composer(非必需)
  5. Vagrant
  6. VirtualBox

如果大家有需要离线安装欢迎直接留言回复哈

安装git

1.下载GitHub for Windows

https://windows.github.com/

安装php

建议大家尽量安装当前最新版本的 PHP

1.下载PHP

http://windows.php.net/download/

2.解压目录

我的路径D:\php

3.添加环境变量

右键计算机->高级系统设置->环境变量->系统变量->PATH

C:\ProgramData\Oracle\Java\javapath;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\nodejs\;D:\php;C:\ProgramData\ComposerSetup\bin

4.设置php.ini

进入 PHP 安装目录(例如 D:\php)。找到 php.ini-development 文件并复制一份到当前目录,重命名为 php.ini,修改以下配置
去掉extension=php_mbstring.dll 前面的分号(888 行左右)
去掉extension=php_openssl.dll前面的分号(893 行左右)
去掉extension_dir = "ext"前面的分号(736 行左右)

5.使环境变量生效

重启explorer.exe

安装Laravel

1.下载Laravel

http://www.golaravel.com/download/

2.解压目录

我的路径D:\laravel-v5.1.4

3.启动Laravel

d:cd laravel-v5.1.4D:\laravel-v5.1.4>php artisan serveLaravel development server started on http://localhost:8000/

在浏览器中访问http://localhost:8000/

artisan 的 serve 命令还支持两个参数:

host 设置主机地址
port 设置 web server 监听的端口号
例如:php artisan serve --port=8888

安装Composer

1.下载Composer-Setup.exe

https://getcomposer.org/doc/00-intro.md#installation-windows

2.配置Composer

通义万相
通义万相

通义万相,一个不断进化的AI艺术创作大模型

下载
Loading composer repositories with package informationInstalling dependencies (including require-dev)SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed”

如果安装过程提示报错为缺少CA证书,下载cacert.pem到自定义路径
http://curl.haxx.se/docs/caextract.html

然后修改php.ini文件(1983行左右)

openssl.cafile=D:\php\verify\cacert.pem

3.测试Composer

composer -VComposer version 1.0-dev (d79427f1a7b15e8f4d46ce8124124a4d0c58ba1479c) 2015-07-04 11:22:58

安装Vagrant

1.下载Vagrant

https://www.vagrantup.com/downloads.html

2.离线下载虚拟镜像

https://github.com/tommy-muehle/puppet-vagrant-boxes/releases/download/1.0.0/centos-6.6-x86_64.box

上面给出的是centos-6.6镜像下载链接,要下载其他镜像请访问官网
http://www.vagrantbox.es/

安装VirtualBox

BIOS里面开启CPU硬件虚拟化支持VT(Virtualization Technology)

1.下载VirtualBox

https://www.virtualbox.org/wiki/Downloads

2.导入镜像

设置VirtualBox目录并拷贝镜像centos-6.6-x86_64.box
E:\VirtualBox\centos-6.6-x86_64.box

3.命令初始化Vagrant

#切换VirtualBox目录e:cd .\VirtualBox#输入命令初始化E:\VirtualBox> vagrant init centos6.6A `Vagrantfile` has been placed in this directory. You are nowready to `vagrant up` your first virtual environment! Please readthe comments in the Vagrantfile as well as documentation on`vagrantup.com` for more information on using Vagrant.#执行添加命令E:\VirtualBox> vagrant box add centos6.6 centos-6.6-x86_64.box==> box: Adding box 'centos6.6' (v0) for provider:    box: Downloading: file://E:/VirtualBox/centos-6.6-x86_64.box    box: Progress: 100% (Rate: 670M/s, Estimated time remaining: --:--:--)==> box: Successfully added box 'centos6.6' (v0) for 'virtualbox'!#检查是否导入成功E:\VirtualBox> vagrant box listcentos6.6 (virtualbox, 0)

Vagrant配置

详细配置文档可以参考官方手册 - https://docs.vagrantup.com/v2/

启动Vagrant

通过Shell进入目录E:\VirtualBox后执行命令

 vagrant up

顺利启动的完整过程如下所示

E:\VirtualBox>vagrant upBringing machine 'default' up with 'virtualbox' provider...==> default: Importing base box 'centos6.6'...==> default: Matching MAC address for NAT networking...==> default: Setting the name of the VM: VirtualBox_default_1437213832296_68434==> 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: Forwarding ports...    default: 22 => 2222 (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: Warning: Connection timeout. Retrying...    default:    default: Vagrant insecure key detected. Vagrant will automatically replace    default: this with a newly generated keypair for better security.    default:    default: Inserting generated public key within guest...    default: Removing insecure key from the guest if it's present... default: Key inserted! Disconnecting and reconnecting using new SSH key... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... default: The guest additions on this VM do not match the installed version of default: VirtualBox! In most cases this is fine, but in rare cases it can default: prevent things such as shared folders from working properly. If you see default: shared folder errors, please make sure the guest additions within the default: virtual machine match the version of VirtualBox you have installed on default: your host and reload your VM. default: default: Guest Additions Version: 4.3.28 default: VirtualBox Version: 5.0 ==> default: Mounting shared folders... default: /vagrant => E:/VirtualBox

虚拟机启动之后则可以通过 vagrant ssh 联入虚拟机进行进一步的环境配置,或者软件安装相关的工作,在Windows系统下,并不能直接通过vagrant ssh连到虚拟机,需要使用SecureCRT/Putty/Xshell等第三方工具进行连接。连接地址127.0.0.1,端口2222。登录的帐号root的密码为vagrant

E:\VirtualBox> vagrant upBringing machine 'default' up with 'virtualbox' provider...==> 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: 80 => 8080 (adapter 1)    default: 22 => 2222 (adapter 1)==> default: Booting VM...==> default: Waiting for machine to boot. This may take a few minutes...The guest machine entered an invalid state while waiting for itto boot. Valid states are 'starting, running'. The machine is in the'poweroff' state. Please verify everything is configuredproperly and try again.If the provider you're using has a GUI that comes with it, it is often helpful to open that and watch the machine, since the GUI often has more helpful error messages than Vagrant can retrieve. For example, if you're using VirtualBox, run `vagrant up` while theVirtualBox GUI is open.

如果有报上述错误,并且运行Virtualbox去安装系统时出错:Failed to open a session for the virtual machine,Unable to load R3 module C:\Program Files\Oracle\VirtualBox/VBoxDD.DLL (VBoxDD): GetLastError=1790 (VERR_UNRESOLVED_ERROR).,需要使用UniversalThemePatcher还原未破解的themeservice.dll themeui.dll uxtheme.dll文件

已经打包好的下载链接 - http://pan.baidu.com/s/1c0HGj2g

==> 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: Warning: Connection timeout. Retrying...    default: Warning: Connection timeout. Retrying...    default: Warning: Connection timeout. Retrying...

如果报default: Warning: Connection timeout. Retrying...,建议打编辑Vagrantfile打开VirtualBox图形化界面vb.gui = true进一步分析错误代码和原因。

导出box

通过Shell进入目录E:\VirtualBox后执行命令

vagrant packagevagrant package --output NAME --vagrantfile FILE#可选参数:--output NAME : (可选)设置通过NAME来指定输出的文件名--vagrantfile FILE:(可选)可以将Vagrantfile直接封进box中

完成后会在当前目录就会生成package.box,可以在家或者团队成员共享开发环境保持一致性

其它命令

vagrant up (启动虚拟机)
vagrant halt (关闭虚拟机??对应就是关机)
vagrant suspend (暂停虚拟机??只是暂停,虚拟机内存等信息将以状态文件的方式保存在本地,可以执行恢复操作后继续使用)
vagrant resume (恢复虚拟机 ?? 与前面的暂停相对应)
vagrant box remove centos6.6 (移除box,其中centos6.6是box名)
vagrant destroy (删除虚拟机,删除后在当前虚拟机所做进行的除开Vagrantfile中的配置都不会保留)

Laravel Homestead

细节部分可参考官方文档 - http://laravel.com/docs/5.1/homestead

1.下载安装包

vagrant box add laravel/homesteadE:\Homestead>vagrant box add laravel/homestead==> box: Loading metadata for box 'laravel/homestead'    box: URL: https://atlas.hashicorp.com/laravel/homesteadThis box can work with multiple providers! The providers that itcan work with are listed below. Please review the list and choosethe provider you will be working with.1) virtualbox2) vmware_desktopEnter your choice: 1==> box: Adding box 'laravel/homestead' (v0.2.7) for provider: virtualbox    box: Downloading: https://vagrantcloud.com/laravel/boxes/homestead/versions/0.2.7/providers/virtualbox.box    box: Progress: 0% (Rate: 9d/s, Estimated time remaining: 0:05:30)11))

由于国内网络环境问题建议离线下载后手动导入

#输入命令初始化E:\Homestead>vagrant init laravelA `Vagrantfile` has been placed in this directory. You are nowready to `vagrant up` your first virtual environment! Please readthe comments in the Vagrantfile as well as documentation on`vagrantup.com` for more information on using Vagrant.#执行添加命令E:\Homestead>vagrant box add laravel laravel.box==> box: Box file was not detected as metadata. Adding it directly...==> box: Adding box 'laravel' (v0) for provider:    box: Unpacking necessary files from: file://E:/Homestead/laravel.box    box: Progress: 100% (Rate: 141M/s, Estimated time remaining: --:--:--)==> box: Successfully added box 'laravel' (v0) for 'virtualbox'!#检查是否导入成功E:\Homestead>vagrant box listcentos6.6 (virtualbox, 0)laravel   (virtualbox, 0)#启动Lavarel HomesteadE:\Homestead>vagrant upBringing machine 'default' up with 'virtualbox' provider...==> default: Importing base box 'laravel'...==> default: Matching MAC address for NAT networking...==> default: Setting the name of the VM: Homestead_default_1437217549272_56101==> default: Clearing any previously set network interfaces...==> default: Preparing network interfaces based on configuration...    default: Adapter 1: nat==> default: Forwarding ports...    default: 22 => 2222 (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: Warning: Connection timeout. Retrying...    default:    default: Vagrant insecure key detected. Vagrant will automatically replace    default: this with a newly generated keypair for better security.    default:    default: Inserting generated public key within guest...    default: Removing insecure key from the guest if it's present... default: Key inserted! Disconnecting and reconnecting using new SSH key... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... default: The guest additions on this VM do not match the installed version of default: VirtualBox! In most cases this is fine, but in rare cases it can default: prevent things such as shared folders from working properly. If you see default: shared folder errors, please make sure the guest additions within the default: virtual machine match the version of VirtualBox you have installed on default: your host and reload your VM. default: default: Guest Additions Version: 4.3.14 default: VirtualBox Version: 5.0 ==> default: Mounting shared folders... default: /vagrant => E:/Homestead

登录帐户vagrant/vagrant,开始全新的Laravel Homestead体验之旅吧。

版权声明:本文为博主原创文章,未经博主允许不得转载。

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
Golang 生态工具与框架:扩展开发能力
Golang 生态工具与框架:扩展开发能力

《Golang 生态工具与框架》系统梳理 Go 语言在实际工程中的主流工具链与框架选型思路,涵盖 Web 框架、RPC 通信、依赖管理、测试工具、代码生成与项目结构设计等内容。通过真实项目场景解析不同工具的适用边界与组合方式,帮助开发者构建高效、可维护的 Go 工程体系,并提升团队协作与交付效率。

1

2026.02.24

Golang 性能优化专题:提升应用效率
Golang 性能优化专题:提升应用效率

《Golang 性能优化专题》聚焦 Go 应用在高并发与大规模服务中的性能问题,从 profiling、内存分配、Goroutine 调度、GC 机制到 I/O 与锁竞争逐层分析。结合真实案例讲解定位瓶颈的方法与优化策略,帮助开发者建立系统化性能调优思维,在保证代码可维护性的同时显著提升服务吞吐与稳定性。

2

2026.02.24

Golang 面试题精选:高频问题与解答
Golang 面试题精选:高频问题与解答

Golang 面试题精选》系统整理企业常见 Go 技术面试问题,覆盖语言基础、并发模型、内存与调度机制、网络编程、工程实践与性能优化等核心知识点。每道题不仅给出答案,还拆解背后的设计原理与考察思路,帮助读者建立完整知识结构,在面试与实际开发中都能更从容应对复杂问题。

1

2026.02.24

Golang 运行与部署实战:从本地到云端
Golang 运行与部署实战:从本地到云端

《Golang 运行与部署实战》围绕 Go 应用从开发完成到稳定上线的完整流程展开,系统讲解编译构建、环境配置、日志与配置管理、容器化部署以及常见运维问题处理。结合真实项目场景,拆解自动化构建与持续部署思路,帮助开发者建立可靠的发布流程,提升服务稳定性与可维护性。

3

2026.02.24

Golang 疑难杂症解决指南:常见问题排查与优化
Golang 疑难杂症解决指南:常见问题排查与优化

《Golang 疑难杂症解决指南》聚焦开发过程中常见却棘手的问题,从并发模型、内存管理、性能瓶颈到工程化实践逐步拆解。通过真实案例与调试思路,帮助开发者定位问题根因,建立系统化排查方法。不只给出答案,更强调分析路径与工具使用,让你在复杂 Go 项目中具备持续解决问题的能力。

1

2026.02.24

Golang 入门学习路线:从零基础到上手开发
Golang 入门学习路线:从零基础到上手开发

Golang 入门路线涵盖从零到上手的核心路径:首先打牢基础语法与切片等底层机制;随后攻克 Go 的灵魂——接口设计与 Goroutine 并发模型;接着通过 Gin 框架与 GORM 深入 Web 开发实战;最后在微服务与云原生工具开发中进阶,旨在培养具备高性能并发处理能力的后端工程师。

0

2026.02.24

中国研究生招生信息网官方网站入口 研招网网页版在线入口
中国研究生招生信息网官方网站入口 研招网网页版在线入口

中国研究生招生信息网入口(https://yz.chsi.com.cn) 此网站是研究生报名入口的唯一官方网站

95

2026.02.24

苹果官网入口与在线访问指南_中国站点快速直达与iPhone查看方法
苹果官网入口与在线访问指南_中国站点快速直达与iPhone查看方法

本专题汇总苹果官网最新可用入口及中国站点访问方式,涵盖官网直达链接、iPhone官方页面查看方法与常见访问说明,帮助用户快速进入苹果官方网站,便捷了解产品信息与官方服务。

14

2026.02.24

Asianfanfics官网入口与访问指南_AFF官方平台最新登录地址
Asianfanfics官网入口与访问指南_AFF官方平台最新登录地址

本专题系统整理Asianfanfics(AFF)官方网站最新可用入口,涵盖官方平台最新直达地址、官网登录方式及中文访问指引,帮助用户快速、安全地进入AFF平台浏览与使用相关内容。

15

2026.02.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号