在我工作的地方,我们尝试了两种解决 Vagrant + Symfony 速度慢的问题的方法。我推荐第二个(nfs 和绑定挂载)。
rsync 方法
首先,我们使用rsync。我们的方法与AdrienBrault 的回答中概述的方法略有不同。相反,我们的代码如下Vagrantfile
:
config.vm.define :myproj01 do |myproj|
# Networking & Port Forwarding
myproj.vm.network :private_network, type: "dhcp"
# NFS Share
myproj.vm.synced_folder ".", "/home/vagrant/current", type: 'rsync', rsync__exclude: [
"/.git/",
"/vendor/",
"/app/cache/",
"/app/logs/",
"/app/uploads/",
"/app/downloads/",
"/app/bootstrap.php.cache",
"/app/var",
"/app/config/parameters.yml",
"/composer.phar",
"/web/bundles",
"/web/uploads",
"/bin/behat",
"/bin/doctrine*",
"/bin/phpunit",
"/bin/webunit",
]
# update VM sooner after files changed
# see https://github.com/smerrill/vagrant-gatling-rsync#working-with-this-plugin
config.gatling.latency = 0.5
end
正如您可能从上面注意到的那样,我们使用Vagrant gatling rsync 插件保持文件同步。
改进的 NFS 方法,使用绑定挂载(推荐解决方案)
rsync 方法解决了速度问题,但我们发现它存在一些问题。特别是,当在 VM 上生成文件(如composer.lock
或 Doctrine 迁移)时,或者当我们想要访问/vendor
. 我们必须使用 SFTP 将内容复制回来 - 对于新文件,请在下一次运行 gatling 插件清除它们之前执行此操作!
因此,我们转向了一种解决方案,该解决方案使用绑定挂载来以不同的方式处理缓存和日志等文件夹。没有这些共享会大大提高速度。
Vagrantfile 的相关位如下:
# Binding mounts for folders with dynamic data in them
# This must happen before provisioning, and on every subsequent reboot, hence run: "always"
config.vm.provision "shell",
inline: "/home/vagrant/current/bin/bind-mounts",
run: "always"
上面引用的bind-mounts
脚本如下所示:
#!/bin/bash
mkdir -p ~vagrant/current/app/downloads/
mkdir -p ~vagrant/current/app/uploads/
mkdir -p ~vagrant/current/app/var/
mkdir -p ~vagrant/current/app/cache/
mkdir -p ~vagrant/current/app/logs/
mkdir -p ~vagrant/shared/app/downloads/
mkdir -p ~vagrant/shared/app/uploads/
mkdir -p ~vagrant/shared/app/var/
mkdir -p ~vagrant/shared/app/cache/
mkdir -p ~vagrant/shared/app/logs/
sudo mount -o bind ~vagrant/shared/app/downloads/ ~/current/app/downloads/
sudo mount -o bind ~vagrant/shared/app/uploads/ ~/current/app/uploads/
sudo mount -o bind ~vagrant/shared/app/var/ ~/current/app/var/
sudo mount -o bind ~vagrant/shared/app/cache/ ~/current/app/cache/
sudo mount -o bind ~vagrant/shared/app/logs/ ~/current/app/logs/
NFS + 绑定安装是我推荐的方法。