Vagrant allows you to create a project for each virtual machine, you can select the operating system of the virtual machine is running, the need to configure the environment, such as installing Web servers, database management systems. Catalog items can be placed on your computer, it will sync with a directory on the virtual machine. Between your computer and the virtual machines can access each other, so, as usual, you can use your favorite editor to develop Web applications, and then use the virtual machine environment configured to run your project. Use Vagrant, you can use to configure a virtual machine on your local computer as a production environment with the Web application environment, the same operating system, the same software, the same configuration. You can also share your development environment and other members of the team,
Download and install the following two things:
VirtualBox is a free open source virtual machine software, cross-platform, Windows, OS X can be used. Vagrant is also cross-platform. This article presentation is done in the OS X operating system below.
box is a virtual machine image can be downloaded to the Vagrant Cloud Inside, you can choose what you want the operating system, and here I use the CentOS 6.5, because my site production environment, but also intends to use CentOS 6.5 operating system . Here I use it is:
chef / centos-6.5
Back to the command-line tool, go to the box downloaded to the local computer, and add to the vagrant inside:
vagrant box add chef / centos-6.5
==> Box: Loading metadata for box 'chef / centos-6.5'
box: URL: vagrantcloud.com/chef/centos-6.5
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.
Because we are using VirtualBox, therefore, there should choose the first option, enter the number 1, and then press Enter. Such vagrant downloaded from our designated box on Vagrant Cloud. Because the image contains the operating system, it is generally relatively large, several hundred MB to several G has.
After completion, enter the following command to view the box can be used:
vagrant box list
chef / centos-6.5 (virtualbox, 1.0.0)
Now, you can use the CentOS-6.5 version of the operating system to run we want to develop a Web application.
Open a command-line tool, OS X can use the system comes on top of a terminal tool, Windows users can use command prompt tools. Then create a directory for the project, it can be placed anywhere. I'm going on the currently logged on user's desktop, create a directory called linuxidc.net:
cd ~ / desktop
Into the directory created for this project:
Then Run the initialization of the project:
vagrant init chef / centos-6.5
Prior to the above command is to use our download and add the chef / centos-6.5 Click the box to initialize the project, that is, our project will be run in a virtual machine centos 6.5 this version of the operating system on.
Implementation of the above command line, the directory of the project which will generate a file called Vagrantfile, this file tells the kind of vagrant how to run the virtual machine.
If you want to be able to communicate with each other on your computer with a virtual machine, such as your virtual machine installed on a Web server, you're going to be able to on the local computer, or other devices on the same network can access this virtual machine on the project. This need to do some configuration, there are three ways, port forwarding (Forwarded Ports), private networks (Private Network), public network (Public Network).
Next, we went to a public network configuration, that is, you can access to a virtual machine on your computer through an internal ip address of other devices within the same network, you can also have access to this virtual machine.
First open project directory below Vagrantfile file. Find this line of code:
# Config.vm.network "public_network"
Remove the # in front of it:
After configuring the network, it is possible to start the virtual machine, and to determine the current directory is the directory where the project, and then execute:
Bringing machine 'default' up with 'virtualbox' provider ...
==> Default: Importing base box 'chef / centos-6.5' ...
==> Default: Matching MAC address for NAT networking ...
==> Default: Checking if box 'chef / centos-6.5' is up to date ...
==> Default: Setting the name of the VM: linuxidcnet_default_1404827171486_40050
==> Default: Fixed port collision for 22 => 2222. Now on port 2200.
==> Default: Clearing any previously set network interfaces ...
==> Default: Available bridged network interfaces:
1) en1: Wi-Fi (AirPort)
2) en0: Ethernet
3) en4: Thunderbolt 1
4) en3: Thunderbolt 2
default: What interface should the network bridge to?
After the configured network, the first time you start, there will be such a prompt above, let us choose bridge mode, choose which depends on your network environment. For example, my computer is in a small local area network, the Internet through a wireless router, so here, I chose the first option: en1: Wi-Fi (Airport). Enter the number 1, and then press Enter.
==> Default: Preparing network interfaces based on configuration ...
default: Adapter 1: nat
default: Adapter 2: bridged
==> Default: Forwarding ports ...
default: 22 => 2200 (adapter 1)
==> Default: Booting VM ...
==> Default: Waiting for machine to boot This may take a few minutes ....
default: SSH address: 127.0.0.1:2200
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning:. Connection timeout Retrying ...
default: Warning:. Remote connection disconnect Retrying ...
==> Default: Machine booted and ready!
==> Default: Checking for guest additions in VM ...
==> Default: Configuring and enabling network interfaces ...
==> Default: Mounting shared folders ...
default: / vagrant => /Users/xiaoxue/Desktop/linuxidc.net
After the start, we can ssh to the virtual machine. Use the command:
This can operate as a normal Linux server, to install the software, configuration, and so on.
Note that the directory on your computer projects virtual machine inside / vagrant This directory is synchronized.
Next, we installed on the virtual machine at the Web server nginx as a demonstration. On CentOS a complete system configuration can run php environment.
sudo rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm
Then install nginx with yum:
sudo yum install nginx
After installation is complete, you can start nginx service:
sudo service nginx start
After the start of the service nginx virtual machine on our computers in the same network or other device, you can access to this virtual machine to provide Web services. But first we need to know which virtual machine network, including IP addresses, execute the command:
eth0 Link encap: Ethernet HWaddr 08: 00: 27: CE: 08: 3D
inet addr: 10.0.2.15 Bcast: 10.0.2.255 Mask: 255.255.255.0
inet6 addr: fe80 :: a00: 27ff: fece: 83d / 64 Scope: Link
UP BROADCAST RUNNING MULTICAST MTU: 1500 Metric: 1
RX packets: 596 errors: 0 dropped: 0 overruns: 0 frame: 0
TX packets: 397 errors: 0 dropped: 0 overruns: 0 carrier: 0
collisions: 0 txqueuelen: 1000
RX bytes: 61284 (59.8 KiB) TX bytes: 50362 (49.1 KiB)
eth1 Link encap: Ethernet HWaddr 08: 00: 27: C4: AB: A1
inet addr: 192.168.1.104 Bcast: 192.168.1.255 Mask: 255.255.255.0
inet6 addr: fe80 :: a00: 27ff: fec4: aba1 / 64 Scope: Link
UP BROADCAST RUNNING MULTICAST MTU: 1500 Metric: 1
RX packets: 2 errors: 0 dropped: 0 overruns: 0 frame: 0
TX packets: 11 errors: 0 dropped: 0 overruns: 0 carrier: 0
collisions: 0 txqueuelen: 1000
RX bytes: 1180 (1.1 KiB) TX bytes: 1578 (1.5 KiB)
lo Link encap: Local Loopback
inet addr: 127.0.0.1 Mask: 255.0.0.0
inet6 addr: :: 1/128 Scope: Host
UP LOOPBACK RUNNING MTU: 16436 Metric: 1
RX packets: 0 errors: 0 dropped: 0 overruns: 0 frame: 0
TX packets: 0 errors: 0 dropped: 0 overruns: 0 carrier: 0
collisions: 0 txqueuelen: 0
RX bytes: 0 (0.0 b) TX bytes: 0 (0.0 b)
My computer in the LAN has an internal IP address, the address is 192.168.1. *, In the other devices inside the network, such as iPhone, iPad or other computer, their internal IP addresses are this form. So, I can tell, eth1 inside 192.168.1.104, which should be the virtual machine, including the network's IP address.
On your computer, open a browser and enter 192.168.1.104, you should be able to access the Web services provided by the virtual machine.
Turn off the virtual machine:
Restart the virtual machine:
Destruction of a virtual machine:
Detailed instructions, you can view vagrant official documents.
Today, together with everyone in the group qq, we test it under Windows to install the configuration vagrant. I also installed Windows on a virtual machine to do the test. Most people have successfully configured, and summarize the following common problems.
In addition, when a vagrant box, and pay attention to the version of the system. If your operating system is 32-bit, then you must also download the box is 32, and you want the search box on VagrantCloud, then select the appropriate version. For example, this article I recommend installing the chef / centos-6.5 this box, it is a 64-bit system. If you need a 32-bit system, you can try vagrant init rafacas / centos65-i386-plain.
Using vagrant up, after the start, use ssh to connect to a virtual machine, Windows users can use putty, fill in the host 127.0.0.1, port number is 2222, the user login name is the vagrant, the password is vagrant.
With vagrant package packing box after running, you can add the box, with vagrant box add command. Next time init project, you can use this box a. At boot time, you may encounter problems, my problems are caused by the card, the solution is to remove /etc/udev/rules.d/70-persistent-net.rules.
Today found nginx server can not synchronize static files locally edited, the solution is to edit the nginx configuration files, server or location inside the block, add sendfile off;