Tutorial on using Vagrant to build a local development environment
You have set up a development environment locally, which may be prepared for php. You have created a project on this local development environment, when you migrate it to a real server, you find some problems. This is probably because there is a difference between your local development environment and the production environment of the server. Slowly, you want to try node again because it has a good relationship with JavaScript. Or she is interested in ruby because she has a nice name. If you try to install software and configure the running environment, you will find that managing these environments may be a problem.
Vagrant allows you to create a virtual machine for each project. You can choose the operating system where the virtual machine runs to configure the required environment, such as installing Web servers and database management systems. The project directory can be placed on your computer, which will be synchronized with a directory on the virtual machine. Your computer and virtual machine can access each other, so you can use your favorite editor to develop Web applications as usual, then run your project in the Environment configured on the virtual machine. With Vagrant, you can use virtual machines on your local computer to configure the same environment as the production environment of Web applications, the same operating system, the same software, and the same configuration. You can also share your development environment with other team members,
Preparation
Download and install the following two things:
VirtualBox is an open-source free virtual machine software that can be used across platforms, Windows, and OS X. Vagrant is also cross-platform. The demonstration in this article is completed under OS X.
Box
Box is a virtual machine image, which can be downloaded from Vagrant Cloud. You can select the OS you want. Here I want CentOS 6.5, because of the production environment of my website, we also plan to use the CentOS 6.5 operating system. Here I want to use:
chef/centos-6.5
Return to the command line tool, download the box to your local computer, and add it to vagrant:
vagrant box add chef/centos-6.5
Return Value:
==> box: Loading metadata for box 'chef/centos-6.5' box: URL: vagrantcloud.com/chef/centos-6.5This box can work with multiple providers! The providers that itcan work with are listed below. Please review the list and choosethe provider you will be working with.1) virtualbox2) vmware_desktop
Because we use VirtualBox, we should select the first option here, enter the number 1, and then press Enter. In this way, vagrant downloads the specified box from Vagrant Cloud. Because the image contains the operating system, it is generally large, with several hundred MB to several GB.
After that, enter the following command to view the available box:
vagrant box list
Return Value:
chef/centos-6.5 (virtualbox, 1.0.0)
Now, we can use the CentOS-6.5 version of the operating system to run the Web application we want to develop.
Initialization
Open the command line tool. On OS X, you can use the terminal tool that comes with the system. On Windows, you can use the command prompt tool. Create a directory for the project, which can be placed anywhere. I plan to create a directory named bkjia.net on the desktop of the currently logged-on user:
cd ~/desktopmkdir bkjia.net
Go to the directory created for the project:
cd bkjia.net
Run the following command to initialize the project:
vagrant init chef/centos-6.5
The above command is to use the box we downloaded and added chef/centos-6.5 to initialize the project, that is, our project runs on a virtual machine of the operating system of centos 6.5.
After executing the above command, a file named Vagrantfile will be generated in the project directory, which tells vagrant how to run the virtual machine.
Configure the network
If you want to allow communication between your computer and the virtual machine, for example, if you have installed a Web server on the virtual machine, you want to be able, or other devices in the same network can access projects on this virtual machine. This requires some configuration, including three methods: Forwarded Ports, Private Network, and Public Network ).
Next, we will configure a public network, that is, you can access the virtual machine through an internal ip address on your computer, other devices in the same network, you can also access this virtual machine.
Open the Vagrantfile file in the project directory. Find this line of code:
# config.vm.network "public_network"
Remove the # sign before it:
config.vm.network "public_network"
Save the file.
Start
After configuring the network, you can start the virtual machine, confirm that the current directory is the project directory, and then execute:
vagrant up
Return Value:
Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'chef/centos-6.5 '... ==> default: Matching MAC address for NAT networking... = & gt; default: Checking if box 'chef/centos-6.5 'is up to date... ==> default: Setting the name of the VM: bkjianet_default_1404827171486_40050 ==> default: Fixed port collision for 22 => 2222. now on port 2200. ==> default: Clearing any pr Eviusly set network interfaces... ==> default: Available bridged network interfaces: 1) en1: Wi-Fi (AirPort) 2) en0: Ethernet 3) en4: Thunderbolt 14) en3: Thunderbolt 25) p2p06) bridge0 default: What interface shoshould the network bridge?
After the network is configured and started for the first time, the above prompt will appear. Let's select the bridge mode, which one should depend on your network environment. For example, my computer accesses the Internet through a wireless router in a small LAN, so here I choose the first option: en1: Wi-Fi (Airport ). Enter the number 1 and 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/bkjia.net
Ssh
After it is started, we can ssh to the virtual machine. Run the following command:
vagrant ssh
In this way, you can install software, configure, and so on like operating Linux servers.
Note that the project directory on your computer is synchronized with the/vagrant directory in the virtual machine.
Install nginx
Next, we will install the nginx Web server on the virtual machine as a demonstration. Configure a complete environment for running php on CentOS.
wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
Run the following command:
sudo rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm
Then install nginx with yum:
sudo yum install nginx
After the installation is complete, you can start the nginx service:
sudo service nginx start
After starting the nginx service on the virtual machine, you can access the Web service provided by this virtual machine on our computer or other devices in the same network. But first, we need to know the IP address of this virtual machine in the Intranet, and execute the command:
ifconfig
Return Value:
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 has an internal IP address in this lan, Which is 192.168.1. * The internal IP addresses of other devices on the network, such as the iPhone, iPad, or other computers, are in this form. Therefore, I can tell that 192.168.1.104 in eth1 is the IP address of this virtual machine in the intranet.
On your computer, open your browser and enter 192.168.1.104. You should be able to access the Web Services Provided by the virtual machine.
Command
Turn off the VM:
vagrant halt
Restart the VM:
vagrant reload
Destroy virtual machines:
vagrant destroy
For detailed instructions, see the official vagrant documentation.
Update
2014.07.11
Today, I tested how to install and configure vagrant in the qq group with everyone. I also tested Windows installed on a virtual machine. Most of them have been configured successfully. The following describes the common problems.
When adding a box for vagrant, pay attention to the system version. If your operating system is 32-bit, the box you download must also be 32-bit. Search for the box you want on VagrantCloud and select the appropriate version. For example, in this article I Recommend installing the chef/centos-6.5 box, which is a 64-bit system. If you need a 32-bit system, try vagrant init rafacas/centos65-i386-plain.
After vagrant up is enabled, connect to the VM Using ssh. For Windows users, you can use putty, enter 127.0.0.1 on the host, set the port number to 2222, set the logon username to vagrant, and set the password to vagrant.
After you use vagrant package to package a running box, you can add this box and use the vagrant box add command. You can use this box next time you start the init project. During startup, problems may occur. My problem is caused by the NIC. The solution is to delete/etc/udev/rules. d/70-persistent-net.rules.
2014.08.04
Today, we found that the nginx server cannot synchronize static files edited locally. The solution is to edit the nginx configuration file and add sendfile off in the server or location block;
How to Use vagrant to install a Hadoop cluster on a virtual machine
Efficient Puppet module Management in Vagrant
Use Vagrant and Fabric for Integration Testing
Build a development environment using Vagrant
Configure the Vagrant environment in Windows
Build a development environment using Vagrant
Build a virtual machine environment through Vagrant
This article permanently updates the link address: