本文共 7398 字,大约阅读时间需要 24 分钟。
做无用之事读无用之书
Vagrant Boxes are prepackaged development environments that are the foundation of Vagrant. In most cases, this is usually just a stripped and naked operating system such as , , or . Boxes exist with the intention to be provisioned with additional features like Apache and PHP using tools like Chef or Puppet. This is really powerful, but it can be time consuming to setup the first configuration and difficult for beginners. Plus, not everyone has the skill-set of a system admin or work with a huge collaborative team.
Vagrant Boxs是预包装的开发环境,是Vagrant的基础。 在大多数情况下,这通常只是一个剥离的裸操作系统,例如 , 或 。 存在一些框,意在使用Chef或Puppet等工具为它们提供诸如Apache和PHP的附加功能。 这确实很强大,但是设置第一个配置可能很耗时,并且对于初学者来说很困难。 另外,并非每个人都具备系统管理员的技能或与庞大的协作团队一起工作。
This post will describe to you how to create your own prepackaged from and existing virtual machine. In my opinion, it's the quickest and easiest way for beginners to get started with Vagrant. This way you'll be able to reuse it over and over and even share it. I feel this generally goes against the main idea of Vagrant where you provision your development environments from a single config. You generally lose a lot of the debugging and configurability of your development environment, but this should be helpful for plenty of developers, teams, and projects regardless.
这篇文章将向您介绍如何从现有虚拟机创建自己的预包装的 。 我认为,这是初学者入门Vagrant的最快,最简单的方法 。 这样,您将能够反复使用它,甚至共享它。 我认为这通常与Vagrant的主要思想背道而驰,Vagrant的主要思想是从单个配置中配置开发环境。 通常,您会失去很多开发环境的调试和可配置性,但这无论如何对于许多开发人员,团队和项目都应该有所帮助。
The first thing you'll need to do is pick a box that you want to build from. The lets you easily find boxes that people have shared. We'll be working off of Hashicorp's (Vagrant's) Precise64. This box is a good one to work off of because Chef and Puppet are already installed, plus all the settings we're configured by the creator(s) of Vagrant.
您需要做的第一件事是选择一个要从中构建的框。 可让您轻松找到人们共享的盒子。 我们将使用Hashicorp(Vagrant)的Precise64。 此框是一个不错的选择,因为已经安装了Chef和Puppet,以及我们由Vagrant的创建者配置的所有设置。
After you've chosen a box, initialize the Vagrant box. Each one is slightly different, but here's how to do it for the example we're doing:
选择一个框后,初始化“ Vagrant”框。 每个示例都略有不同,但是在我们正在做的示例中,这是这样做的方法:
vagrant init hashicorp/precise64
This will also create a Vagrant file for you. Now, boot the box with Vagrant by doing (it will need to download if it's the first time using the it):
这也会为您创建一个Vagrant文件。 现在,用Vagrant引导盒子(如果是第一次使用它,则需要下载):
vagrant up
We'll now SSH into the box and start customizing it. If you don't know anything about servers, you can always just use since all of this is already done for you. Here's how to SSH into the box:
现在,我们将SSH放入框中并开始对其进行自定义。 如果您对服务器一无所知,您可以随时使用因为所有这些工作已经为您完成。 以下是SSH进入包装盒的方法:
vagrant ssh
Now, we need to setup our server by installing whatever we want on it. This example will install a basic LAMP stack, but you can do whatever you like (Ruby, Nginx, etc.). The point of this article isn't to teach you how to setup a server, but instead how to turn your virtual environment into a Vagrant Box. So this step might be than what is actually shown:
现在,我们需要通过在服务器上安装我们想要的任何东西来设置服务器。 此示例将安装基本的LAMP堆栈,但您可以执行任何喜欢的操作(Ruby,Nginx等)。 本文的重点不是要教您如何设置服务器,而是要教您如何将虚拟环境变成无所事事的盒子。 因此,此步骤可能比实际显示的要得多:
sudo apt-get updatesudo apt-get upgradesudo apt-get install vimsudo apt-get install apache2sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysqlsudo mysql_install_dbsudo /usr/bin/mysql_secure_installationsudo apt-get install php5 libapache2-mod-php5 php5-mcryptsudo apt-get install php5-cgi php5-cli php5-curl php5-common php5-gd php5-mysqlsudo service apache2 restart
We're now going to clean up disk space on the VM so when we package it into a new Vagrant box, it's as clean as possible. First, remove APT cache
现在,我们将清理虚拟机上的磁盘空间,以便将其打包到新的Vagrant盒中时,它尽可能地干净。 首先,删除APT缓存
sudo apt-get clean
Then, "zero out" the drive (this is for Ubuntu):
然后,将驱动器“清零”(这是Ubuntu的):
sudo dd if=/dev/zero of=/EMPTY bs=1Msudo rm -f /EMPTY
Lastly, let's clear the Bash History and exit the VM:
最后,让我们清除Bash历史记录并退出VM:
cat /dev/null > ~/.bash_history && history -c && exit
We're now going to repackage the server we just created into a new Vagrant Base Box. It's very easy with Vagrant:
现在,我们将刚创建的服务器重新打包到一个新的Vagrant Base Box中。 使用Vagrant非常简单:
vagrant package --output mynew.box
The previous command will have created a "mynew.box" file. You can technically put this wherever you want on your computer. Now, let's add this new Vagrant Box into Vagrant:
前面的命令将创建一个“ mynew.box”文件。 从技术上讲,您可以将其放置在计算机上的任何位置。 现在,让我们将此新的Vagrant Box添加到Vagrant中:
vagrant box add mynewbox mynew.box
This now will "download" the box into your Vagrant install allowing to initiate this from any folder, but before we do this, let's delete and remove the Vagrant file we built this box from.
现在,此操作会将框“下载”到您的Vagrant安装中,从而可以从任何文件夹中启动该框,但是在执行此操作之前,让我们删除并删除构建此框的Vagrant文件。
vagrant destroyrm Vagrantfile
We need to now initialize a Vagrant environment from our brand new box using the same command from earlier but referencing the new Box.
现在,我们需要使用与前面相同的命令引用新Box,从全新Box初始化Vagrant环境。
vagrant init mynewbox
When you initialize the Vagrant environment, it creates a Vagrantfile for you (just like from before). Open the Vagrantfile and delete everything. You can use what was in there as a reference or just use , but we're going to only use the absolute essentials that I've already spelled out for you.
初始化Vagrant环境时,它会为您创建一个Vagrantfile(就像之前一样)。 打开Vagrantfile并删除所有内容 。 您可以使用其中的内容作为参考,也可以只使用 ,但是我们将仅使用我已经为您阐述的绝对要领。
Now paste the following bare-bones code into your Vagrantfile:
现在,将以下基本代码粘贴到您的Vagrantfile中:
vim index.php
And add the following line to demo what we just installed with PHP:
并添加以下行以演示我们刚刚使用PHP安装的内容:
phpinfo();
Now, visit the IP address from the Vagrantfile in your browser. If everything works, you should see a PHP info dump of the index.php file you just created.
现在,从浏览器中的Vagrantfile访问IP地址。 如果一切正常,您应该会看到刚刚创建的index.php文件PHP信息转储。
Now that you've just created your own Vagrant Base Box from an existing one, you should consider sharing it on the . This way other people can download and use it!
现在,您已经从现有的基础上创建了自己的Vagrant Base Box,您应该考虑在上共享它。 这样其他人可以下载和使用它!
There are tons of way of building Vagrant Boxes, and this method is only intended to get you started in the easiest and fastest way. You should consider these additional resources if you want to build from scratch or use a service to help you with the process:
有大量的方式来创建Vagrant Box,而这种方法仅旨在使您以最简单,最快的方式入手。 如果要从头开始构建或使用服务来帮助您完成此过程,则应考虑以下其他资源:
Alternative, if you want to just focus on the code and still use Vagrant, you can use my Vagrant Base Box, .
或者,如果您只想专注于代码并仍然使用Vagrant,则可以使用我的Vagrant基本框 。
翻译自:
做无用之事读无用之书
转载地址:http://tjywd.baihongyu.com/