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

原文:https://learnku.com/articles/9009/solve-the-slow-response-problem-of-windows-system-using-homestead-to-run-laravel-local-project

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

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

file

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

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

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

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

copy> cd ~/Homestead && vagrant up

然后运行安装命令

copy$ vagrant plugin install vagrant-winnfsd

file

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

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

文件 1:homestead/scripts/homestead.rb

copy# 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

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

copyif 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

copyfolders:
    - map: ~/Code
      to: /home/vagrant/Code
      type: nfs
copyvagrant provision && vagrant reload
//更新配置并重启虚拟机

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

posted @   pine007  阅读(543)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示