Building a development environment using Vagrant

Source: Internet
Author: User
Tags administrator password big blue button vmware fusion

Building a development environment using Vagrant

Summary: This article describes the use of Vagrant to build a unified development environment.

Problem

As a developer, the issues we typically face are:

    1. The development environment requires manual installation of the configuration, which includes versions and configurations of the operating system (CentOS, Ubuntu), PHP/HHVM, Python, node. js, MySQL, Apache/nginx, and so on.
    2. The problem cannot be reproduced.
      A: You said the question, why on my machine is not OK?
      B: But the problem really exists, do not believe you to my machine to see.
      A: What's the difference between my machine and your environment?
      Because the environment is different, for example, the development environment and production environment are different, and the test environment is not exactly the same.
    3. The team does not have a unified approach to configure the development environment, to maintain the same environment, good may have a document to tell you to download, install things, the lack of documentation may be asked everywhere.
    4. On the same machine, you need to configure different environments for the different projects involved, because the required software and version are different, and sometimes this is not possible at all.
    5. Different members of the team use different machines and operating systems, and it is difficult to guarantee the exact same environment.
Solution-the way of vagrant

Using a virtual organization to build a consistent environment and automating the installation and configuration process is what vagrant does. Follow the vagrant process:

    • Team members only need to check out code from source control and run vagrant up to have a complete development environment. Developers can still continue to use their familiar editors, browsers, and other tools on their machines, while code and databases and Web servers are running in vagrant virtual machines.
    • The system engineer only needs to prepare the vagrant configuration and the corresponding script to configure the virtual machine.
    • If you mess up the virtual machine, just use vagrant destroy to remove the virtual machine, then run vagrant up and you can get a pre-configured virtual machine.
    • After use, only need to use vagrant halt or vagrant suspend the virtual machine or hibernate, the next day with the vagrant up to restart the VM, restore to yesterday's state.
    • This management of virtual machines can be used not only in a project's team, but also across multiple projects. Using the same process and environment increases productivity, and there is no question of "clearly no problem on my machine".

This is the way of vagrant (see [5] on page 4th).

Vagrant can do the following things:

    • To log in to a virtual machine via SSH
    • Turn off the virtual machine
    • Remove virtual machines, completely remove virtual hard disks and configuration information
    • Pausing and resuming a virtual machine
    • Package a virtual machine so that it can be distributed to other developers

Vagrant the concepts involved:

    • Vagrant itself is not a virtual machine, it uses virtual machines through its suppliers (provider), supported virtual machines with Oracle's VirtualBox, VMware's VMware Fusion and VMware Workstation and Microsoft's Hyper-V, and containers like Docker, as well as public clouds such as Amazone AWS, Rackspace, and Digitalocean. The default is to use VirtualBox because it is free and the easiest to configure.
    • Virtual machines simply provide a virtual machine, where the installation and configuration of the various software depends on provision, the most basic shell scripts supported by the configuration tools, and Puppet, chef, ansible, and so on.
    • In order to facilitate the distribution and sharing of virtual machines, virtual hard disks and configuration of the VM itself plus vagrant settings files can be packaged together, which is called box in Vagrant, which is a virtual machine image file that can be used directly by vagrant. Many vendors and users in Atlas (formerly known as Vagrant Cloud) provide a variety of box, corresponding to the provider you use. You can also configure, distribute, and share your own box to your team or the public on top of other box.
    • If the above features do not meet your requirements for the environment, Vagrant also provides plug-ins to extend the basic functionality, and even you can develop your own plugins according to their specifications to meet specific requirements.

Let's take a few examples to illustrate the use of vagrant.

Please download, install VirtualBox and vagrant first. Because Vagrant is developed in Ruby, you also download and install Ruby. Whether your machine is a Windows, Mac, or a variety of Linux distributions, these software versions are available.

Example one of the simplest vagrant environments

Create a new directory, for example, called example. Then run:

Vagrant Init hashicorp/precise64

In the above command specified hashicorp/precise64 for the use of box, which is Ubuntu 12.04 LTS, more information is visible https://atlas.hashicorp.com/hashicorp/boxes/precise64. When you need a box for the first time, Vagrant automatically downloads the box to your machine, which is usually located in

The above will generate a basic vagrant configuration file Vagrantfile, the simplest case can be only this one file. The basic content is as follows:

#vagrantfile api/syntax version. Don ' t touch unless you know ' re doing!Vagrantfile_api_version ="2"vagrant.configure (vagrantfile_api_version) do|config|#every Vagrant virtual environment requires a box to build off of.Config.vm.box ="hashicorp/precise64"  #Create a forwarded port mapping which allows access to a specific port  #Within the machine from a port to the host machine. In the example below,  #accessing "localhost:8080" would access port in the guest machine.Config.vm.network"Forwarded_port", guest:80, host:8080End

This file specifies the following basic configuration:

  • In the above generated configuration file, the following box is specified:
    " hashicorp/precise64 "
  • Second, you specify the mapping directory between the virtual machine and the host, which is typically shared by the source file directory. In this way, you can continue to write code on the host with your familiar editor and use the virtual machine environment to run and debug.
    The above configuration file does not specify any mapping directories, but Vagrant lacks the capital to map the directory where the host Vagrantfile configuration file resides to the/vagrant directory on the virtual machine.
  • Also, you are configuring the virtual machine's network, otherwise you will not be able to access the virtual machine from the host. There are three modes: forwarded ports, host-only networking, and bridged networking.
    • Forwarded Ports, the default value, select a port on the host, forward to a port of the virtual machine, so that you do not have to give the virtual machine an IP, you can access the virtual machine from the host.
      In the above configuration file, forwarded Ports is used:
      " Forwarded_port ", guest:80, host:8080

      This maps the 80 port of the virtual machine to the 8080 port of the host, and the virtual machine can be accessed via localhost:8080 on the host. At the same time, the missing capital has another mapping, which maps the 22 port of the virtual machine to the 2222 port of the host, in order to allow SSH logons to the virtual machine from the host.

    • Host-only Networking, which creates a private network on the host that is visible only to the host and virtual machines, so that the virtual machine gets a specified static IP address that can access the virtual machine from the host through that static IP address, and the virtual machine can also pass the *.*.*. 1 IP address to access the host. In this case, other machines on the network where the host is located cannot "see" the private network and cannot access the virtual machine.
    • Bridged Networking, bridge the virtual machine to a device on the host (which should refer to a network card), so that the virtual machine is like another physical machine on the network of the host, which means that the virtual machine can access network resources and other machines on the network can access the virtual machine.

With this vagrantfile configuration file, you can start the virtual machine. Run the following command at the command line:

Vagrant up

The output is as follows:

From the above output, we can see the mapping of the specified box, port, and shared directory.

As mentioned earlier, the virtual machine 22 port mapping to host 2222 port is for SSH, we can now login to the virtual machine with SSH. Run the following command:

Ssh

Note: If your command line does not support SSH, you will not be able to log in. On Windows, when you install Git, you have an option to add the appropriate directory to the system path.

The corresponding output of the above command is:

This will log into the virtual machine, and at the prompt you can use the Linux command to manipulate the virtual machine.

Example two Python Sphinx document Environment

In the second example, we use VirtualBox and shell scripts to build a basic virtual environment for using Sphinx to generate documents. Create a new directory, such as Docs, where you create a new file, Vagrantfile, with the full configuration:

# vagrantfile Api/syntax version. Don'T touch unless you know'Re doing!vagrantfile_api_version="2"vagrant.configure (vagrantfile_api_version) Do|config|# Every Vagrant virtual environment requires a box to build off of. Config.vm.box="puphpet/ubuntu1404-x64"# Share A additional folder to the guest VM. The first argument is # The path of the host to the actual folder. The second argument is # The path on the guest toMountThe folder. and the optional third # argument is a set of non-required Options. # Config.vm.synced_folder".. /data","/vagrant_data"Config.vm.synced_folder".","/docs"Config.vm.provider"VirtualBox"  Do|v|V.name="Cakephp_docs"v.memory=384End Config.vm.provision:shell, path:"provision.sh"End

One of the most common versions of Linux is Ubuntu 14.04, which we'll use as a basis for specifying the Puppet/ubuntu1404-x64 box in the configuration:

  " puphpet/ubuntu1404-x64 "

The current directory of the mapped host (that is, the directory where the Vagrantfile configuration file resides) is the/docs directory for the virtual machine, where the source code *.rst files are placed:

  " .. /.. /"/docs"

In addition, the network is not configured because the virtual machine's Web server does not need to be accessed from the host.

Then specify the use of a shell script to install and configure Sphinx:

  " provision.sh "

In the same directory, create the shell script file povision.sh with the following contents:

#!/usr/bin/EnvBashEcho "update apt repository ..."Apt-get Update >/dev/NULL 2>&1Echo "Installing Python-setuptools ..."sudoApt-getInstall-Y Python-setuptools >/dev/NULL 2>&1Echo "Installing Easy_install ..."sudoApt-getInstall-Y Easy_install >/dev/NULL 2>&1Echo "Installing Sphinx ..."sudoEasy_install sphinx==1.2>/dev/NULL 2>&1Echo "Installing Sphinxcontrib-phpdomain ..."sudoEasy_install Sphinxcontrib-phpdomain >/dev/NULL 2>&1

The script installs two packages Python-setuptools and Easy_install using Apt-get, and then installs Sphinx 1.2 and Sphinxcontrib-phpdomain with Easy_install.

This is configured to run

Ssh

After that, SSH to the virtual machine, where you can build HTML documents from source code with Sphinx.

Example three lamp development environment

In a third example, we use Puphpet to configure a lamp development environment for PHP. Puphpet is a wizard-style interface for Web pages that makes it easier to generate vagrant configuration files that use Puppet .

Open https://puphpet.com with your browser.

The first step is to select the deployment target, default to local, and use VirtualBox. We chose the operating system for Ubuntu 14.04 64 bit as shown in:

Then, set the virtual machine IP address, memory, port, and directory mapping, and note that the directory here is used as the Web site to apply the root directory, as shown in:

The second step is to select the package to install on the virtual machine. We chose Ubuntu Htop and Vim as shown in:

The third step, firewall rules, does not need to do any setup, skip:

Fourth step, select Web Server, we choose to use Apache, to install the rewrite module as shown in:

Then set the domain name, the site root directory, and allow multiple vhost to be set, as shown in:

Fifth step, select the language PHP, as well as the relevant settings and required modules, as shown in:

You can also select the PHP library you want, as shown in:

Sixth step, select database MySQL and set the administrator password as shown in:

Then create a database that allows you to create multiple databases, as shown in:

Where/var/www/database/backup/2014-10-19_cakephp_blog.sql is the database backup to be restored in the virtual machine, which is related to the directory mapping you set in the first step.

Seventh, select other tools, including mail tools Mailcatcher, queue tools BEANSTALKD and RABBITMQ, search tools elastic searches and SOLR, as shown in:

We don't use any extra tools, so skip this step.

The eighth step, which is the final step, is completed as shown in:

Click on the Big blue button "GO AHEAD Then, make it!" To download a puphpet.zip of nearly 3MB. To open this. zip file, you can see the Vagrantfile configuration as set out in the first to eighth step above and the corresponding puppet module, as shown in:

Unzip it into a directory that can be placed in source control with the source code of the PHP project and used by other members of the project team.

Running vagrant up, you can get a running lamp virtual machine. In the host's hosts, add the following line:

192.168.56.102    Www.cakephp3.dev                # CakePHP 3

The Web site on the virtual machine can be accessed via Www.cakephp3.dev on the host's browser.

Common commands for Vagrant
vagrant  Up # Start, configure virtual machine vagrant suspend  # hibernate virtual machine vagrant Halt  # Turn off virtual machine vagrant destroy  # Remove virtual machine vagrant Status  # The state of the vagrant virtual machine that corresponds to the Vagrantfile configuration in the current directory, and the commands that can be used in this state vagrant global-status  # The status of all vagrant virtual machines on this computer

For more commands, refer to command-line Interface in [3].

Further thinking
    • With Docker, you can save resources, greatly reduce the startup time of your virtual machines, and also facilitate deployment of your production environment.
    • Pack and Distribute Box
    • Can I use vagrant to configure Windows virtual machines? What provision should I use? Can I use PowerShell?
    • Can I use vagrant to configure my Mac virtual machine? Probably not, at least there's no Mac's ' box ' found on Atlas. And, I'm afraid it's not necessary.
Resources
    1. Vagrant Official website
      https://www.vagrantup.com/
    2. Vagrant Getting Started
      https://docs.vagrantup.com/v2/getting-started/
    3. Vagrant Documentation
      https://docs.vagrantup.com/v2/
    4. Atlas (previously called Vagrant Cloud)
      Https://atlas.hashicorp.com
    5. Vagrant:up and running, ISBN 978-1-449-33583-0
      Http://shop.oreilly.com/product/0636920026358.do
    6. Hello Vagrant-Huang Boven
      Http://www.cnblogs.com/huang0925/p/3349841.html
    7. Comparison of features between Docker and vagrant
      Http://www.cnblogs.com/vikings-blog/p/3973265.html

Building a development environment using Vagrant

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.