vagrant 启动_立即启动VAGRANT并运行

vagrant 启动

This is a quick getting started tutorial for Vagrant to help you get your foot in the door. The official documentation is fantastic at getting you started as well, but this article is geared more towards the absolute beginner and will cut directly to the chase on certain things.

这是Vagrant的快速入门教程,可帮助您入门。 官方文档也很适合您入门,但是本文更适合绝对的初学者,并且将直接针对某些事情。

This in no way is a replacement for referencing their documentation, and I encourage anyone who reads this to immediately read the docs after. So, with that being said, read this article if:

这绝不是参考他们的文档的替代品,我鼓励阅读此文档的任何人在之后立即阅读文档。 因此,话虽如此,请阅读以下文章:

  • You want to learn what the heck Vagrant is and does.

    您想了解到底是什么和做什么。
  • You want a low-level understanding of why Vagrant will supercharge your development environment.

    您想对为什么Vagrant会增强您的开发环境有一个低级的了解。
  • You quickly want to be able to boot up a local PHP 5.4 development environment.

    您很快就希望能够启动本地PHP 5.4开发环境。
Warning! Although still very, very relevant, this article is old! Some things might not work as expected with new Vagrant updates. The quickest, easiest, and most reliable way to get a LAMP stack on your maching is with our new Vagrant Box, 警告! 尽管仍然非常非常相关,但这篇文章很旧! 对于新的Vagrant更新,某些事情可能无法正常工作。 在您的机器上获得LAMP堆栈的最快,最简单,最可靠的方法是使用我们的新型Vagrant Box Scotch Box. 苏格兰威士忌盒
Heads Up! If you're looking for a tutorial on configuring Vagrant with Chef or Puppet, this article isn't for you. This article also expects you to have a basic understanding of the command line and Git. Lastly, the instructions here are for Mac users, but it should be very similar for both PC and Linux users. 小心! 如果您正在寻找有关使用Chef或Puppet配置Vagrant的教程,那么本文不适合您。 本文还希望您对命令行和Git有一个基本的了解。 最后,此处的说明适用于Mac用户,但适用于PC和Linux用户。

Vagrant is a tool to "Create and configure lightweight, reproducible, and portable development environments."

Vagrant是用于“创建和配置轻量级,可复制且可移植的开发环境”的工具

In laymen terms, this means Vagrant takes all the headache out of setting up a local development environment and replaces it with almost a single config file where you can pick and choose what features you want your server to have. No longer are you stuck using XAMMP, configuring virtual environments manually, or working remotely in your editor via FTP.

用外行术语来说,这意味着Vagrant摆脱了设置本地开发环境的所有麻烦,并用几乎一个配置文件代替了它,您可以在其中选择并选择服务器希望具有的功能。 您再也不用使用XAMMP,手动配置虚拟环境或通过FTP在编辑器中远程工作了。

Vagrant is super powerful, and it is a lot more extensive than what this article will cover or where my understanding goes. This article will cover some pretty cool things with Vagrant, such as:

Vagrant具有超强的功能,并且比本文介绍的内容或我的理解范围要广泛得多。 本文将介绍Vagrant的一些非常酷的功能,例如:

  • Getting a PHP 5.4 LAMP stack running locally on your machine.

    获取在计算机上本地运行PHP 5.4 LAMP堆栈。
  • How to easily access the database of your virtual machine.

    如何轻松访问虚拟机的数据库。
  • How to automatically update your hostfile to reference the virtual server with pretty URLs (developing on http://localhost:8080 versus http://myprojectname.local).

    如何自动更新主机文件以使用漂亮的URL引用虚拟服务器(在http:// localhost:8080和http://myprojectname.local上开发)。
  • Some essential Vagrant commands.

    一些基本的Vagrant命令。
  • The best and most useful Vagrant plugins.

    最好,最有用的Vagrant插件。
  • Links to great resources and prebuilt Vagrant stacks.

    链接到大量资源和预建的Vagrant堆栈。

什么是流浪汉,为什么我还要关心? (What is Vagrant and why should I even care?)

简单原因: (Simple Reason:)

Developing locally rocks. Vagrant is quick, easy, and helps you manage multiple development environments at one time.

发展本地岩石。 Vagrant快速,轻松,可帮助您一次管理多个开发环境。

长原因: (Long Reason:)

Imagine you're developing an app with a team of say 15 people. This app is crazy awesome! It uses the Laravel PHP Framework, Redis and Memcached, Imagemagick and GD PHP Modules, curl, MySQL, PostgreSQL, and even MongoDB. On top of this, Laravel has the specific requirements of needing PHP version 5.3.7 or higher and the MCrypt PHP Extension.

假设您要与一个由15个人组成的团队一起开发应用程序。 这个程序太棒了! 它使用Laravel PHP框架,Redis和Memcached,Imagemagick和GD PHP模块,curl,MySQL,PostgreSQL甚至MongoDB。 最重要的是,Laravel有需要PHP版本5.3.7或更高版本以及MCrypt PHP扩展的特定要求。

Ideally, you're going to want all 15 people on the team working on this app to have as identical development environemts. Not all the developers on the team are going to have the expertise of Sysops or being a System Admin. Getting the development environments identically setup can be a huge undertaking. On top of that, some people use Mac while others Use Linux or Windows. Before you know it, developers will be throwing computers through walls exhausted from constantly configuring and configuring.

理想情况下,您将希望该应用程序团队中的所有15个人拥有相同的开发环境。 并非团队中的所有开发人员都将拥有Sysops的专业知识或成为系统管理员。 以相同的方式设置开发环境可能是一项艰巨的任务。 最重要的是,有些人使用Mac,而其他人则使用Linux或Windows。 在不知不觉中,开发人员将不断地进行配置和配置,使计算机陷入瘫痪。

Vagrant manages all this for you so everyone can focus on coding rather than maintaining their development environement.

Vagrant可以为您管理所有这一切,因此每个人都可以专注于编码而不是维护其开发环境。

Then, let's say half-way through the project, it's discovered that you need Beanstalkd installed on the server to start processing queues. Normally, everyone on the team will have to reconfigure their environments to use Beanstalkd hoping that it works. With Vagrant, you'll push an update to the configuration file, and everyone will just need to reload their Vagrant boxes. Boom! Everyone can now start implementing queues in their code. It doesn't matter if they're on Windows, Linux, or a Mac, everyone's development environment remains identical.

然后,假设在项目进行到一半时,发现需要在服务器上安装Beanstalkd才能开始处理队列。 通常,团队中的每个人都必须重新配置他们的环境以使用Beanstalkd,希望它能起作用。 使用Vagrant,您可以将更新推送到配置文件,每个人都只需要重新加载其Vagrant框即可。 繁荣! 现在,每个人都可以开始在其代码中实现队列。 无论是在Windows,Linux还是Mac上,每个人的开发环境都相同。

提供者和供应者 (Providers and Provisioners)

When I reference that Vagrant manages all of this for you, that's not completely true. Vagrant works with "Providers" and "Provisioners" to help create these development environments. Providers, such as VirtualBox, VMWare, Amazon AWS, and Digital Ocean, are the services where your virtual environment will be created and hosted. If it be just on your local machine with VirtualBox or VMWare, or alternatively, if it be with Amazon or Digital Ocean to quickly deploy these virtual environments into the cloud.

当我提到Vagrant为您管理所有这一切时,那不是完全正确的。 Vagrant与“ Providers”和“ Provisioners”一起工作,以帮助创建这些开发环境。 提供程序(例如VirtualBox,VMWare,Amazon AWS和Digital Ocean)是将在其中创建和托管虚拟环境的服务。 如果仅在具有VirtualBox或VMWare的本地计算机上,或者与Amazon或Digital Ocean一起使用,则可以将这些虚拟环境快速部署到云中。

Provisioners are tools that allow you to quickly configure your server to your exact requirements. These are great to use because they automate the sometimes daunting process of setting up a server through configuration before the server is even created. You can configure nearly everything to be installed on the server. Some examples are PHP, PHP Modules, Apache, Git, Vim, databases, logins, Xdebug, etc. The two most common provisioners used with Vagrant are
Puppet and Chef. The provisioners are really neat to use because when you need to build your staging or production environments, you can use these exact same blue prints to ensure you have perfectly synced environments between development, staging, and production.

PuppetChef 。 供应商真的很整洁,因为当您需要构建暂存或生产环境时,可以使用这些完全相同的蓝图来确保开发,暂存和生产之间的环境完全同步。

足够! 让我们启动并运行一个LAMP (Enough! Let's get a LAMP stack up and running)

This is a step-by-step guide on setting up Vagrant on your Mac. I find that I get incompatibility errors sometimes based on the combined OS, Vagrant version, Chef versions, and even the virtual box version. I am in no way an expert, so these errors might be a lot more minor and fixable than I make them out to be. This also may seem like a lot of points for failure, but when it all works together it is absolutely astounding. Plus, Vagrant support is fantastic and their developers (beast developer?) push fixes and updates all the time. I suggest being exceptionally cautious when upgrading any of the moving parts with Vagrant, especially after you have it working perfectly for you or your organization's purpose. Your development environment is your baby!

这是在Mac上设置Vagrant的分步指南。 我发现有时会由于操作系统,Vagrant版本,Chef版本甚至虚拟盒版本的组合而出现不兼容错误。 我绝不是专家,因此这些错误可能比我指出的错误要小得多并且可以修复。 这似乎也有很多失败的要点,但是当它们一起工作时,绝对令人震惊。 另外, Vagrant的支持非常棒,他们的开发人员( 野兽开发人员? )一直在进行修复和更新。 我建议在使用Vagrant升级任何活动部件时要格外谨慎,尤其是在使其完全适合您或组织的目的之后。 您的开发环境就是您的宝贝!

安装必要的依赖项 (Install Necessary Dependencies)

Note: When this was written, I was currently using Vagrant version 1.4, VirtualBox 4.3.4, and Ruby 1.9 on Mac OS X Mavericks 10.9. 注意:撰写本文时,我目前在Mac OS X Mavericks 10.9上使用Vagrant版本1.4,VirtualBox 4.3.4和Ruby 1.9。

After Vagrant is installed, pop open your terminal to verify it's installed. You can do this simply by typing this in your terminal:

安装Vagrant后,请弹出您的终端以确认其已安装。 您只需在终端中输入以下内容即可:

vagrant -v

The Vagrant installer should automatically add Vagrant to your system's PATH. You should see something similar to the image below and Vagrant is now successfully installed on your system.

Vagrant安装程序应将Vagrant自动添加到系统的PATH中。 您应该看到与下图类似的图像,并且Vagrant现在已成功安装在系统上。

Vagrant Version Command Line Earlier we discussed the Chef provisioner. The example we're going through will use that instead of Puppet. There is a tool called
Berkshelf that helps manage Chef's cookbooks (PHP, PHP Modules, Apache, Git, Vim, databases, logins, Xdebug, etc.). The example also uses this. Vagrant or Chef don't require Berkshelf, but it's nice to have in case the Vagrant instance you use or build will take advantage of it. You can install it with the following command on Mac:

Berkshelf的工具,可以帮助管理Chef的食谱(PHP,PHP模块,Apache,Git,Vim,数据库,登录名,Xdebug等)。 该示例也使用此方法。 Vagrant或Chef不需要Berkshelf,但如果您使用或构建的Vagrant实例可以利用它,那就很好了。 您可以在Mac上使用以下命令安装它:

gem install berkshelf

安装有用的Vagrant插件 (Install Useful Vagrant Plugins)

The plugins we will be installing are Vagrant Berkshelf, Vagrant Hostmanager, and Vagrant Omnibus. The Vagrant Berkshelf plugin allows Vagrant to communicate with the Berkshelf cookbook manager we just installed.

我们将要安装的插件是Vagrant BerkshelfVagrant HostmanagerVagrant Omnibus 。 Vagrant Berkshelf插件允许Vagrant与我们刚安装的Berkshelf食谱管理器进行通信。

The Vagrant Hostmanager plugin lets you add a configuration to Vagrant to automatically update your hostfile and point it to the IP in use. I really enjoy this plugin because it allows you to develop on a pretty URL such as "http://myproject.local" versus "172.22.22.22" or the IP in use.

Vagrant Hostmanager插件可让您向Vagrant添加配置,以自动更新主机文件并将其指向使用中的IP。 我非常喜欢这个插件,因为它允许您在漂亮的URL上进行开发,例如“ http://myproject.local”与“ 172.22.22.22”或使用的IP。

The Vagrant Omnibus plugin ensures the desired version of Chef is installed. The example we will be using doesn't use this, but I find this plugin super useful for debugging when Chef related issues come up. I believe something like this might be baked right into Vagrant core one day.

Vagrant Omnibus插件可确保安装所需版本的Chef。 我们将使用的示例未使用此示例,但是当出现与Chef相关的问题时,我发现此插件对于调试非常有用。 我相信这样的事情可能有一天会被放到Vagrant的核心中。

Installing Vagrant plugins is a breeze once Vagrant is installed. In your terminal simply type:

一旦安装了Vagrant,安装Vagrant插件就变得轻而易举。 在您的终端中,只需键入:

vagrant plugin install vagrant-berkshelf
vagrant plugin install vagrant-hostmanager
vagrant plugin install vagrant-omnibus

克隆无用的LAMP堆栈 (Clone a Vagrant LAMP Stack)

You could build your own LAMP Stack and configurations, but for simplicity and the purpose of this tutorial, we'll just be using an already built one by this awesome developer. I've forked and modified the repo to get it working with my version of Vagrant, Mavericks, and VirtualBox.

您可以构建自己的LAMP堆栈和配置,但是为了简单起见和本教程的目的,我们将只使用由这位了不起的开发人员构建的。 我已经分叉并修改了存储库,以使其能够与我的Vagrant,Mavericks和VirtualBox版本一起使用。

Navigate to the folder you want your project to be at in your terminal and do:

导航到您希望项目位于终端中的文件夹,然后执行以下操作:

git clone https://github.com/scotch-io/Vagrant-LAMP-Stack.git myfirstvagrantproject

Next, navigate to the folder and start vagrant:

接下来,导航到该文件夹​​并开始流浪汉:

cd myfirstvagrantproject
vagrant up

At this point Vagrant will start building a virtual environment based on the configuration file called "Vagrantfile". If you want for future projects, you can go inside of here and set some useful config options that this stack has. The options I usually change are:

此时,Vagrant将开始基于名为“ Vagrantfile”的配置文件构建虚拟环境。 如果您想要将来的项目,则可以进入此处并设置此堆栈具有的一些有用的配置选项。 我通常更改的选项是:

# IP Address for the host only network, change it to anything you like
# but please keep it within the IPv4 private network range
ip_address = "172.22.22.22"

# The project name is base for directories
# Will also be the hostname for your project (e.g.: http://projectname.local)
project_name = "projectname"

After Vagrant has successfully completed, open up your browser and navigate to http://projectname.local or 172.22.22.22 to connect to your local environment (note that the image URL below is not correct for this tutorial and from an older version):

Vagrant LAMP Stack That's it! You're now up and running with Vagrant. To edit files and start developing, just navigate to the public folder and begin coding as you normally would. Any changes made on your computers public folder are automatically shared to the virtual environment.

Vagrant成功完成后,打开浏览器并导航到http://projectname.local172.22.22.22以连接到本地环境(请注意,以下图像URL不适合本教程,也不适用于较旧的版本):

流浪汉灯栈 而已! 您现在可以在Vagrant上运行了。 要编辑文件并开始开发,只需导航到公用文件夹并按照通常的方式开始编码。 您对计算机公用文件夹所做的任何更改都会自动共享到虚拟环境。

Vagrant and VirtualBox

访问数据库 (Accessing the Database)

From the example above, phpMyAdmin will not be installed. You can install phpMyAdmin through the provisoner if you prefer though. I prefer accessing the database through a desktop client better, however. I use both Sequel Pro or Navicat interchangeably. Connecting to the database is easy and the credentials in this example are available in both the Vagrantfile and the example index.php file located in the public folder. To connect with PHP, use these credentials:

从上面的示例中,将不会安装phpMyAdmin。 如果愿意,您可以通过provisoner安装phpMyAdmin。 但是,我更喜欢通过桌面客户端访问数据库。 我可以同时使用Sequel ProNavicat 。 连接到数据库很容易,并且此示例中的凭据在Vagrantfile和公共文件夹中的示例index.php文件中均可用。 要连接PHP,请使用以下凭据:

Host:       localhost
User:       root
Password:   root
Port:       3306

Here's what the credentials will look like when connecting to from Sequel Pro:

从Sequel Pro连接时,凭据如下所示:

MySQL Host: 172.22.22.22 (or the IP used)
User:       root
Password:   root
Port:       3306

Depending on thow the Vagrant file was configured, you might not be able to connect this way since it's considered remotely accessing the database. You can still connect with a desktop client by doing a nice SSH port forwaring trick. You'll more about SSH in the next section.

根据配置的Vagrant文​​件的不同,您可能无法以这种方式连接,因为它被认为是远程访问数据库。 您仍然可以通过执行漂亮的SSH端口警告技巧来连接桌面客户端。 在下一部分中,您将进一步了解SSH。

MySQL Host: localhost or 127.0.0.1
User:       root
Password:   root
Port:       3306
SHH Host:   172.22.22.22
SSH User:   vagrant
SSH Key:    ~/.vagrant.d/insecure_private_key (or your path to the private key)

Vagrant Database Note that these credentials come directly from running
vagrant ssh-config.

vagrant ssh-config

游民命令 (Vagrant Commands)

There's a ton of commands you can use to talk to Vagrant. For a full list see the official docs here, but I'll go over the more common ones.

您可以使用大量命令与Vagrant对话。 有关完整列表,请参见此处官方文档 ,但我将介绍更常见的文档

无所事事 (vagrant up)

vagrant up This is the command you just ran to boot the box up and provision the server based on the configuration file Vagrantfile. I use this for starting as well as resuming my environments.

vagrant up这是您刚用来启动设备并根据配置文件Vagrantfile设置服务器的命令。 我使用它来启动和恢复我的环境。

无业游民暂停 (vagrant suspend)

vagrant suspend Use this command to "pause" your virtual environment. Make sure you do this before shutting down your computer to safely be able to restore the environment later.

vagrant suspend使用此命令可以“暂停”您的虚拟环境。 确保在关闭计算机之前执行此操作,以便以后能够安全地还原环境。

Starting, Pausing, and Resuming: To start or restore the environment you can just run 启动,暂停和恢复:要启动或恢复环境,您只需运行 vagrant up. The state will be saved and everything should be where you left it off from including your database changes. vagrant up 。 状态将被保存,并且所有内容都应保留在其中(包括数据库更改)。

无业游民的破坏 (vagrant destroy)

vagrant destroy This permanently removes the virtual environment from your machine.

vagrant destroy这将永久性从您的计算机中删除虚拟环境。

流浪者重装 (vagrant reload)

vagrant reload and vagrant reload --provision If your environment suddenly stops working, it's useful to reboot by running this command. If you add the --provision flag, it will reprovision the box as well. This is useful with removing or adding things to the server via Cookbooks or Puppet.

vagrant reloadvagrant reload --provision如果您的环境突然停止工作,则通过运行此命令来重新引导很有用。 如果添加--provision标志,则也会重新设置该框。 这对于通过Cookbooks或Puppet将内容删除或添加到服务器很有用。

流浪的SSH (vagrant ssh)

vagrant ssh Anything you edit on your computer in the public folder is automatically shared to the virtual environment without having to SSH in. However, if for whatever reason you need to connect to the virtual server, just run this command.

vagrant ssh您在计算机上的公用文件夹中vagrant ssh任何编辑都会自动共享到虚拟环境,而无需进行SSH。但是,如果出于某种原因需要连接到虚拟服务器,只需运行此命令。

流浪者ssh-config (vagrant ssh-config)

vagrant ssh-config This lets you see the detailed login credentials of how you could connect to the virtual environment.

vagrant ssh-config这使您可以查看有关如何连接到虚拟环境的详细登录凭据。

总结,调试和资源 (Wrap-Up, Debugging, and Resources)

That's all it takes to get up and running with Vagrant. Developing locally, having easy database access like this, being able to provision virtual servers from Chef or Puppet configurations, and maintaining a universal development environment for everyone can be extremely beneficial to workflow. One of the added pluses of using these provisioners are that you could technically use them anywhere to copy environments to say a staging or production server. This way you have identical development, staging, and production environments as well.

这就是使用Vagrant进行启动和运行所需的全部工作。 在本地进行开发,具有像这样的轻松数据库访问权限,能够从Chef或Puppet配置中配置虚拟服务器,以及为每个人维护通用的开发环境,对工作流程极为有利。 使用这些资源调配器的另一个好处是,您可以从技术上在任何地方使用它们来复制环境,例如临时服务器或生产服务器。 这样,您也具有相同的开发,登台和生产环境。

If you are having problems getting this up and running, here are some other Vagrant LAMP stacks you can try out are:

如果您在启动和运行时遇到问题,可以尝试以下其他Vagrant LAMP堆栈:

If you have questions, I'll be happy to help as much as possible. Otherwise, try these resources for support:

如果您有任何疑问,我们将竭诚为您服务。 否则,请尝试以下资源以获得支持:

If you're dancing circles around this, you'll probably want to go start building and provisioning your own Vagrant boxes that work for you or your organization.

如果您对此感兴趣,那么您可能需要开始构建并配置自己的Vagrant框,以适合您或您的组织。

Anyways, I really appreciate anyone who spent the time to read this! I hope you enjoy using Vagrant as much as I do. Goodluck!

无论如何,我真的很感谢那些花时间阅读本文的人! 希望您能像我一样喜欢使用Vagrant。 祝好运!

翻译自: https://scotch.io/tutorials/get-vagrant-up-and-running-in-no-time

vagrant 启动

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值