在 Mac OSX 上非常慢 laravel homestead/vagrant/virtualbox

Very slow laravel homestead/vagrant/virtualbox on Mac OSX

我在 Mac 上使用 Homestead + Vagrant + Virtualbox。

问题

虽然我发现很多 threads/answers 如何解决响应时间慢的问题(例如 TTFB),但其中 none 有效。我的响应时间在 25 到 32 秒之间变化,这对于本地开发来说显然是不可接受的。

建议的解决方案

我从这里尝试了很多建议的解决方案:https://github.com/laravel/homestead/issues/901

并且还阅读并尝试了来自这些线程的许多建议:

  • Very Slow Responses On Homestead
  • Vagrant Homestead slow
  • vagrant slow page load after 60 seconds from last request
  • Speed up sync latency between host and guest on Vagrant (NFS sync folders)

即使有已接受的答案,none 对我有帮助。

正在禁用 xdebug

我可以这么说 像解释的那样禁用 xdebug here 帮助我节省了 5 秒。

更改光盘大小

虽然按照建议将 VM 的磁盘大小从动态更改为固定 here and explained here 根本没有帮助(结果更糟)。

按照建议使用 NFS(同步文件夹)here

同时将 homestead/vagrant 设置为 NFS 也没有任何帮助。

已尝试(流浪文件):

Vagrant.configure("2") do |config|
  config.vm.synced_folder ".", "/vagrant", type: "nfs", mount_options:['nolock,vers=3,udp,noatime,actimeo=1']
end

也尝试过(homestead.yaml)

folders:
    -
        map: '/Users/myuser/PhpstormProjects/example.com'
        to: /home/vagrant/code
        type: "nfs"
        options:
            mount_options: ['nolock','vers=3','udp','noatime','actimeo=1']

NFS 在这两种情况下都有效,但它并没有改变页面加载时 TTFB 的任何事情。

正在设置 natdnshostresolver:关闭

我也尝试按照建议关闭 natdnshostresolver here 它没有改变一件事。

调整 Virtualbox 图像

当然,我也尝试过增加 RAM、CPU、图形等,但如您所见,这并没有帮助。

任何其他建议

截至目前,我也愿意尝试,例如valet 或您可以提供的任何其他建议/解决方案。

非常感谢!

更新 1

更改我的虚拟机上的 nginx.conf(在@emotality 建议调整后)确实有一点帮助。为了完整起见,为了可能会有更多的调整,这里是 nginx.conf 文件的整个 http 部分。

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        # keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        keepalive_disable none;
        keepalive_requests 200;
        keepalive_timeout 300s;

        server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}


更新 2

homestead.yaml的内容:

ip: 192.168.10.14
memory: 4096
cpus: 2
provider: virtualbox
natdnshostresolver: off
authorize: ~/.ssh/id_rsa.pub
keys:
    - ~/.ssh/id_rsa
folders:
    -
        map: '/Users/myUser/PhpstormProjects/exampleproject.com'
        to: /home/vagrant/code
        type: "nfs"
        options:
            mount_options: ['nolock','vers=3','udp','noatime','actimeo=1']
sites:
    -
        map: exampleproject.local
        to: /home/vagrant/code
databases:
    - homestead
features:
    -
        mariadb: false
    -
        ohmyzsh: false
    -
        webdriver: false
name: exampleproject
hostname: exampleproject

Vagrantfile 的内容:

# -*- mode: ruby -*-
# vi: set ft=ruby :

require 'json'
require 'yaml'

VAGRANTFILE_API_VERSION ||= "2"
confDir = $confDir ||= File.expand_path("vendor/laravel/homestead", File.dirname(__FILE__))

homesteadYamlPath = File.expand_path("Homestead.yaml", File.dirname(__FILE__))
homesteadJsonPath = File.expand_path("Homestead.json", File.dirname(__FILE__))
afterScriptPath = "after.sh"
customizationScriptPath = "user-customizations.sh"
aliasesPath = "aliases"

require File.expand_path(confDir + '/scripts/homestead.rb')

Vagrant.require_version '>= 2.2.4'

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 " + File.dirname(__FILE__)
    end

    Homestead.configure(config, settings)

    if File.exist? afterScriptPath then
        config.vm.provision "shell", path: afterScriptPath, privileged: false, keep_color: true
    end

    if File.exist? customizationScriptPath then
        config.vm.provision "shell", path: customizationScriptPath, privileged: false, keep_color: true
    end

    if Vagrant.has_plugin?('vagrant-hostsupdater')
        config.hostsupdater.aliases = settings['sites'].map { |site| site['map'] }
    elsif Vagrant.has_plugin?('vagrant-hostmanager')
        config.hostmanager.enabled = true
        config.hostmanager.manage_host = true
        config.hostmanager.aliases = settings['sites'].map { |site| site['map'] }
    end
end

我的 Laravel 项目也很慢,但只有在使用 Postman 时才会如此,假设每次我发出请求时它都会启动,这会给每个请求增加 10-15 秒。我的解决方案是调整 Keep-Alive 设置。

假设正在发生的事情是它打开一个新连接,进行握手,传输资源,关闭连接,并为页面上的每个资源重复。我可能是错的,但请在下面尝试并让我们看看。 :)

这只适用于本地开发,不建议用于生产环境。


Apache

$ sudo nano /etc/apache2/httpd.conf

顶部:

KeepAlive On
MaxKeepAliveRequests 200
KeepAliveTimeout 300

然后重启apache


nginx

$ sudo nano /etc/nginx/nginx.conf

http {}块中:

keepalive_disable none;
keepalive_requests 200;
keepalive_timeout 300s;

然后重启nginx

我曾经有一个网站连接到 'localhost' 而不是我本地的“127.0.0.1”用于开发,这个小事实使得 DNS 查找需要很长时间,甚至 GraphQL 也需要 3 秒才能响应。也许你这边也有类似的情况。

感谢你们所有人,但我找到了一个非常有趣的解决方案,或者更确切地说,我遇到了一个问题。

我正在使用本地环境进行 wordpress 安装。在使用 Memcached 的 wp-content 文件夹中有一个名为“object-cache.php”的文件。 Memcached 安装在 homestead 中,但配置似乎与我的实时服务器不同。

这会导致本地文件无法正确缓存,最终导致代码为每个请求从数据库中提取所有可用选项。

所以总而言之,这是一个巨大的缓存问题。

删除对象-cache.php文件现在是我的解决方案(导致 TTFB 为 1.23 秒)。

我只是把它留在这里,以防有人遇到类似的问题。再次感谢你们提供的所有帮助和想法。

对于 macOS "High Sierra" 或更高版本上的 运行 Homestead,对我有用的解决方案就像在 homestead.rb[ 中更改一些设置一样简单=36=] 文件.

无论您在 homestead.rb 文件中的哪个位置找到 settings['cpus'] ||= 1 的设置,请将其更改为 settings['cpus'] ||= 2。您也可以增加内存大小(我没有)并将值设置为大于默认值 settings['memory'] ||= 2048.

在我尝试几乎所有在网络上找到的解决方案之前,从确保 nfs 已设置、添加脚本和其他建议以及 none 工作直到我增加 cpu 默认值设置为 settings['cpus'] ||= 2

在终端中,运行 npm run dev 或任何 php artisan 命令的简单任务 大约需要 10 到 15 秒 直到提示可以自由继续其他命令。

经过上述更改,现在只需要 2 到 3 秒

我希望这可以帮助遇到同样缓慢性能的任何人,尤其是在 macOS 上。 "High Sierra" 或更高版本。我在 macOS "Catalina" 上,现在一切正常。

Vagrant via VirtualBox on Catalina(MacMini Late 2012(双 SSD 和 16MB RAM)post-升级)对我来说非常慢,不仅限于 PHP 或Javascript 个项目,尽管这主要是我一直在从事的工作。我花了一点时间研究,对我有用的解决方案是将 /sbin/nfsdVirtualBox 添加到 Mac 上的 Settings->Privacy 中的 Full Disk Access,如在下面的 link 中描述。我希望这会帮助别人。在我的例子中,TTFB 从大约 15 秒减少到不到 1 秒。(这对 Vagrant 来说非常好,哈!)

https://github.com/hashicorp/vagrant/issues/10961#issuecomment-567430897

@wbq 的回答是最好的。但它错过了对我有用的一件事。 我尝试了一切:最新版本的 Vagrant、Virtualbox、NFS 文件夹、增加 RAM 等,检查代码(不是这个问题出现在除本地以外的任何其他环境中)。每次请求都需要8-12秒。废话少说,这是我的解决方案:

在.env文件中

CACHE_DRIVER=memcached

(而不是 CACHE_DRIVER=数组)

https://laravel.com/docs/6.x/cache

10 岁信息:memcached vs. internal caching in PHP?