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

标签 php laravel macos vagrant homestead

我正在使用 Mac 上的 Homestead + Vagrant + Virtualbox .

问题

虽然我发现了很多线程/答案如何修复缓慢的响应时间(例如 TTFB),但它们都不起作用。我的响应时间在 25 到 32 秒之间变化,这对于本地开发来说显然是 Not Acceptable 。

建议的解决方案

我从这里尝试了很多建议的解决方案: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)

  • 尽管有公认的答案,但没有一个对我有帮助。

    禁用 xdebug

    我可以说
    禁用 xdebug 如解释 here帮我节省了 5 秒钟。

    更改光盘大小

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

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

    同样将宅基地/Vagrant 设置为 NFS 也无济于事。

    尝试过( Vagrant 文件):
    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 在我的 VM 上(在@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
    

    最佳答案

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

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

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

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

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

    我只是留在这里以防万一有人遇到类似的问题。再次感谢所有的帮助,并认为你们投入了这个。

    关于php - 在 Mac OSX 上非常慢的 laravel homestead/vagrant/virtualbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58870436/

    相关文章:

    php - React前端到Laravel和mysql的axios post请求失败

    php - 拉维尔 5 : Krlove method does not exist

    php - Laravel 护照动态范围

    macos - 警告 : Your version of git is 1. 9.3。存在严重的安全漏洞

    php - Swift 上传图片,服务器响应说文件不是图片?

    php - 如何将下拉列表中的值插入到 mysql 查询中以从数据库中检索数据

    php - 在 Ubuntu 服务器上安装多个 PHP 版本

    php - Laravel 重定向到区域设置更改的路由

    ruby - 如何为 Ruby 永久修复 "dyld: Library not loaded:/usr/local/opt/gmp/lib/libgmp.10.dylib"?

    xcode - 在 OSX 上安装 Tensorflow - clang : error: linker command failed with exit code 1