What is Vagrant
Vagrant is a tool for building and managing virtual machine environments in a single workflow. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time, increases production parity, and makes the "works on my machine" excuse a relic of the past.
In this tutorial we will dive into Vagrant which is used to create isolated & portable development environments.
We will go into each part of the
Vagrantfile, do some exploring via SSH and create a LAMP stack box (Linux, Apache, MySQL, PHP).
LAMP stack and versions on this Vagrant box:
Linux - ubuntu/trusty64 Apache - apache2 MySQL - mysql-server (5.5.62-0ubuntu0.14.04.1) PHP - PHP 7.2
Please download the proper package for your operating system and architecture.
Download - Vagrant by HashiCorp
Please download VirtualBox that correspond your operating system.
Downloads – Oracle VM VirtualBox
Getting Started with Vagrant
Note: At anytime having the
VirtualBox open, you can follow when the box are created, running, etc
Verify version on your terminal by running:
It should display:
Note: My version is 2.2.0, but you can have any other version.
The settings within config.vm modify the configuration of the machine that Vagrant manages.
Main config files:
-> config.vm.box - This file configures what box the machine - Operating system - will be brought up against.
-> config.vm.provider - Configures provider-specific configuration, which is used to modify settings which are specific to a certain provider, we use
VirtualBox. (More details)
-> config.vm.network - Configures networks on the machine. Ex: IP address, Port, etc. (More details)
-> config.vm.synced_folder - Configures synced folders on the machine, so that folders on your host machine can be synced to and from the guest machine. (More details)
-> config.vm.provision - Configures provisioners on the machine, so that software (ex: LAMP Stack) can be automatically installed and configured when the machine is created. (More details)
Discover Vagrant Boxes list
Creating a box
First, create a folder that will contain your vagrant data:
# new folder mkdir my_vagrant_box cd my_vagrant_box # new folder mkdir www # Create a vagrantfile vagrant init ubuntu/trusty64
After running the above commands
vagrant init ubuntu/trusty64, you will have a new file:
Vagrantfile, that contains Vagrant box configuration.
Since we would like to create a LAMP stack in this box we will update this new
Vagrantfile file and replace the current content with the follow:
# -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure("2") do |config| # Box Settings config.vm.box = "ubuntu/trusty64" # Provider Settings config.vm.provider "virtualbox" do |vb| # vb.memory = 2048 # vb.cpus = 4 end # Network Settings # config.vm.network "forwarded_port", guest: 80, host: 8080 # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1" config.vm.network "private_network", ip: "192.168.33.10" # Folder Settings config.vm.synced_folder "www/", "/var/www/html" config.vm.provision "shell", path: "bootstrap.sh" end
Next its content that will instruct what can be install & upgrade.
Create a new file with the following content:
# Update Packages apt-get update # Upgrade Packages apt-get upgrade # Basic Linux Stuff apt-get install -y git # Apache apt-get install -y apache2 # Enable Apache Mods a2enmod rewrite #Add Onrej PPA Repo apt-add-repository ppa:ondrej/php apt-get update # Install PHP apt-get install -y php7.2 # PHP Apache Mod apt-get install -y libapache2-mod-php7.2 # Restart Apache service apache2 restart # PHP Mods apt-get install -y php7.2-common apt-get install -y php7.2-mcrypt apt-get install -y php7.2-zip # Set MySQL Pass debconf-set-selections <<< 'mysql-server mysql-server/root_password password root' debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password root' # Install MySQL apt-get install -y mysql-server # PHP-MYSQL lib apt-get install -y php7.2-mysql # Restart Apache sudo service apache2 restart
The file structure and filenames should be:
./boostrapp.sh ./Vagrantfile ./www/
Finally running Up And SSH
It is time to boot your first Vagrant environment.
Run the following from your terminal:
After finish you will have a virtual machine running Ubuntu.
To prove that it is running, you can SSH into the machine:
This command will drop you into a full-fledged SSH session.
The SSH session can be terminated with CTRL+D or type
Once the machine is running, load
http://220.127.116.11 in your browser. You should see a web page that is being served from the virtual machine that was automatically setup by Vagrant.
vagrant up -> This command creates and configures guest machines according to your Vagrantfile.
vagrant ssh -> This will SSH into a running Vagrant machine and give you access to a shell.
vagrant suspend -> This suspends the guest machine Vagrant is managing, rather than fully shutting it down or destroying it.
vagrant resume ->This resumes a Vagrant managed machine that was previously suspended, perhaps with the suspend command.
vagrant destroy -> This command stops the running machine Vagrant is managing and destroys all resources that were created during the machine creation process.
vagrant reload -> This command is usually required for changes made in the Vagrantfile to take effect. After making any modifications to the Vagrantfile, a reload should be called.
Command: vagrant box
This is the command used to manage (add, remove, etc.) boxes.
The main functionality of this command is exposed via even more subcommands:
Subscribe to NL Slack
Get the latest posts delivered right to your inbox