Laravel homestead鼓捣记录
之前用的别人配置好的,现在闲来无事想弄个新版的,就自己花了一晚上鼓捣了下。不知道为啥,现在的教程都是低级的,其实我觉得应该都差不多。
前置任务
安装Git/composer vagrant virtualbox
任务1 配置homestead环境
从Git clone homestead
git clone https://github.com/laravel/homestead.git
然后把这一段抄来的 Vagrantfile
扔进去
- 注意,不能用vagrant init获得,否则生成几乎为空的文件,无法直接连接homestead.yaml的配置
# -*- mode: ruby -*- # vi: set ft=ruby : require 'json' require 'yaml' VAGRANTFILE_API_VERSION ||= "2" confDir = $confDir ||= File.expand_path(File.dirname(__FILE__)) homesteadYamlPath = confDir + "/Homestead.yaml" homesteadJsonPath = confDir + "/Homestead.json" afterScriptPath = confDir + "/after.sh" aliasesPath = confDir + "/aliases" require File.expand_path(File.dirname(__FILE__) + '/scripts/homestead.rb') #Vagrant.require_version '>= 1.9.0' Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| if File.exist? aliasesPath then config.vm.provision "file", source: aliasesPath, destination: "/tmp/bash_aliases" config.vm.provision "shell" do |s| s.inline = "awk '{ sub(\"\r$\", \"\"); print }' /tmp/bash_aliases > /home/vagrant/.bash_aliases" end end if File.exist? homesteadYamlPath then settings = YAML::load(File.read(homesteadYamlPath)) elsif File.exist? homesteadJsonPath then settings = JSON.parse(File.read(homesteadJsonPath)) else abort "Homestead settings file not found in #{confDir}" end Homestead.configure(config, settings) if File.exist? afterScriptPath then config.vm.provision "shell", path: afterScriptPath, privileged: false end if defined? VagrantPlugins::HostsUpdater config.hostsupdater.aliases = settings['sites'].map { |site| site['map'] } end end
任务2 下载盒子并导入vagrant
-
下载
这里是homestead v8.2.1的下载地址,然后下完了创建文件metadata.json
和它一起。-
metadata.json的内容如下
{ "name": "laravel/homestead", "versions": [ { "version": "8.2.1", "providers": [ { "name": "virtualbox", "url": "homestead.box" } ] } ] }
-
-
导入
用命令行(如git、ps等)切到它们所在的文件夹,输入vagrant box add metadata.json
导入就此结束。
任务3 配置并启动homestead
其实并没有什么配置的必要,切到homestead目录用命令行vagrant up
启动,vagrant ssh
连接即可。
- 另外关闭是halt,挂起suspend,恢复resume,热更新
homestead.yaml
配置的provision(常用的就这些吧)。 - 关于配置,在
homestead.yaml
里有,to
是本机位置,map
是映射到虚拟机的位置。 - 所有的端口后面加个0映射到本机。
- 默认服务器是Nginx,需要改root就
sudo vim /etc/nginx/sites-available/你的yaml内自定义域名
里边改,或者用上边方法热更。 - 本机hosts改一下homestead域名和IP的绑定。
然后就没有然后了。