这是一个美妙的开发环境,不需要在你的本地机器中安装PHP、web服务以及其他任何服务软件,完全在虚拟机中运行,不用再担心会搞乱你的操作系统!如果出了什么乱子,你可以在几分钟之内完全重建你的开发环境。
整套环境中包含以下软件,可以帮助你开发出惊人的软件(未完全列出):
安装过程中会使用到:
PS:官网列出VirtualBox 6.x,VMWare,Parallels或Hyper-V以及Vagrant。这些软件,其中只要安装VirtualBox 6.x和vagrant两款软件就可以。其它VMWare ,Parallels和Hyper-V是起同样作用的。
第一步,下载VirtualBox 与 Vagrant 两款软件。
下载地址:VirtualBox https://www.virtualbox.org/wiki/Downloads
Vagrant https://www.vagrantup.com/downloads.html
PS:其中virtualbox要根据homestead版本下载对应的版本。具体的可以参照官网:https://laravel.com/docs/5.8/homestead#first-steps
以上两个软件下载之后都是简单的dmg安装包,安装完成之后进行下一步。
首先需要下载Vagrant box
使用终端运行:vagrant box add laravel/homestead
服务提供者选择:2
Last login: Wed Jun 13 09:08:45 on ttys000
fourn@mac:~$ 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) parallels
2) virtualbox
3) vmware_desktop Enter your choice
: 2
==> box: Adding box 'laravel/homestead' (v6.1.0) for provider:
virtualbox box: Downloading: https://vagrantcloud.com/laravel/boxes/homestead/versions/6.1.0/providers/virtualbox.box box: Progress: 0% (Rate: 0*/s, Estimated time remaining: --:--:--)^C
==> box: Waiting for cleanup before exiting...
有时可以因为版本不同无需选择类型。
因为国内政策原因,没有可能下载下来,只能复制下载地址,使用迅雷下载,但是使用迅雷下载需要vip会员才有可能下载下来。大家可以在网上寻找一日试用会员。
https://vagrantcloud.com/laravel/boxes/homestead/versions/8.0.1/providers/virtualbox.box
百度云上的下载地址:https://download.csdn.net/download/hjiaqing/11577320
接下来,使用本地方式添加box,在终端运行:vagrant box add laravel/homestead ~/Downloads/virtualbox.box
PS:~/Downloads/virtualbox.box是你下载文件的摆放位置
打开仿达,同时按下Command+Shift+G,前往下面这个位置:
到这个目录下,首先会看到一个名为0.0
的文件夹,需要将名字改为你下载的box的版本号,例如8.0.1
然后,你还需要新建一个名为metadata_url
的文件,里面放上如下内容:
https://atlas.hashicorp.com/laravel/boxes/homestead
PS:注意不要留空格
你需要使用从版本库克隆的方式安装Homestead,在此之前先运行一下git命令,如果是第一次使用git的话,mac会提示你并自动完成安装。
接着运行以下命令,将Homestead安装到你的“home”目录下
git clone https://github.com/laravel/homestead.git ~/Homestead
...中间过程省略
cd ~/Homestead git checkout v7.1.2
最新的版本总是比较稳定,但不一定适合所有的box,请按照自己在之前安装Vagrant和下载的virtualbox.box版本进行选择。
这里我们可以选择
cd ~/Homestead
git checkout release
然后我们使用命令生成一个Homestead.yaml
配置文件,你的所有项目站点及数据库将会在这里进行配置。
cd ~/Homestead
bash init.sh
使用文本编辑器打开上一个步骤中生成好的Homestead.yaml文件
这部分不需要对配置文件修改,关键是在启动之前,我们需要在主机上生成ssh key,使用如下命令:
ssh-keygen -t rsa
然后一路回车
在Homestead.yaml文件中所列出你想要和Homestead环境共享目录,其中所有的文件都会在你的本机和虚拟环境中保持同步,你可以尽可能多的配置。
folders:
- map: ~/code
to: /home/vagrant/code
~/code
是本地的目录,如果不存在可以新建,我们用于存放所有的项目文件/home/vagrant/code
是Homestead虚拟机中的目录
sites:
- map: wiki.local #维基
to: /home/vagrant/code/deepwiki
wiki.local
是将来浏览器中访问的地址/home/vagrant/code/deepwiki
是Homestead虚拟机中的该项目的入口目录
修改/etc/hosts
文件:
192.168.10.10 wiki.local
使浏览器在访问本地项目地址时能够定位到Homestead
databases:
- book
- wx_pro
- wx_dev
这将在初始化的时候创建三个数据库,当然你也可以在虚拟机运行过程中自行创建数据库。
连接地址:127.0.0.1
端口号:33060(MySql)
默认账号密码:homestead / secret
一旦你认为你的Homestead.yaml已经配置好了,那么就到~/Homestead
目录下运行:
vagrant up
这将会启动虚拟机并自动完成所有的文件及站点配置
首先在~/.bash_profile
文件(没有就创建)中添加:
function vag() {
( cd ~/Homestead && vagrant $* )
}
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
这样就可以很方便的运行vag up、vag ssh等命令。
然后可以在~/.bashrc
文件(没有就创建)中添加:
alias hosts='sudo vim /etc/hosts'
alias sites='vim ~/Homestead/Homestead.yaml'
这样就可以快速的进行Homestead以及Hosts配置,方便今后新增站点。
PS:在启动vagrant up 时遇到IP地址占用问题,此时要修改homestead.yaml中的IP
为192.168.10.0,vagrant 重启失败~~
The specified host network collides with a non-hostonly network!
This will cause your specified IP to be inaccessible. Please change
the IP or name of your host only network so that it no longer matches that of
a bridged or non-hostonly network.
A:更改homestead.yaml 的ip:193.168.10.10
PS:如果你是直接copy电脑中laravel文件可能会遇到下面这个问题。
php artisan route:clear
php artisan config:clear
php artisan cache:clear
执行以上三个命令后就可以正常访问了,具体原因没有找到...
参考文献:
https://yq.aliyun.com/articles/667223
https://laravel.com/docs/5.8/homestead#first-steps
https://learnku.com/docs/laravel-development-environment/5.5/development-environment-macos/937
https://blog.csdn.net/saber_h/article/details/90041262
https://www.jianshu.com/p/451b13e23e62
https://www.jianshu.com/p/6ac5fa9e905b
https://yq.aliyun.com/articles/667223