我在Mac上使用 Homestead + Vagrant + Virtualbox 。
虽然我发现了很多解决慢响应时间(例如TTFB)的线程/答案,但是它们都不起作用。我的响应时间在25到32秒之间变化,这显然对于本地开发来说是不可接受的。
我从这里尝试了很多建议的解决方案:https://github.com/laravel/homestead/issues/901
并且还阅读并尝试了以下这些线程的许多建议:
即使已经接受了答案,但没有一个能帮助我。
我可以这么说 像说明的here一样禁用xdebug 可以帮助我节省5秒钟。
按照建议here并按照here的说明将VM的磁盘大小从动态更改为固定时,根本没有帮助(结果更糟)。
也将宅基地/流浪者设置为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的情况。
我还尝试根据建议here关闭natdnshostresolver 它并没有改变任何事情。
当然,我也尝试增加RAM,CPU,图形等内容,但是如您所见,这没有帮助。
截至目前,我也愿意尝试valet或您可以提供的其他任何建议/解决方案。
非常感谢!
在我的VM上更改 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/*;
}
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
答案 0 :(得分:3)
我的Laravel项目也很慢,但只有在使用Postman时,假设每次我发出请求时它都会启动,这会使每个请求增加10-15秒。我的解决方案是调整Keep-Alive
设置。
假设正在发生的事情是打开一个新连接,进行握手,转移资源,关闭连接,并对页面上的每个资源重复执行。我可能是错的,但请尝试以下内容,让我们看看。 :)
这仅用于本地开发,我不建议在生产环境中使用。
Apache
$ sudo nano /etc/apache2/httpd.conf
在顶部:
KeepAlive On
MaxKeepAliveRequests 200
KeepAliveTimeout 300
然后重新启动apache
nginx
$ sudo nano /etc/nginx/nginx.conf
在http {}
块中:
keepalive_disable none;
keepalive_requests 200;
keepalive_timeout 300s;
然后重新启动nginx
答案 1 :(得分:2)
谢谢大家,但是我找到了一个非常有趣的解决方案,或者说是我遇到的一个问题。
我正在使用本地环境进行wordpress安装。在使用Memcached的wp-content文件夹中有一个名为“ object-cache.php ”的文件。 Memcached已安装在宅基地内,但配置与实时服务器不同。
这导致本地文件无法正确缓存,最终导致代码为每个请求从数据库中提取所有可用选项。
总而言之,这是一个巨大的缓存问题。
现在,删除object-cache.php文件是我的解决方案(导致TTFB为1.23秒)。
如果有人遇到类似问题,我就把它留在这里。再次感谢您提供的所有帮助,并以为你们对此表示感谢。
答案 2 :(得分:0)
我曾经有一个站点连接到本地主机上的“ localhost”而不是“ 127.0.0.1”进行开发,这个事实使DNS查找花费了很长时间,甚至GraphQL都花了3秒来响应。也许这与您的目标相似。
答案 3 :(得分:0)
对于在macOS“ High Sierra”或更高版本上运行Homestead的用户,对我有效的解决方案就像更改 homestead.rb 文件中的一些设置一样简单。
只要在 homestead.rb 文件中找到settings['cpus'] ||= 1
的设置,即可将其更改为 settings['cpus'] ||= 2
。另外,您可能会增加内存大小(我没有),并将其值设置为大于默认值settings['memory'] ||= 2048
。
在尝试尝试在网络上找到的几乎所有解决方案之前,都要确保设置了nfs
,添加脚本和其他建议,但是在我将 cpu 默认值设置增加到 settings ['cpus'] || = 2
在终端中,运行npm run dev
或任何php artisan
命令的简单任务花费大约10到15秒,直到提示可以自由执行其他命令。
通过上述更改,现在仅需2到3秒!
我希望这可以帮助遇到同样缓慢性能的任何人,尤其是在macOS上。 “高山脉”或更高版本。我在macOS“ Catalina”上运行,现在一切正常。
答案 4 :(得分:0)
通过Catalina上的VirtualBox(在MacMini 2012后期(双SSD和16MB RAM)升级后)流浪对于我来说异常缓慢,不仅限于PHP或Javascript项目,尽管这主要是我一直在做的事情。我花了一些时间进行研究,对我有用的解决方案是在Mac上的/sbin/nfsd
中将VirtualBox
和Full Disk Access
添加到Settings->Privacy
中,如链接所示下面。我希望这会帮助其他人。就我而言,TTFB从大约15秒减少到不到1秒。(对Vagrant来说,这很不错,哈!)
https://github.com/hashicorp/vagrant/issues/10961#issuecomment-567430897
答案 5 :(得分:0)
@wbq 的回答是最好的。但它错过了对我有用的一件事。 我尝试了一切:最新版本的 Vagrant、Virtualbox、NFS 文件夹、增加 RAM 等,检查代码(不是这个问题出现在除本地之外的任何其他环境中)。每次我提出请求都需要 8-12 秒。话不多说,这是我的解决方案:
在.env文件中
CACHE_DRIVER=memcached
(而不是 CACHE_DRIVER=array)