文档结构  
翻译进度:63%     翻译赏金:0 元 (?)    ¥ 我要打赏

setting up a vm on the fly

受欢迎的这篇文章在2017年被完全更新。变化包括公钥信息, 故障排除技巧,和针对Windows 10和其他相关软件的更新。

Vagrant已迅速成为普及的横跨Mac, Windows, 和inux操作系统的本地开发首选工具。

作者更多文章

Vagrant可以帮助你通过一组可重用配置文件来动态的创建虚拟机。开发者可以通过GitHub和其他方式分享他们的配置和脚本,因此其他开发这可以运转起一个相同的环境和工具。

第 1 段(可获 1.25 积分)

如果你想在启动服务器群来测试程序,学习如何使用Linux工具,或是在生产环境做变更前在测试环境中测试那么这是一个很好的工具。 你想学习在Ubuntu上从无到有安装PHP/Apache/MySQL栈么? 或是玩转一下设置缓存服务器就像Apache的前端Varnish? 想亲手试试Nginx么? 学习下PHP 7? Vagrant让比单独使用VirtualBox做这些事情更加的简单。

Vagrant logo

让我们看一看如何让在Windows环境中设置Vagrant。

安装中的要点和碎碎念

在正式开始前, 先安装这些核心的工具:

第 2 段(可获 1.43 积分)

VirtualBox和Vagrant的安装和其他Windows程序一样。Vagrant会将自己安装到全局路径中因此你可以在任何地方运行它。

创建你的项目

现在你已经把一切都建立起来了,你可以通过创建一个包含不同虚拟机配置的项目文件夹来开始你的首个Vagrant项目。你将会在这些目录中使用命令行来运行Vagrant命令。

第 3 段(可获 1.29 积分)

创建项目文件夹,我使用的是 E:\Vagrant\sitepoint.

小提示: 作为一个Windows用户,你可以通过按住shift和右击文件夹或文件夹内的空白之后选择“此处打开命令窗口”来快速从资源管理器打开一个命令提示行。

使用Vagrant

从主要命令vagrant initvagrant up开始

打开一个命令行并更改目录到你的测试项目文件夹。 运行 vagrant init将会在当前目录新建一个拥有基本的起动器配置被称为“Vagrantfile” (无扩展名)的vagrant文件。

第 4 段(可获 1.05 积分)

在Notepad++或Sublime等文本编辑器中打开并查看Vagrantfile, 学习每个配置值之前的注释。你可以设置客户机和宿主机之间的共享文件夹, 打开端口转发,设置主机名, 等等。注意可能除了一行外所有的都被注释掉了: 即使在这没有更改任何设置,Vagrant都会使用一个默认配置运行。

这个Vagrantfile不指向任何类型的虚拟机,所以直接删除刚刚创建的vagrantfile,让我们找一个“基础box”来使用。

Vagrant Cloud上浏览你喜欢使用的基础boxes。对于这个示例,我们将使用“ubuntu/trusty64” 它是Ubuntu Server 14.04 LTS (Trusty Tahr)的官方”基础box“ 。要让Vagrant自动使用基础box,输入下列命令:

第 5 段(可获 1.74 积分)
vagrant init ubuntu/trusty64

这个时候,如果你打开Vagrantfile,你会注意到在“config.vm.box”中它列出了基础box。其他设置仍然被注释掉。

Vagrant init

现在你有一个包含有基础box配置的Vagrantfile了,你可以用这个神奇命令来运转虚拟机了:

vagrant up

Vagrant up

解释下这里发生了什么, 如果你还没有基础box,Vagrant会首先导入它(我已经有了, 否则它会首先去下载)。然后检查你的box是不是最新的。

注: 这些基础box在你的 %userprofile%/.vagrant.d/boxes文件夹中。通过输入vagrant box list你可以列出所有已安装box。你可以通过vagrant box remove box/name来删除boxes。

第 6 段(可获 1.33 积分)

你能看到它将SSH转发端口设置为“2222”了: 你在使用像PuTTY的SSH客户端时需要这个。

注意用户名是“vagrant”,始终都是它。你还会注意到它生成一组新的密钥,Vagrant在以前的版本中没这么做过。公钥被自动复制到虚拟机并把默认的替换掉。私钥也将放在项目文件夹里的一个新的被称作“.vagrant”的文件夹中。如果你深入到文件夹中你会发现“private_key”,它将在之后的PuTTY使用中用到。

接着注意检查客户端增强包和挂载宿主机和客户机间的共享目录。这个可以按照你的想法来更改,或着是添加额外的共享文件夹。第一个文件夹“/vagrant”在虚拟机上;它将与我的本地项目文件夹共享。在工作期间文件在文件夹间将保持同步。

第 7 段(可获 2.06 积分)

现在我可以访问服务器了,对吗?

是的! 稍等片刻!

你要明白,Vagrant必须能登录到新服务器并执行附加定制,例如设置IP配置, 端口转发,文件夹共享, VirtualBox客户端增强包等等。为了做到这一点, 所有基础box必须已经安装了Vagrant的公钥,连同一个名为“vagrant”的用户,并有无密码sudo权限。

一旦创建虚拟机,Vagrant将提供一些可以查看错误的输出。看之前的屏幕截图。大多数基础box安装不会有问题,但是运行高级的脚本定制有时会遇到麻烦并且在配置过程中会有数百行文本输出。

第 8 段(可获 1.5 积分)

我们现在面临的问题是Windows不带有SSH命令行客户端。这意味着Vagrant内置的SSH功能将不一定适用于我们Windows用户。但这就是为什么我们有PuTTY。在你需要时获取SSH信息时键入下列命令:

vagrant ssh-config

在你忘记的情况下它会告诉你使用的IP和SSH端口和一些其他信息。

尽管许多基础box只是准系统,但是作为Vagrant可用性的要求之一它们需要安装一个SSH Server。在许多基础box中除了OpenSSH将不装任何软件。

第 9 段(可获 1.36 积分)

打开PuTTY,输入IP和端口号, 为连接指定一个名称,并将其保存。你现在可以使用PuTTY通过SSH连接到新的服务器。用户名是“vagrant” 密码应该也是 “vagrant” , 但这个在特殊情况时可以为基础box定制。

Vagrant with PuTTY

Vagrant在默认2222端口被占用时会自动选择其他的端口, 你所有的虚拟机将得到一个新的端口,所以一定要注意哪个VM用哪个端口!

边注: 你可以安装一个Windows的SSH客户端, 如 Cygwin 或 MinGW。甚至Git附带一个SSH客户端。 输入vagrant ssh ,看看会发生什么! Linux和Mac用户应该已经有了SSH客户端。如果你没有客户端, Vagrant将向你展示一些选项。

第 10 段(可获 1.6 积分)

使用公钥和私钥

你可以使用Vagrant 创建的新的私钥Vagrant created了, 因为它已经保存在你的本地机器上。 然而,PuTTY 不能使用OpenSSH格式的私钥。这就是为什么我们下载PuTTYGen的原因, 那么现在打开它。

单击“Load” 并且一路浏览到你的项目文件夹 **\\.vagrant\machines\default\virtualbox**。切换到查看“all files”, 选中“private_key”并单击Open。

你可以立即点击“Save private key”按钮, 选中 “Yes” 来无密保存。取一个类似“private_key_putty” 的名字放在同一个文件夹中。 这将创建一个带.PPK扩展名的文件。现在关闭PuTTYGen并回到PuTTY。

第 11 段(可获 1.4 积分)

Save private key

在PuTTY,加载你的连接到新的服务器,如果你没有保存,你可以再次输入它,然后去到工具栏的 Connection->SSH->Auth中并按下"浏览"去找到私钥,你将可以看到PPK的类型而看不到其他,因为PuTTY不使用其他类型,现在确保保存了你的配置,以便你可以继续使用

Private key in PuTTY

下一次,你可以打开连接并选择"vagrant"使用,它会自动用密钥进行身份验证,你将登录而不用输入密码

你还可以再让PuTTY自动输入用户名,去到PuTTY的Connection>Data 并输入"vagrant"到"Auto-login username"的字段,现在打开你保存好的连接,PuTTY将会使用配置上的用户名和秘钥来帮你自动登录

第 12 段(可获 1.96 积分)

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.

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.

第 13 段(可获 1.55 积分)

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.

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.

第 14 段(可获 1.38 积分)

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.

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.

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.

第 15 段(可获 1.65 积分)

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 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.

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:

第 16 段(可获 1.28 积分)
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
第 17 段(可获 2.44 积分)

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.

George Fekete covered how to create your own base box.

You can even use DigitalOcean as a provider and deploy droplets using Vagrant.

Automation tools like PuPHPeT and Protobox help you create the provisioning scripts for deploying more full-featured development VMs.

第 18 段(可获 1.15 积分)

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.

Read Bruno’s Re-introducing Vagrant article for deeper information regarding web development tooling and the LAMP stack using Vagrant.

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.

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.

第 19 段(可获 2.23 积分)

文章评论