📅  最后修改于: 2023-12-03 14:48:15.773000             🧑  作者: Mango
Vagrant is a powerful tool for managing virtual machines. With Vagrant, you can easily create and configure virtual environments to test your applications.
In this tutorial, we will show you how to create a new Ubuntu 16.04 virtual machine using Vagrant and configure it using Shell/Bash.
Before getting started, you will need to have the following prerequisites installed:
Note: This tutorial assumes that you have a basic understanding of how to use the command line.
First, let's create a new directory for our project and navigate into it:
mkdir my-project
cd my-project
Now, we can use the vagrant init
command to create a new Vagrantfile:
vagrant init ubuntu/xenial64
This command will create a new Vagrantfile in the current directory based on the specified box (Ubuntu 16.04 in this case).
Open the Vagrantfile in your favorite text editor and add the following lines to configure the virtual machine:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
# Configure the virtual machine
config.vm.box = "ubuntu/xenial64"
config.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
vb.cpus = "2"
end
# Run shell script to configure the virtual machine
config.vm.provision "shell", inline: <<-SHELL
# Update the package list
sudo apt-get update
# Install necessary packages
sudo apt-get install -y apache2 php mysql-server
# Configure Apache to serve our application
sudo rm /var/www/html/index.html
sudo ln -s /vagrant/public /var/www/html/my-app
# Restart Apache
sudo systemctl restart apache2
SHELL
end
The above configuration will do the following:
Save and close the Vagrantfile.
To start the virtual machine, run the following command:
vagrant up
This command will start the virtual machine and run the provisioning script to set up your environment.
Once the virtual machine is up and running, you can SSH into it by running:
vagrant ssh
This command allows you to log in to the virtual machine and use it as if it were a physical machine.
In this tutorial, we showed you how to create a new Ubuntu 16.04 virtual machine using Vagrant and configure it using Shell/Bash. With Vagrant, it's easy to create and manage virtual environments to test your applications.