解决 Windows 系统使用 Homestead 运行 Laravel 本地项目响应缓慢问题

注意: 此方法可能带来未知副作用,请操作之前务必备份原配置文件!!

使用 win7 电脑学习第二本教程,发现项目运行起来切换页面每次都要 7-10 s,很不正常啊,部署到服务器发现加载仅需要 200 ms 左右。
file

尝试过增加虚拟机配置, 但是没有任何效果, 经过验证也不是数据库的原因,通过网上查询了解到, 是因为 VirtualBox 的 IO 引起的。

解决方案是安装NFS Plugin参考文章(英文)

英文不好的小伙伴接着看 ~

首先,命令行进入 Homestead 启动 vagrant

> cd ~/Homestead && vagrant up

然后运行安装命令

$ vagrant plugin install vagrant-winnfsd

file

如上图,安装成功后修改配置文件

修改配置文件前建议先备份,以免修改后出现问题!

文件1:homestead/scripts/homestead.rb

# Register All Of The Configured Shared Folders
if settings.include? 'folders'
    settings["folders"].each do |folder|
        if File.exists? File.expand_path(folder["map"])
            mount_opts = []

            if (folder["type"] == "nfs")
                mount_opts = folder["mount_options"] ? folder["mount_options"] : ['actimeo=1', 'nolock']
            elsif (folder["type"] == "smb")
                mount_opts = folder["mount_options"] ? folder["mount_options"] : ['vers=3.02', 'mfsymlinks']
            end

            # For b/w compatibility keep separate 'mount_opts', but merge with options
            options = (folder["options"] || {}).merge({ mount_options: mount_opts })

            # Double-splat (**) operator only works with symbol keys, so convert
            options.keys.each{|k| options[k.to_sym] = options.delete(k) }

            config.vm.synced_folder folder["map"], folder["to"], type: folder["type"] ||= nil, **options

            # Bindfs support to fix shared folder (NFS) permission issue on Mac
            if Vagrant.has_plugin?("vagrant-bindfs")
                config.bindfs.bind_folder folder["to"], folder["to"]
            end
        else
            config.vm.provision "shell" do |s|
                s.inline = ">&2 echo \"Unable to mount one of your folders. Please check your folders in Homestead.yaml\""
            end
        end
    end
end

查找此段代码(可能略有不同),替换为以下内容

if settings.include? 'folders'
  settings["folders"].sort! { |a,b| a["map"].length <=> b["map"].length }

  settings["folders"].each do |folder|
    config.vm.synced_folder folder["map"], folder["to"], 
    id: folder["map"],
    :nfs => true,
    :mount_options => ['nolock,vers=3,udp,noatime']
  end
end

文件2:Homestead.yaml

folders:
    - map: ~/Code
      to: /home/vagrant/Code
      type: nfs

重启 Homestead 使配置文件生效,大功告成。再次运行项目响应时间已经正常了,希望帮助有相同情况的小伙伴!
file

本文章首发在 Laravel China 社区