Very slow laravel homestead/vagrant/virtualbox on Mac OSX

走远了吗. 提交于 2021-02-18 21:12:20

问题


I am using Homestead + Vagrant + Virtualbox on a Mac.

Problem

While I found lots of threads/answers how to fix slow response times (e.g. TTFB) none of them worked. My response times vary between 25 - 32 seconds, which of obviously is not acceptable for local development.

Suggested Solutions

I have tried a lot of suggested solutions from here: https://github.com/laravel/homestead/issues/901

And have also read and tried many suggestions from these threads:

  • 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)

Even though there were accepted answers, none of them helped me.

Disabling xdebug

I can say that Disabling xdebug like explained here helped me to save 5 seconds.

Changing disc size

While changing the VM's disc size from dynamic to fixed as suggested here and explained here didn't help at all (result was even worse).

Using NFS (sync folders) as suggested here

Also setting homestead/vagrant to NFS didn't help a thing.

Tried (vagrant file):

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

Also tried (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 was working in both cases but it didn't change a thing regarding TTFB on page load.

Setting natdnshostresolver: off

I also tried to turn off natdnshostresolver as suggested here It didn't change a thing.

Adjusting Virtualbox Image

Of course I also tried to increase RAM, CPUs, Graphic stuff, etc. but as you can figure it didn't help.

Any other suggestions

As of now I'm also open to try e.g. valet or for any other recommendations / solutions you could give.

Thanks a lot in advance!

Update 1

Altering the nginx.conf on my VM (after @emotality suggested a tweak) did help a little bit. For the sake of completeness and the possibility there could be tweaked even a little bit more, here's the whole http part of the nginx.conf file.

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/*;
}


Update 2

Content of 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

Content of 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

回答1:


Thanks to all of you guys, but I found a quite interesting solution or rather an issue that I had.

I was using the local environment for a wordpress install. There was a file called "object-cache.php" in the wp-content folder which uses Memcached. Memcached is installed within homestead but seems to have a different configuration than my live server.

This leads to local files not being cached properly, which ultimately results in the code pulling all available options from the database for each request.

So in sum it was a huge caching issue.

Removing the object-cache.php file is now my solution (resulting in a TTFB of 1.23 seconds).

I just leave this here in case anyone runs into a similar issue. Thanks again for all the help and thought you guys put into this.




回答2:


My Laravel projects are also slow but only when using Postman, assuming it's booting up every time I make a request which adds like 10-15seconds to every request. My solution was to tweak the Keep-Alive settings.

Assuming what's happening is it opens a new connection, do handshakes, transfer resource, close connection, and repeats for every resource on your page. I might be wrong but try below and lets see. :)

This is only for local development, I don't suggest this for production environment.


Apache

$ sudo nano /etc/apache2/httpd.conf

At the top:

KeepAlive On
MaxKeepAliveRequests 200
KeepAliveTimeout 300

Then restart apache


nginx

$ sudo nano /etc/nginx/nginx.conf

In the http {} block:

keepalive_disable none;
keepalive_requests 200;
keepalive_timeout 300s;

Then restart nginx




回答3:


I once had a site connecting to 'localhost' instead of '127.0.0.1' on my local for development, that little fact made the DNS lookup take ages and even GraphQL took 3 seconds to respond. Maybe it's something similar on your end.




回答4:


Vagrant via VirtualBox on Catalina (MacMini Late 2012 (dual SSD and 16MB RAM) post-upgrade) has been incredibly slow for me, not limited to PHP or Javascript projects, though that's mostly what I've been working on. I've spent a little time researching and the solution that worked for me was to add /sbin/nfsd and VirtualBox to Full Disk Access in Settings->Privacy on the Mac, as described at the link below. I hope this will help someone else. In my case, TTFB went from about 15 seconds to less than 1. (That's pretty good for Vagrant, ha!)

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




回答5:


The answer from @wbq is the best. But it misses one thing that worked for me. I tried everything: Newest version of Vagrant, Virtualbox, NFS-folders, increase RAM etc, check the code (not that this issue came on any other environments except locally). It took 8-12seconds every time I made a request. Enough talking, here is my solution:

In the file .env

CACHE_DRIVER=memcached

(Instead of CACHE_DRIVER=array)

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

10 years old info: memcached vs. internal caching in PHP?




回答6:


For those running Homestead on macOS "High Sierra" or later, the solution that worked for me was as simple as changing a few settings in the homestead.rb file.

Wherever you find in your homestead.rb file, the settings for settings['cpus'] ||= 1 change it to settings['cpus'] ||= 2. also you may increase memory size (I didn't) and set the value greater than the default that is settings['memory'] ||= 2048.

Before I tried almost every solution found on the web, from ensuring nfs was set, adding scripts and other suggestions and none worked until I increased the cpu default value settings to settings['cpus'] ||= 2

In the terminal, the simple task of running npm run dev or any php artisan commands took about 10 to 15 seconds until the prompt was free to proceed with other commands.

With the above changes, now it takes only 2 to 3 seconds!

I hope this helps anyone experiencing this same slow performance, especially on macOS. "High Sierra" or later. I'm on macOS "Catalina" and all works fine now.



来源:https://stackoverflow.com/questions/58870436/very-slow-laravel-homestead-vagrant-virtualbox-on-mac-osx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!