10

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:

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
wbq
  • 633
  • 4
  • 13
  • Using nginx or apache? – emotality Nov 18 '19 at 14:25
  • @emotality Thanks for your comment! It's **nginx/1.15.8** – wbq Nov 18 '19 at 16:01
  • Answered, not sure if its the issue but lets see? Let me know :) – emotality Nov 18 '19 at 16:47
  • try to install the bindfs plugin `vagrant plugin install vagrant-bindfs` and keep the mapping in `homestead.yaml` not in the vagrant file. I also suggest destroying the VM and start fresh. – Razor Nov 18 '19 at 17:28
  • @Razor thanks for your suggestion. I did install Bindfs and I'm getting ```==> myproject: Bindfs seems to not be installed on the virtual machine, installing now myproject: Bindfs 1.13.7 is installed ==> myproject: Machine is ready to use bindfs! ==> myproject: Creating bind mounts after synced_folders... myproject: /home/vagrant/code => /home/vagrant/code``` . Unfortunately it didn't solve the issue. – wbq Nov 19 '19 at 06:09
  • @dev_dari can you share your homestead.yaml file (also the Vagrantfile if you made any changes), and the version number of homestead/vagrant/virtualbox. Try to sync a new laravel project instead of yours so we can at least narrow down the issues – Razor Nov 19 '19 at 14:07
  • Is it 25-32 seconds to first byte (ttfb) or until the browser loads all files? How many files does the browser load? – Mischa Nov 19 '19 at 21:30
  • @Razor thanks for getting back! homestead.yaml is now attached as well as the Vagrantfile. Thanks a lot in advance for your help!! – wbq Nov 20 '19 at 09:17
  • @Mischa dev console gives me about 150 requests / 2.7MB resources and I'm talking about TTFB (e.g. **TTFB:** 26.71 s; **Fully loaded:** 29.65 s) . – wbq Nov 20 '19 at 09:23

6 Answers6

4

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.

wbq
  • 633
  • 4
  • 13
3

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

emotality
  • 12,795
  • 4
  • 39
  • 60
  • 1
    Thank you very much for your extensive answer. I logged into vagrant and edited the nginx.conf with nano. I commented out `keepalive_timeout 65;` and added your suggested lines. I recognized a slight decrease of about 3 seconds in TTFB, but in sum I still have 22-23 seconds (best case) for a single page load. So it seems to have helped a little bit so far. Maybe there are any other tweaks in the nginx.conf which I could do which is why I will add the http part of the nginx.conf to my question. Thx! – wbq Nov 19 '19 at 05:44
  • Dammit, this is bothering me now.. Did you try and destroy the VM and start fresh? – emotality Nov 19 '19 at 07:24
  • yes, several times. Not with your nginx changes though. I will try to do so. – wbq Nov 19 '19 at 07:52
  • Thanks again. I now destroyed the vagrant and upped it again. Re-did your suggested changes and also made sure Bindfs (suggested by @Razor ) is running. Unfortunately still no result for the better. I guess It might has something to do with the amount of files (14242)? – wbq Nov 19 '19 at 08:44
0

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.

magicbyt3
  • 143
  • 9
  • Thanks for the reply. I figured it out just now and will post my result in a couple of minutes. – wbq Nov 23 '19 at 15:52
0

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

just adding and enabling nfsd to the full disk access list should work

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

Jeremy Anderson
  • 826
  • 5
  • 16
  • 1
    This answer actually did not help as much as indicated, I'm not sure if I should redact it or what? I switched to Laravel Valet, zippy and simple, but more variant from the production environment, ah well. – Jeremy Anderson Apr 02 '20 at 18:44
0

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?

Rbbn
  • 627
  • 7
  • 13
-1

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.

McRui
  • 1,879
  • 3
  • 20
  • 31