Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Very slow laravel homestead/vagrant/virtualbox on Mac OSX

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
like image 420
wbq Avatar asked Nov 15 '19 04:11

wbq


2 Answers

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.

like image 50
wbq Avatar answered Oct 20 '22 09:10

wbq


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

like image 3
emotality Avatar answered Oct 20 '22 08:10

emotality