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.
Key Points
- To create a Vagrant base image, you need to install VirtualBox and Vagrant on your machine, and then use VirtualBox to create a new virtual machine. Install the necessary software and configurations and use the
vagrant package
command to package them into a mirror file. - Vagrant Basic Mirror is a pre-packaged environment that can be used as a starting point for a project. It ensures consistency between different development environments and saves time by eliminating the need to set everything manually from scratch.
- The Vagrant basic image can be shared by uploading it to a mirror hosting service such as Vagrant Cloud. Once uploaded, the mirror URL can be shared with others, which they can add to their Vagrant environment using the
vagrant box add
command. - Vagrant basic mirroring usually includes the operating system, necessary software (such as a web server or database server), and any configuration required by the development environment. However, the content can be adjusted according to the needs of a specific project.
Create a virtual machine
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.
About LTS Version
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.
Install Ubuntu
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.
- When prompted for a host name, type vagrant
- For username and password, also type vagrant
- Encrypt your home directory? Select No
- On the Time Zone panel, select UTC or your own preferred time zone
- Parallel Method: Boot-Use the entire disk and set up LVM
- When prompted to install which software, select OpenSSH server, and the remaining software (such as LAMP or MySQL) will be installed later
- Select to install the GRUB boot loader on the main boot record
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>
Configure virtual machines for Vagrant
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>
Installing VirtualBox Client Add-ons
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>
Create the basic image package
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.
Shared basic mirror
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 http://ipnx.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.
Conclusion
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.
FAQ for Creating and Sharing Vagrant Basic Mirroring (FAQ)
What is a Vagrant basic mirror and why do I need it?
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.
How to create a Vagrant basic image?
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.
How to share my Vagrant basic image with others?
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 should I include in my Vagrant base image?
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.
How to update my Vagrant basic image?
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.
Can I use Vagrant basic mirroring for production environments?
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.
How to troubleshoot my Vagrant basic mirroring problem?
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.
Can I use Vagrant basic mirrors with other virtualization software?
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.
How to optimize my Vagrant basic image?
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.
Can I use Vagrant basic mirrors for cross-platform development?
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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

To determine the strength of the password, it is necessary to combine regular and logical processing. The basic requirements include: 1. The length is no less than 8 digits; 2. At least containing lowercase letters, uppercase letters, and numbers; 3. Special character restrictions can be added; in terms of advanced aspects, continuous duplication of characters and incremental/decreasing sequences need to be avoided, which requires PHP function detection; at the same time, blacklists should be introduced to filter common weak passwords such as password and 123456; finally it is recommended to combine the zxcvbn library to improve the evaluation accuracy.

Common problems and solutions for PHP variable scope include: 1. The global variable cannot be accessed within the function, and it needs to be passed in using the global keyword or parameter; 2. The static variable is declared with static, and it is only initialized once and the value is maintained between multiple calls; 3. Hyperglobal variables such as $_GET and $_POST can be used directly in any scope, but you need to pay attention to safe filtering; 4. Anonymous functions need to introduce parent scope variables through the use keyword, and when modifying external variables, you need to pass a reference. Mastering these rules can help avoid errors and improve code stability.

To safely handle PHP file uploads, you need to verify the source and type, control the file name and path, set server restrictions, and process media files twice. 1. Verify the upload source to prevent CSRF through token and detect the real MIME type through finfo_file using whitelist control; 2. Rename the file to a random string and determine the extension to store it in a non-Web directory according to the detection type; 3. PHP configuration limits the upload size and temporary directory Nginx/Apache prohibits access to the upload directory; 4. The GD library resaves the pictures to clear potential malicious data.

There are three common methods for PHP comment code: 1. Use // or # to block one line of code, and it is recommended to use //; 2. Use /.../ to wrap code blocks with multiple lines, which cannot be nested but can be crossed; 3. Combination skills comments such as using /if(){}/ to control logic blocks, or to improve efficiency with editor shortcut keys, you should pay attention to closing symbols and avoid nesting when using them.

AgeneratorinPHPisamemory-efficientwaytoiterateoverlargedatasetsbyyieldingvaluesoneatatimeinsteadofreturningthemallatonce.1.Generatorsusetheyieldkeywordtoproducevaluesondemand,reducingmemoryusage.2.Theyareusefulforhandlingbigloops,readinglargefiles,or

The key to writing PHP comments is to clarify the purpose and specifications. Comments should explain "why" rather than "what was done", avoiding redundancy or too simplicity. 1. Use a unified format, such as docblock (/*/) for class and method descriptions to improve readability and tool compatibility; 2. Emphasize the reasons behind the logic, such as why JS jumps need to be output manually; 3. Add an overview description before complex code, describe the process in steps, and help understand the overall idea; 4. Use TODO and FIXME rationally to mark to-do items and problems to facilitate subsequent tracking and collaboration. Good annotations can reduce communication costs and improve code maintenance efficiency.

ToinstallPHPquickly,useXAMPPonWindowsorHomebrewonmacOS.1.OnWindows,downloadandinstallXAMPP,selectcomponents,startApache,andplacefilesinhtdocs.2.Alternatively,manuallyinstallPHPfromphp.netandsetupaserverlikeApache.3.OnmacOS,installHomebrew,thenrun'bre

TolearnPHPeffectively,startbysettingupalocalserverenvironmentusingtoolslikeXAMPPandacodeeditorlikeVSCode.1)InstallXAMPPforApache,MySQL,andPHP.2)Useacodeeditorforsyntaxsupport.3)TestyoursetupwithasimplePHPfile.Next,learnPHPbasicsincludingvariables,ech
