Home >Backend Development >PHP Tutorial >How to Create and Share a Vagrant Base Box
Every new tools appear every day to help developers become more efficient and allow them to focus on the actual business value of their projects.
Vagrant is such a tool that is becoming one of the most powerful assistants for developers, which standardizes the way development environments are created and managed.
This article will teach you how to create your own Vagrant basic image based on Ubuntu 14.04 LTS, configure it with your favorite app, and share it with others via Vagrant Cloud.
vagrant package
command to package them into a mirror file. vagrant box add
command. Note: The following example requires that Vagrant be installed at least 1.5 version on the local computer.
To create a virtual machine, you need a virtualization provider. Although Vagrant does not discriminate against which provider to use, the following example will use VirtualBox.
Before creating a virtual machine, you need to install VirtualBox first.
Next, go to the Ubuntu download page and get the Ubuntu 14.04 LTS ISO image.
A new Ubuntu version is released every six months, and support for each version is limited.
LTS stands for long-term support, and it is given a specific subset of Ubuntu version. The LTS spans up to five years. If you install the LTS version today, you will get an update in five years.
For web development, it is best to always use Ubuntu with LTS tags, the good news is that Ubuntu 14.04 LTS was released on April 17, 2014, so support continues until 2019.
After the ISO file is downloaded, start VirtualBox and click New to create a new virtual machine, and select Linux as type, Ubuntu (64-bit) as version , and name your virtual machine.
Click Continue and set the memory size to 1024 MB or higher (if you think memory-intensive tasks will be run in the virtual machine).
In the next panel, select Create virtual hard disk now, and then select VMDK (Virtual Machine Disk).
In the Physical Hard Drive Storage panel, select Dynamic Allocation and set at least 8 GB. Click Continue and your virtual machine will be almost finished.
You need to insert the downloaded Ubuntu 14.04 LTS ISO file as a virtual CD to install the operating system when starting the virtual machine. It's like you plugged a bootable CD into a CD-ROM.
Select the virtual machine from the list, click Set, and then click the Storage tab. On Controller: IDE, click the small CD icon (highlighted in red in the image below), and then click to select disk and select the Ubuntu ISO file.
Now we are ready to start the virtual machine and install Ubuntu as the guest operating system. There are several steps in the installation process, such as selecting language, country/region, etc. I will introduce more important steps below.
This should summarize the installation process and you will be prompted to log in next. Log in with vagrant user and password.
Next, switch to root user by typing the following command:
<code>sudo su -</code>
Please note that this is not a very secure setting, and protecting Ubuntu is not within the scope of this article. Safety is not important for developing machines.
Using root user, update to the latest software package by typing the following command
<code>apt-get update apt-get upgrade</code>
Vagrant requires setting some specific options to work well with the virtual machine.
Execute command without password
All operations performed by Vagrant will be used with thevagrant user, and all sudo commands need to be executed without a password every time they are run.
To set this, we need to run thevisudo command to configure the user with no password. Just type:
<code>visudo</code>Add the following line to the end of the file and save:
<code>vagrant ALL=(ALL) NOPASSWD:ALL</code>Root Password
The general rule when creating a Vagrant image is to set the root password to a well-known password. Vagrant uses vagrant as password. To set this, type the following command as root and type your password when prompted:
<code>sudo su -</code>
SSH configuration
To enable Vagrant to SSH to a virtual machine, it uses public key authentication. We need to set this for our vagrant users.
Still log in as root user, go to vagrant user's home directory:
<code>apt-get update apt-get upgrade</code>
Create the following folder:
<code>visudo</code>
Vagrant Connect to the virtual machine using an insecure key pair, you can download it here: github.com/mitchellh/vagrant/blob/master/keys/vagrant.pub
Simply run the following command to get it directly:
<code>vagrant ALL=(ALL) NOPASSWD:ALL</code>
OpenSSH has very strict permissions for this folder and file, so let's change it to the correct permissions:
<code>passwd</code>
If everything goes well, this is the folder structure of /home/vagrant and /home/vagrant/.ssh , including permissions.
Optional steps: In order to speed up the SSH connection speed to the virtual machine, we need to modify the SSH server configuration:
<code>cd /home/vagrant</code>
Add this line to the end of the file and save:
<code>mkdir .ssh</code>
Then restart the SSH server:
<code>wget https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub -O .ssh/authorized_keys</code>
Finally, we can log out of the root user by typing the following command:
<code>chmod 700 .ssh chmod 600 .ssh/authorized_keys chown -R vagrant:vagrant .ssh</code>
In order for Vagrant to correctly share folders between the client and host operating system, the VirtualBox Client Add-on is required.
The prerequisite for installing client add-ons on a Linux system are software packages such as Linux headers and development tools. Let's install them first by typing the following:
<code>cd /etc/ssh pico sshd_config</code>Next, we need to make sure that the client add-on image is inserted into the virtual CD-ROM of the virtual machine. To do this, please follow the window of the virtual machine and select
Device -> Insert the client add-on CD image .
Settings -> Storage . You should see VBoxGuestAdditions.iso under Controller: IDE.
<code>UseDNS no</code>After this operation is complete, it is best to restart the virtual machine to ensure that the new settings take effect:
<code>sudo su -</code>
Now that we have created the basic virtual machine, we need to create the actual Vagrant base image. Simply type the following command in the terminal of your actual host machine (not the command line of your virtual machine):
<code>apt-get update apt-get upgrade</code>
<code>visudo</code>
In our case, it's called Ubuntu 14.04 x64 LTS Vagrant Base Box, and it also contains spaces, so make sure you enclose it in quotes like this:
<code>vagrant ALL=(ALL) NOPASSWD:ALL</code>
This process will generate a package.box file, which is an archive of the virtual machine and Vagrant metadata files.
Vagrant Recently created a service called Vagrant Cloud where anyone can create and share Vagrant images for others to use.
To share our basic image on Vagrant Cloud, we first need to register. After logging in, you will see the dashboard below. Click the to create a mirror link in the upper right corner.
We need to assign a name and a short description to our image. You can enter anything here, I added ubuntu-trusty64 to our image. Trusty is the development code for Ubuntu 14.04 LTS.
Next, you need to create a version for the image, let's add 1.0.0 for the initial version number and click to create a version .
After creating the image and version, we need to add a provider. Click Create a new provider and type virtualbox, then click the URL button and type the URL where the package.box file is located.
Note: Only paid advanced users can upload the basic image directly to Vagrant Cloud. For more information, check out the pricing page.
After adding the provider, click the Publish now button to make the image available on Vagrant Cloud.
The Vagrant image used as an example in this article is available at https://www.php.cn/link/cb0d6d3459c57b7a5c968956b1ecf1e3. This is a perfect base image you can use for your development environment, but remember it is not safe and only used locally.
You learned how to create a VirtualBox virtual machine, install Ubuntu as a guest operating system, and how to package this VM for use with Vagrant.
Finally, we show you how to distribute your Vagrant basic image on Vagrant Cloud and share it publicly on the internet.
In a subsequent article in this article, you will learn how to use this Vagrant basic image to install your preferred software and set up your development environment using a combination of shell scripts and other configuration tools.
Vagrant Basic Mirroring is essentially a pre-packaged environment that you can use as a starting point for your project. It contains the operating system, pre-installed software, and the configuration required for the development environment. The main advantage of using Vagrant base mirroring is that it ensures consistency between different development environments, thus reducing the possibility of having a “workable on my machine” problem. It also saves time because you don't have to manually set everything from scratch.
Creating a Vagrant basic image involves multiple steps. First, you need to install VirtualBox and Vagrant on your machine. Then, you use VirtualBox to create a new virtual machine, install the necessary software and configurations, and use the vagrant package command to package it into a mirror file. This image file can then be used to initialize the new Vagrant environment.
You can share your Vagrant basic image by uploading it to a mirror hosting service like Vagrant Cloud. Once uploaded, you can share the mirror URL with others, and they can add it to their Vagrant environment using the vagrant box add command.
What is included in your Vagrant base image depends on your project requirements. However, it usually includes an operating system, necessary software (such as a web server or database server), and any configuration required by the development environment.
To update your Vagrant base image, you need to make the necessary changes to the virtual machine, repackage it into a mirror file, and upload it to the image hosting service. You can then use the vagrant box update command to update the image in the Vagrant environment.
While you can technically use Vagrant base mirroring for production environments, this is not recommended. Vagrant is primarily used to create reproducible development environments, and using them for production can lead to performance and security issues.
Troubleshooting issues with Vagrant base images may include checking Vagrant and VirtualBox logs, verifying image file integrity, and ensuring that your Vagrant and VirtualBox versions are compatible.
Yes, Vagrant supports several other virtualization providers besides VirtualBox, including VMware, Hyper-V, and Docker. However, you need to make sure the image files are compatible with the provider you are using.
Optimizing your Vagrant basic image may include reducing image file size, minimizing the number of pre-installed software, and using scripts to automate the image creation process.
Yes, one of the main advantages of using Vagrant basic mirroring is that it allows for cross-platform development. Since the image file contains a complete development environment, it can be used on any platform that supports Vagrant and selected virtualization providers.
The above is the detailed content of How to Create and Share a Vagrant Base Box. For more information, please follow other related articles on the PHP Chinese website!