我在用Mac 上的 Homestead + Vagrant + Virtualbox.
Problem
虽然我发现很多线程/答案如何解决响应时间慢的问题(例如 TTFB),但没有一个起作用。我的响应时间在 25 - 32 秒之间,这对于本地开发来说显然是不可接受的。
建议的解决方案
我从这里尝试了很多建议的解决方案:https://github.com/laravel/homestead/issues/901 https://github.com/laravel/homestead/issues/901
并且还阅读并尝试了这些线程中的许多建议:
- Homestead 的反应非常慢 https://stackoverflow.com/questions/37636159/very-slow-responses-on-homestead
- 流浪家园慢 https://stackoverflow.com/questions/25761707/vagrant-homestead-slow
- 自上次请求 60 秒后,vagrant 页面加载缓慢 https://stackoverflow.com/questions/29571079/vagrant-slow-page-load-after-60-seconds-from-last-request
- 加快 Vagrant 上主机和来宾之间的同步延迟(NFS 同步文件夹) https://stackoverflow.com/questions/26818907/speed-up-sync-latency-between-host-and-guest-on-vagrant-nfs-sync-folders
尽管有公认的答案,但没有一个对我有帮助。
禁用 xdebug
我可以这么说禁用 xdebug就像解释的那样here https://christattum.com/disabling-xdebug-in-laravel-homestead/帮我节省了 5 秒。
更改光盘大小
根据建议将虚拟机的磁盘大小从动态更改为固定here https://github.com/laravel/homestead/issues/901#issuecomment-483332495并解释了here https://gist.github.com/christopher-hopper/9755310根本没有帮助(结果更糟)。
按照建议使用 NFS(同步文件夹)here https://stackoverflow.com/questions/26818907/speed-up-sync-latency-between-host-and-guest-on-vagrant-nfs-sync-folders
将 homestead/vagrant 设置为 NFS 也没有任何帮助。
尝试过(流浪文件):
Vagrant.configure("2") do |config|
config.vm.synced_folder ".", "/vagrant", type: "nfs", mount_options:['nolock,vers=3,udp,noatime,actimeo=1']
end
也尝试过(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 在这两种情况下都可以工作,但它并没有改变页面加载时 TTFB 的任何情况。
设置 natdnshostresolver: 关闭
我还尝试按照建议关闭 natdnshostresolverhere https://github.com/laravel/homestead/issues/901#issuecomment-406409231它没有改变任何事情。
调整 Virtualbox 映像
当然,我也尝试过增加 RAM、CPU、显卡等,但正如你所见,这并没有帮助。
任何其他建议
截至目前,我也愿意尝试,例如valet https://laravel.com/docs/6.x/valet或您可以提供的任何其他建议/解决方案。
预先非常感谢!
Update 1
改变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/*;
}
Update 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
Vagrant文件的内容:
# -*- 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