Vagrant and VirtualBox on macOS X - mhulse/mhulse.github.io GitHub Wiki

Work in progress May 1, 2017

First, Install Vagrant and VirtualBox.

Use the links above to install manually, or:

$ brew cask install virtualbox vagrant vagrant-manager

Somewhere on you machine, create a directory for you Vagrant projects. I put mine here:

/Users/mhulse/dev/vagrant/<name of project>

Note: We’re going to organize our Vagrant boxes on a per-project basis.

Navigate to your new Vagrant project folder and run (see caveats section below.):

$ vagrant init ubuntu/trusty64

Note: The best place to find more boxes is HashiCorp’s Atlas box catalog.

Next, run:

$ vagrant up

Common commands:

$ vagrant ssh
$ vagrant halt
$ vagrant up

If you make changes to your Vagrantfile:

# Same as calling `halt` and `up`:
$ vagrant reload [vm-name] [--no-provision]
# Square brackets are optionals.

For a full list of Vagrant’s CLI commands, see: Command-Line Interface

From here, you can ssh into the current running Vagrant box:

$ vagrant ssh

You are now connected to the Vagrant box at /home/vagrant.

Use this to disconnect:

$ logout

… and then this when you are done developing:

# Terminate the use of any resources by the virtual machine:
$ vagrant destroy

Note: The vagrant destroy command does not actually remove the downloaded box file. To completely remove the box file, you can use the vagrant box remove command.

Automated provisioning, an example

Vagrant allows us to install dependencies, a.k.a. “Automatic Provisioning” when running vagrant up; this will create your machine and Vagrant will automatically provision it.

First, here’s what your Vagrantfile should look like:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# https://github.com/spiritix/vagrant-php7
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "ubuntu/trusty64"

  # Network configuration:
  config.vm.network "private_network", ip: "192.168.100.100"

  config.vm.synced_folder ".", "/vagrant",
    create: true,
    id: "vagrant-root",
    owner: "vagrant",
    group: "www-data",
    mount_options: ["dmode=775,fmode=664"]

  # Run this shell script when setting up the machine:
  config.vm.provision :shell, path: "bootstrap.sh"

end

Create a file called bootstrap.sh next to your Vagrantfile and add these contents:

#!/usr/bin/env bash

DOCUMENT_ROOT="public_html/"
SERVER_NAME="cth.local"
DATABASE_NAME="cth"

#-----------------------------------------------------------------------

Message() {

  echo "---------------------------------------------"
  echo $1
  echo "---------------------------------------------"

}

Update() {

  Message "UPDATING PACKAGES"

  sudo apt-get update
  sudo apt-get upgrade

}

#-----------------------------------------------------------------------

Message "STARTING BOOTSTRAP!"

Update

Message "MYSQL PREPARATION"

sudo debconf-set-selections <<< "mysql-server mysql-server/root_password password root"
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again password root"

Message "INSTALLING TOOLS AND HELPERS"

sudo apt-get install -y --force-yes \
  software-properties-common \
  vim \
  htop \
  curl \
  git

Message "INSTALLING PERSONAL PACKAGE ARCHIVES (PPAs)"

sudo add-apt-repository ppa:ondrej/php

Update

Message "INSTALLING PACKAGES"

sudo apt-get install -y --force-yes \
  apache2 \
  mysql-server-5.6 \
  git-core \
  libapache2-mod-php7.1 \
  php7.1 \
  php7.1-bcmath \
  php7.1-cli \
  php7.1-common \
  php7.1-curl \
  php7.1-dev \
  php7.1-fpm \
  php7.1-gd \
  php7.1-json \
  php7.1-mbstring \
  php7.1-mcrypt \
  php7.1-memcached \
  php7.1-mysql \
  php7.1-opcache \
  php7.1-xml \
  php7.1-zip

Update

Message "CONFIGURING APACHE AND PHP"

sed -i "s/error_reporting = .*/error_reporting = E_ALL/" /etc/php/7.1/apache2/php.ini
sed -i "s/display_errors = .*/display_errors = On/" /etc/php/7.1/apache2/php.ini

sudo a2enmod rewrite

Message "CREATING VIRTUAL HOST"

sudo ln -fs /vagrant/app /var/www/app

cat << EOF | sudo tee -a /etc/apache2/sites-available/default.conf
ServerName localhost
<Directory "/var/www/">
    AllowOverride All
</Directory>
<VirtualHost *:80>
  DocumentRoot "/var/www/app/${DOCUMENT_ROOT}"
  ServerName "${SERVER_NAME}"
  ServerAlias "www.${SERVER_NAME}"
  ErrorLog "${APACHE_LOG_DIR}/${SERVER_NAME}-error.log"
  CustomLog "${APACHE_LOG_DIR}/${SERVER_NAME}-access.log" combined
  <Directory "/var/www/app/${DOCUMENT_ROOT}">
    IndexOptions +FancyIndexing NameWidth=*
    Options -Indexes +Includes +FollowSymLinks +MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
    Require all granted
  </Directory>
</VirtualHost>
EOF

sudo a2ensite default.conf

Message "RESTARTING APACHE"

sudo /etc/init.d/apache2 restart

Message "INSTALLING COMPOSER"

curl -s https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer

Message "INSTALLING PHPMYADMIN"

wget -k https://files.phpmyadmin.net/phpMyAdmin/4.0.10.11/phpMyAdmin-4.0.10.11-english.tar.gz
sudo tar -xzvf phpMyAdmin-4.0.10.11-english.tar.gz -C /var/www/
sudo rm phpMyAdmin-4.0.10.11-english.tar.gz
sudo mv /var/www/phpMyAdmin-4.0.10.11-english/ /var/www/phpmyadmin

Message "SETTING UP DATABASE"

mysql -uroot -proot -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; FLUSH PRIVILEGES;"
mysql -uroot -proot -e "CREATE DATABASE ${DATABASE_NAME}";

Message "BOOTSTRAP FINISHED!"

From there:

# Start it:
$ vagrant up
# Reload, no provision:
$ vagrant reload
# Reload and provision:
$ vagrant reload --provision

Using host database

You can use an MySQL database (for example) on the host machine; get the

$ vagrant ssh
$ netstat -rn | grep "^0.0.0.0 " | cut -d " " -f10
10.0.2.2

From there, just fire up MySQL and make sure your users/database exists.

⚠️ **GitHub.com Fallback** ⚠️