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

wbq*_*wbq 6 php macos vagrant laravel homestead

在Mac上使用Homestead + Vagrant + Virtualbox

问题

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

建议的解决方案

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

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

即使有公认的答案,也没有一个能帮助我。

禁用xdebug

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

更改光盘大小

虽然改变从动态虚拟机的磁盘大小,以固定的建议在这里,并解释在这里一点都没有帮助(结果更差)。

使用NFS(同步文件夹)的建议在这里

此外,将宅基地/流浪者设置为NFS也无济于事。

尝试过(无用文件):

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

也尝试过(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']
Run Code Online (Sandbox Code Playgroud)

NFS在这两种情况下均能正常工作,但它并没有改变有关页面加载时TTFB的功能。

设置natdnshostresolver:关闭

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

调整Virtualbox映像

当然,我也尝试增加RAM,CPU,图形等内容,但是如您所见,它没有帮助。

任何其他建议

到目前为止,我也愿意尝试例如代客泊车或其他任何您可以给出的建议/解决方案。

在此先多谢!

更新1

更改我的VM上的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/*;
}


Run Code Online (Sandbox Code Playgroud)

更新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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

wbq*_*wbq 4

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

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

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

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

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

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