vagrant入门
This popular article was fully updated in 2017. Changes include information on public keys, troubleshooting tips, and updates for Windows 10 and other relevant software.
该热门文章于2017年进行了全面更新。更改包括有关公钥,故障排除提示的信息,以及Windows 10和其他相关软件的更新。
Vagrant has quickly become the ubiquitous go-to tool for local development across Mac, Windows, and Linux operating systems.
Vagrant已Swift成为跨Mac,Windows和Linux操作系统进行本地开发的无处不在的工具。
Vagrant helps you create virtual machines on-the-fly via a set of reusable configuration files. Developers can share their configurations and scripts via GitHub and elsewhere, so that other devs can spin up an identical environment and tooling.
Vagrant可帮助您通过一组可重复使用的配置文件动态创建虚拟机。 开发人员可以通过GitHub和其他地方共享其配置和脚本,以便其他开发人员可以启动相同的环境和工具。
It’s a great tool if you want to spring up servers for testing programs, learn how to use Linux tools, or work in a test environment before applying changes on a production system. Do you want to learn to install the PHP/Apache/MySQL stack from scratch on Ubuntu? Or play with setting up a cache server like Varnish in front of Apache? Try your hand at Nginx? Learn PHP 7? Vagrant makes things quite a bit simpler than using VirtualBox alone.
如果要在生产系统上应用更改之前启动服务器来测试程序,学习如何使用Linux工具或在测试环境中工作,则它是一个很好的工具。 您是否想学习在Ubuntu上从头开始安装PHP / Apache / MySQL堆栈? 还是在Apache前面设置像Varnish这样的缓存服务器? 尝试Nginx吗? 学习PHP 7? 与仅使用VirtualBox相比,Vagrant使事情简单得多。
Let’s take a look at how to get Vagrant set up in a Windows environment.
让我们看一下如何在Windows环境中设置Vagrant。
安装兆位和碎片 (Installing the Megabits and Pieces)
To get started, go ahead and install these core tools:
首先,请继续安装以下核心工具:
VirtualBox (the software that creates virtual machines)
VirtualBox (创建虚拟机的软件)
Vagrant (our hero, the software that deploys virtual machines and runs provisioning scripts)
Vagrant (我们的英雄,部署虚拟机并运行配置脚本的软件)
PuTTY and PuTTYGen (SSH client and a generator for security keys)
PuTTY和PuTTYGen (SSH客户端和安全密钥生成器)
VirtualBox and Vagrant install like any other Windows program. Vagrant will install itself to your global path so you can run it from anywhere.
VirtualBox和Vagrant的安装与其他Windows程序一样。 Vagrant会将其自身安装到您的全局路径,以便您可以在任何地方运行它。
创建你的项目 (Create Your Projects)
Now that you have it all set up, you can start your first Vagrant project by creating a project folder which will house the various configurations for each of your VMs. You’ll use the command line to run Vagrant commands from within these folders.
现在已经完成所有设置,您可以通过创建一个项目文件夹来启动第一个Vagrant项目,该文件夹将容纳每个VM的各种配置。 您可以使用命令行来从这些文件夹中运行命令流浪汉。
Create a project folder, I used E:\Vagrant\sitepoint
.
创建一个项目文件夹,我使用E:\Vagrant\sitepoint
。
Tip: As a Windows user, you can quickly open a command prompt from Explorer by holding down shift and right-clicking the folder or white-space within the folder, then choosing “Open Command Window Here”.
提示:作为Windows用户,您可以通过按住shift键并右键单击文件夹或文件夹中的空白,然后选择“在此处打开命令窗口”,从资源管理器中快速打开命令提示符。
使用无业游民 (Using Vagrant)
The primary commands to get started are vagrant init
and vagrant up
.
开始的主要命令是vagrant init
和vagrant up
。
Open a command line and change directories to your test project folder. Run vagrant init
and a new vagrant file will be created in the current directory called “Vagrantfile” (no extension) which holds a basic starter configuration.
打开命令行并将目录更改为测试项目文件夹。 运行vagrant init
,将在当前目录中创建一个新的vagrant文件,名为“ Vagrantfile”(无扩展名),该文件具有基本的启动程序配置。
Open Vagrantfile in a text editor like Notepad++ or Sublime and take a look, study the comments before each configuration value. You can set shared folders between guest and host, turn on port forwarding, set the hostname, and more. Notice that all but maybe one line is commented out: Vagrant has a default configuration it will use even without any settings changed in here.
在文本编辑器(如Notepad ++或Sublime)中打开Vagrantfile,然后看一下,研究每个配置值之前的注释。 您可以在来宾和主机之间设置共享文件夹,打开端口转发,设置主机名等等。 请注意,除了一行以外,所有内容都已被注释掉:Vagrant具有默认配置,即使在此处未更改任何设置,它也将使用。
This Vagrantfile doesn’t point to any sort of virtual machine yet, so go ahead and delete the Vagrantfile you just created and let’s find a “base box” to use.
该Vagrantfile尚未指向任何类型的虚拟机,因此,继续删除刚才创建的Vagrantfile ,让我们找到一个要使用的“基本框”。
Browse the Vagrant Cloud for base boxes you might like to use. For this example, we’ll use “ubuntu/trusty64” which is the “official” base box for Ubuntu Server 14.04 LTS (Trusty Tahr). To have Vagrant automatically use this base box, type this:
浏览Vagrant Cloud以查找您可能要使用的基本框。 在此示例中,我们将使用“ ubuntu / trusty64”,它是Ubuntu Server 14.04 LTS(Trusty Tahr)的“官方”基本框。 要让Vagrant自动使用此基本框,请输入以下内容:
vagrant init ubuntu/trusty64
This time, if you open Vagrantfile, you’ll notice it lists the base box in there for “config.vm.box”. Other settings are still commented out.
这次,如果打开Vagrantfile,您会注意到它列出了“ config.vm.box”的基本框。 其他设置仍被注释掉。
Now that you have a Vagrantfile with a base box configured, you can spin up the VM with this amazing command:
现在,您已经配置了一个配置了基本框的Vagrantfile,您可以使用以下惊人的命令启动VM:
vagrant up
To explain what’s happening here, Vagrant first imports the base box if you don’t already have it (I already had it, otherwise it would download first). It then checks if your box is up to date.
为了解释这里发生的情况,如果您没有基盒,那么Vagrant首先将其导入(我已经安装了基盒,否则先下载)。 然后,它会检查您的包装箱是否为最新。
Note: These base boxes live in your
%userprofile%/.vagrant.d/boxes
folder. You can list all installed boxes by typingvagrant box list
. You can delete boxes withvagrant box remove box/name
.注意:这些基本框位于您的
%userprofile%/.vagrant.d/boxes
文件夹中。 您可以通过键入“vagrant box list
来列出所有已安装的盒子。 您可以使用vagrant box remove box/name
。
You can see it sets up the SSH forwarding port as “2222”: you’ll need this to use an SSH client like PuTTY.
您会看到它将SSH转发端口设置为“ 2222”:使用像PuTTY这样的SSH客户端将需要此端口。
Notice the username is “vagrant”, which it always will be. You’ll also notice that it generates a new set of keys, something Vagrant didn’t do in previous versions. The public key is automatically copied into the VM and replaces the default key in there. It also places the private key in a new folder called “.vagrant” in your project folder. If you drill down in that folder you’ll find “private_key”, which will be useful to use in PuTTY later.
请注意,用户名始终是“ vagrant”。 您还会注意到,它会生成一组新的密钥,而Vagrant在以前的版本中没有这样做。 公钥会自动复制到VM中,并替换其中的默认密钥。 还将私钥放置在项目文件夹中名为“ .vagrant”的新文件夹中。 如果您在该文件夹中向下钻取,则会找到“ private_key”,这对于以后在PuTTY中使用很有用。
Next notice it checks for guest additions and mounts the shared folder between host and guest. This can be changed as you wish, or additional shared folders can be added. The first folder “/vagrant” is on the VM; it will share to my local project folder. Files will be kept in sync between the folders as you work.
下一步注意,它检查来宾添加,并在主机和来宾之间安装共享文件夹。 您可以根据需要进行更改,也可以添加其他共享文件夹。 第一个文件夹“ / vagrant”在VM上; 它将共享到我的本地项目文件夹。 工作时,文件将在文件夹之间保持同步。
现在我可以访问服务器了,对吗? (Now I Can Access my Server, Right?)
Yes! Kind of!
是! 有点儿!
You see, Vagrant has to be able to log in to the new server and perform additional provision, such as setting the configured IP, port forwarding, folder shares, VirtualBox Guest Additions, etc. In order to do this, all base boxes are required to have the Vagrant public key already installed, along with a user named “vagrant”, with password-less sudo privileges.
你看,流浪必须能够登录到新的服务器和执行其他规定,如设置配置的IP,端口转发,文件夹共享,VirtualBox的客户添置等。为了做到这一点,我们要求所有的基地盒并已安装具有无密码sudo权限的Vagrant公钥以及名为“ vagrant”的用户。
Once the VM is created, Vagrant will provide some output which you can scan for errors. See the previous screenshot. Most base box installs won’t have problems, but advanced scripted provisions can sometimes run into trouble and will have hundreds of lines of text output during the provisioning process.
创建虚拟机后,Vagrant将提供一些输出,您可以扫描这些输出以查找错误。 请参阅上一个屏幕截图。 大多数基本盒安装不会有问题,但是高级脚本设置有时会遇到麻烦,并且在设置过程中会输出数百行文本。
The problem we face now is that Windows doesn’t come with an SSH command line client. This means the built-in SSH functions of Vagrant won’t necessarily work for us Windows users. But that’s why we have PuTTY. Type this to get your SSH info whenever you need it:
我们现在面临的问题是Windows没有附带SSH命令行客户端。 这意味着Vagrant的内置SSH功能不一定对我们的Windows用户有效。 但这就是为什么我们有腻子。 键入此命令可在需要时获取SSH信息:
vagrant ssh-config
It will tell you the IP and SSH port to use and a few other bits of info in case you forget.
它将告诉您要使用的IP和SSH端口以及其他一些信息,以防万一您忘记了。
Even though many base boxes are barebones OSs, they do require an SSH server to be installed as one of the requirements of being usable by Vagrant. Many base boxes will have nothing but OpenSSH installed.
即使许多基本系统都是准系统,但作为Vagrant可以使用的要求之一,它们确实需要安装SSH服务器。 许多基地框将没有什么,但安装OpenSSH的。
Open PuTTY, enter the IP and port, give the connection a name, and save it. You can now connect to the new server over SSH with PuTTY. The username is “vagrant” and the password should be “vagrant” as well, but this can be individualized to the base box in special cases.
打开PuTTY,输入IP和端口,为连接命名,然后保存。 现在,您可以使用PuTTY通过SSH连接到新服务器。 用户名是“ vagrant”,密码也应该是“ vagrant”,但是在特殊情况下,可以将其个性化到基本框中。
Vagrant will automatically choose another port if the default 2222 is taken. All your VMs will get a new port, so be sure to note which VM is which!
如果采用默认值2222,Vagrant将自动选择另一个端口。 您所有的VM都将获得一个新端口,因此请务必记下哪个VM!
Side note: You can install a Windows SSH client, such as Cygwin or MinGW. Even Git comes with an SSH client. Type
vagrant ssh
and see what happens! Linux and Mac users should already have SSH clients. If you don’t have a client, Vagrant will show you some options.旁注:您可以安装Windows SSH客户端,例如Cygwin或MinGW 。 甚至Git都带有SSH客户端。 输入
vagrant ssh
,看看会发生什么! Linux和Mac用户应该已经具有SSH客户端。 如果您没有客户,Vagrant将为您显示一些选择。
使用公钥和私钥 (Using Public and Private Keys)
You can use the new private key Vagrant created, as the private key was stored on your local machine. However, PuTTY cannot use the OpenSSH style key. This is why we downloaded PuTTYGen, so open that now.
您可以使用创建的新私钥Vagrant,因为私钥已存储在本地计算机上。 但是,PuTTY无法使用OpenSSH样式密钥。 这就是为什么我们下载PuTTYGen的原因,所以现在就打开它。
Click “Load” and browse to your project folder all the way to **\\.vagrant\machines\default\virtualbox**
. Switch to see “all files”, select “private_key” and click Open.
单击“加载”,然后浏览到项目文件夹,直到**\\.vagrant\machines\default\virtualbox**
。 切换到“所有文件”,选择“ private_key”,然后单击“打开”。
You can immediately click the “Save private key” button, select “Yes” to save without a passphrase. Name it something similar like “private_key_putty” in the same folder. This will create a file with a .PPK
extension. Close PuTTYGen and go back to PuTTY now.
您可以立即单击“保存私钥”按钮,选择“是”进行保存而无需输入密码。 在同一文件夹中将其命名为“ private_key_putty”。 这将创建一个扩展名为.PPK
的文件。 关闭PuTTYGen,然后立即返回PuTTY。
In PuTTY, load your connection to the new server or type it in again if you didn’t save it. Then go to Connection->SSH->Auth in the sidebar and click “Browse” to find the private key. You will see the PPK version but not the other one, since PuTTY doesn’t use that type. Now be sure to save your configuration so you can keep using it later.
在PuTTY中,将您的连接加载到新服务器,或者如果没有保存,请再次输入。 然后转到侧边栏中的Connection-> SSH-> Auth ,然后单击“浏览”以查找私钥。 您将看到PPK版本,但不会看到另一个版本,因为PuTTY不使用该类型。 现在,请确保保存您的配置,以便以后可以继续使用它。
With that done, the next time you open this connection and type “vagrant” as the user, it will automatically authenticate with the key and you’ll be logged in without having to type the password.
完成此操作后,下次打开此连接并以用户身份输入“ vagrant”时,它将自动使用密钥进行身份验证,并且无需登录即可登录。
You can take this a step further and have PuTTY auto-type the username as well. Go in your PuTTY config to Connection>Data and type “vagrant” into the “Auto-login username” field. Now when you open the saved connection, PuTTY will automatically type the username and log in with the private key for you!
您可以更进一步,让PuTTY也自动键入用户名。 在您的PuTTY配置中,转到“ 连接”>“数据 ”,然后在“自动登录用户名”字段中键入“ vagrant”。 现在,当您打开保存的连接时,PuTTY将自动键入用户名并使用私钥登录!
补充笔记 (Additional Notes)
The Vagrantfile is where all configuration starts. This file is meant to be managed by a version control system and is how you would share your development setup with a team. If everybody uses the same vagrant file, they’ll get the same VM setup with the same boxes and provisions. Note that scripts can be called external to this file, meaning some complex vagrant projects could have many files and folders included.
Vagrantfile是所有配置开始的地方。 该文件由版本控制系统管理,是与团队共享开发设置的方式。 如果每个人都使用相同的无用信息文件,他们将使用相同的框和配置获得相同的VM设置。 请注意,脚本可以在此文件外部调用,这意味着某些复杂的无所事事项目可能包含许多文件和文件夹。
You can use VirtualBox to see any VMs you’ve created, but you don’t actually have to open or use VirtualBox at all to use Vagrant. If you make certain changes to the VM from VirtualBox, there’s a chance Vagrant could lose the association to the VM.
您可以使用VirtualBox来查看已创建的所有VM,但实际上不必完全打开或使用VirtualBox即可使用Vagrant。 如果您通过VirtualBox对VM进行某些更改,Vagrant可能会失去与VM的关联。
Vagrant creates a shared folder between your project folder and a folder in your VM which is /vagrant
by default. Often boxes are used as development machines, so a common folder to also share is your Apache or Nginx www or public_html folder.
Vagrant在项目文件夹和VM中的文件夹之间创建一个共享文件夹,默认情况下为/vagrant
。 通常将盒子用作开发机器,因此要共享的一个常用文件夹是Apache或Nginx www或public_html文件夹。
NOTE: Vagrant forwards port 22 automatically so you can get in, but it doesn’t automatically forward a web port such as 80 in case you’re doing web dev work. You’ll have to open your Vagrantfile and uncomment the line which forwards port 80 to enable it. Just read the notes in the file for how. After editing, you’ll want to do a
vagrant reload
.注意:Vagrant自动转发端口22,以便您可以进入,但如果您正在执行Web开发工作,它不会自动转发Web端口(例如80)。 您必须打开Vagrantfile并取消注释转发端口80的行以启用它。 只需阅读文件中的注释即可。 编辑后,您将需要进行
vagrant reload
。
You can turn the VM off by using vagrant halt
, or suspend it with vagrant suspend
. Then turn it on again any time with vagrant up
. Type vagrant status
to see the current state of the VM.
您可以使用vagrant halt
关闭虚拟机,也可以使用vagrant halt
将其vagrant suspend
。 然后再次打开它使用任何时间vagrant up
。 输入vagrant status
以查看虚拟机的当前状态。
All commands can be found here.
所有命令都可以在这里找到。
TIP: Check out Vagrant Manager for a visual System Tray tool to manage your local VMs. Also available for Mac users.
提示: 签出Vagrant Manager以使用可视化的系统任务栏工具来管理本地VM。 也适用于Mac用户。
额外的安全性 (Additional Security)
Because anybody can create base boxes, the Vagrant rules state that they should create a user and password vagrant/vagrant. It also states that it doesn’t expect the root user to have any password — that is, it doesn’t use root or expect a password for it. It would be good practice, if you feel the need, to know what the default users and password are on the box you choose to install.
因为任何人都可以创建基本框,所以Vagrant规则规定他们应该创建用户和密码Vagrant / Vagrant。 它还指出,它不希望root用户拥有任何密码-也就是说,它不使用root或不希望有密码。 如果需要,最好选择知道要安装的包装盒上的默认用户名和密码。
The rules also state that Vagrant needs password-less sudo on the vagrant user so that it can perform the setup, so you should be aware of this if you’re trying to increase security.
规则还指出,Vagrant需要对流浪用户使用无密码的sudo才能执行设置,因此,如果要提高安全性,则应注意这一点。
Vagrant sets SSH ports to bind to localhost, so it won’t accept connections from the “outside”. This can be changed if you wanted access to your VM from some other host somewhere else. You would need additional configuration to do this.
Vagrant将SSH端口设置为绑定到本地主机,因此它将不接受来自“外部”的连接。 如果要从其他地方的其他主机访问VM,可以更改此设置。 您将需要其他配置才能执行此操作。
潜在问题 (Potential Problems)
Based on feedback, there have been some issues using Vagrant. To be brief, here are a few points to consider if you have issues:
根据反馈,使用Vagrant存在一些问题。 简而言之,如果有问题,请考虑以下几点:
- Make sure Vagrant can communicate with secure websites over https. The only thing that might get in the way here are firewalls or limited user accounts. 确保Vagrant可以通过https与安全网站进行通信。 唯一可能造成问题的是防火墙或受限的用户帐户。
- Your PC’s BIOS may have settings for enabling virtualization that must be on. You’ll have to find your motherboard manual to see where these settings would be. Most modern computers for many years now will have defaults set up to enable virtualization. 您PC的BIOS可能具有启用虚拟化的设置,该设置必须处于启用状态。 您必须找到主板手册才能查看这些设置在哪里。 多年以来,大多数现代计算机现在都已设置默认值以启用虚拟化。
- I personally had an issue recently where the latest version of VirtualBox didn’t work with the latest Vagrant. I had to install the prior version of VirtualBox. You could try a previous version in a pinch if nothing else is working. 我个人最近遇到了一个问题,即最新版本的VirtualBox无法与最新的Vagrant一起使用。 我必须安装VirtualBox的先前版本。 如果没有其他效果,则可以尝试一下以前的版本。
If Vagrant has trouble communicating with Atlas for base boxes, you may need the Microsoft Visual C++ 2010 Redistributable Package. This issue is older and may have been fixed now but you can read about it on GitHub here.
如果Vagrant无法与Atlas进行基本箱通信,则可能需要Microsoft Visual C ++ 2010可再发行组件包。 这个问题是旧的,而且可能已经已经得到解决,但你可以在GitHub上读到它在这里 。
Advanced Linux file system attributes may not work well, such as symlinks and certain permissions. There was a problem with shared folders and long file paths (such as what NPM creates), but this was fixed in later versions of Vagrant. Read about it here.
高级Linux文件系统属性可能无法正常工作,例如符号链接和某些权限。 共享文件夹和长文件路径(例如NPM创建的文件)存在问题,但是在更高版本的Vagrant中已解决此问题。 在这里阅读。
下一步? (Next Steps?)
Now that you’ve learned how Vagrant works, you’ll want to go further. You can find additional boxes apart from Vagrant Cloud, such as vagrantbox.es. Using a box from another source is as simple as typing vagrant init box/name url
where the ‘box/name’ and ‘URL’ will be provided for you.
既然您已经了解了Vagrant的工作原理,那么您将需要走得更远。 除了Vagrant Cloud之外,您还可以找到其他框,例如vagrantbox.es 。 使用来自另一个来源的盒子就像输入vagrant init box/name url
一样简单,其中将为您提供“盒子/名字”和“ URL”。
George Fekete covered how to create your own base box.
乔治·费克特(George Fekete)介绍了如何创建自己的底盒 。
You can even use DigitalOcean as a provider and deploy droplets using Vagrant.
您甚至可以将DigitalOcean用作提供程序,并使用Vagrant部署Droplet。
Automation tools like PuPHPeT and Protobox help you create the provisioning scripts for deploying more full-featured development VMs.
诸如PuPHPeT和Protobox之类的自动化工具可帮助您创建配置脚本,以部署更多功能齐全的开发VM。
You may have heard of the popular Vagrant box Laravel Homestead which can be used to deploy a development server for Laravel projects. SitePoint’s Bruno Skvorc has built off of Homestead to create what he calls Homestead Improved with some alternate configuration for web development. This box is often used in tutorials on SitePoint.
您可能已经听说过流行的Vagrant盒子Laravel Homestead ,该盒子可用于为Laravel项目部署开发服务器。 SitePoint的Bruno Skvorc建立在Homestead的基础上,通过一些用于Web开发的替代配置来创建他所谓的Homestead Improvement 。 该框通常在SitePoint上的教程中使用。
Read Bruno’s Re-introducing Vagrant article for deeper information regarding web development tooling and the LAMP stack using Vagrant.
阅读Bruno的Re-introducing Vagrant文章,以获得有关Web开发工具和使用Vagrant的LAMP堆栈的更深入的信息。
There’s a box called VVV which not only creates a development box for WordPress devs, but actually sets it up so you can host multiple WordPress sites on one VM. Homestead does the same thing, so that you aren’t using one entire VM for each project you work on. Aleksander Koko wrote about VVV as well.
有一个名为VVV的框,它不仅为WordPress开发人员创建一个开发框,而且进行了实际设置,因此您可以在一个VM上托管多个WordPress网站。 Homestead会做同样的事情,因此您不会为每个工作项目都使用一个完整的VM。 Aleksander Koko也撰写了有关VVV的文章 。
The sky’s the limit, so jump in and look around for cool boxes, provisioning scripts, or create your own!
天空是极限,所以跳进去四处看看很酷的盒子,置备脚本或创建自己的盒子吧!
I hope that helps get you started! Let us know in the comments if you finally took the plunge, and if it’s working for you.
希望对您有所帮助! 请在评论中让我们知道您是否终于尝试了,是否对您有用。
翻译自: https://www.sitepoint.com/getting-started-vagrant-windows/
vagrant入门