I am using Homestead + Vagrant + Virtualbox on a Mac.
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.
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.
I can say that Disabling xdebug like explained here helped me to save 5 seconds.
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).
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.
I also tried to turn off natdnshostresolver as suggested here It didn't change a thing.
Of course I also tried to increase RAM, CPUs, Graphic stuff, etc. but as you can figure it didn't help.
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!
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/*;
}
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
# -*- 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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With