📌  相关文章
📜  vagrant ubuntu 16.04 - Shell-Bash (1)

📅  最后修改于: 2023-12-03 14:48:15.773000             🧑  作者: Mango

Vagrant Ubuntu 16.04 - Shell/Bash

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.

Prerequisites

Before getting started, you will need to have the following prerequisites installed:

  • Vagrant - https://www.vagrantup.com/downloads.html
  • VirtualBox - https://www.virtualbox.org/wiki/Downloads

Note: This tutorial assumes that you have a basic understanding of how to use the command line.

Creating a new virtual machine

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).

Configuring the virtual machine

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:

  • Set the virtual machine's memory to 2048MB and CPUs to 2
  • Update the package list and install Apache, PHP, and MySQL server
  • Configure Apache to serve our application
  • Restart Apache

Save and close the Vagrantfile.

Running the virtual machine

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.

Conclusion

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.