首页 > 解决方案 > Mac OSX 上非常慢的 laravel homestead/vagrant/virtualbox

问题描述

在 Mac 上使用 Homestead + Vagrant + Virtualbox

问题

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

建议的解决方案

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

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

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

禁用 xdebug

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

更改光盘大小

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

按照此处的建议使用 NFS(同步文件夹)

将宅基地/流浪者设置为 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: off

我还尝试按照此处的建议关闭 natdnshostresolver 它没有改变任何事情。

调整 Virtualbox 图像

当然,我也尝试增加内存、CPU、图形等,但你可以想象它并没有帮助。

任何其他建议

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

提前非常感谢!

更新 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

标签: phplaravelmacosvagranthomestead

解决方案


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

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

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

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

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

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


推荐阅读