解決 Windows 系統使用 Homestead 執行 Laravel 本地專案響應緩慢問題

Yvan發表於2018-03-22

注意: 此方法可能帶來未知副作用,請操作之前務必備份原配置檔案!!

使用 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

相關文章