2013-09-03 18:08:28 +00:00
|
|
|
---
|
2017-03-28 01:19:52 +00:00
|
|
|
layout: "intro"
|
2013-09-06 16:50:43 +00:00
|
|
|
page_title: "Provisioning - Getting Started"
|
2013-09-03 18:08:28 +00:00
|
|
|
sidebar_current: "gettingstarted-provisioning"
|
2016-01-19 18:08:53 +00:00
|
|
|
description: |-
|
|
|
|
We have a virtual machine running a basic copy of Ubuntu and we can edit files
|
|
|
|
from our machine and have them synced into the virtual machine. Let us now
|
|
|
|
serve those files using a webserver.
|
2013-09-03 18:08:28 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
# Provisioning
|
|
|
|
|
|
|
|
Alright, so we have a virtual machine running a basic copy of Ubuntu and
|
|
|
|
we can edit files from our machine and have them synced into the virtual machine.
|
2016-01-19 18:08:53 +00:00
|
|
|
Let us now serve those files using a webserver.
|
2013-09-03 18:08:28 +00:00
|
|
|
|
|
|
|
We could just SSH in and install a webserver and be on our way, but then
|
|
|
|
every person who used Vagrant would have to do the same thing. Instead,
|
|
|
|
Vagrant has built-in support for _automated provisioning_. Using this
|
|
|
|
feature, Vagrant will automatically install software when you `vagrant up`
|
|
|
|
so that the guest machine can be repeatably created and ready-to-use.
|
|
|
|
|
|
|
|
## Installing Apache
|
|
|
|
|
2016-01-19 18:08:53 +00:00
|
|
|
We will just setup [Apache](http://httpd.apache.org/) for our basic project,
|
|
|
|
and we will do so using a shell script. Create the following shell script
|
2013-09-03 18:08:28 +00:00
|
|
|
and save it as `bootstrap.sh` in the same directory as your Vagrantfile:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
apt-get update
|
|
|
|
apt-get install -y apache2
|
2014-09-03 14:08:57 +00:00
|
|
|
if ! [ -L /var/www ]; then
|
|
|
|
rm -rf /var/www
|
|
|
|
ln -fs /vagrant /var/www
|
|
|
|
fi
|
2013-09-03 18:08:28 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
Next, we configure Vagrant to run this shell script when setting up
|
|
|
|
our machine. We do this by editing the Vagrantfile, which should now
|
|
|
|
look like this:
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
Vagrant.configure("2") do |config|
|
2016-01-19 18:08:53 +00:00
|
|
|
config.vm.box = "hashicorp/precise64"
|
2014-05-22 16:35:12 +00:00
|
|
|
config.vm.provision :shell, path: "bootstrap.sh"
|
2013-09-03 18:08:28 +00:00
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
The "provision" line is new, and tells Vagrant to use the `shell` provisioner
|
|
|
|
to setup the machine, with the `bootstrap.sh` file. The file path is relative
|
|
|
|
to the location of the project root (where the Vagrantfile is).
|
|
|
|
|
|
|
|
## Provision!
|
|
|
|
|
|
|
|
After everything is configured, just run `vagrant up` to create your
|
|
|
|
machine and Vagrant will automatically provision it. You should see
|
|
|
|
the output from the shell script appear in your terminal. If the guest
|
2013-09-06 16:50:43 +00:00
|
|
|
machine is already running from a previous step, run `vagrant reload --provision`,
|
2013-09-03 18:08:28 +00:00
|
|
|
which will quickly restart your virtual machine, skipping the initial
|
2013-09-06 16:50:43 +00:00
|
|
|
import step. The provision flag on the reload command instructs Vagrant to
|
|
|
|
run the provisioners, since usually Vagrant will only do this on the first
|
|
|
|
`vagrant up`.
|
2013-09-03 18:08:28 +00:00
|
|
|
|
|
|
|
After Vagrant completes running, the web server will be up and running.
|
2016-01-19 18:08:53 +00:00
|
|
|
You cannot see the website from your own browser (yet), but you can verify
|
2013-09-03 18:08:28 +00:00
|
|
|
that the provisioning works by loading a file from SSH within the machine:
|
|
|
|
|
|
|
|
```
|
|
|
|
$ vagrant ssh
|
|
|
|
...
|
2016-01-19 18:08:53 +00:00
|
|
|
vagrant@precise64:~$ wget -qO- 127.0.0.1
|
2013-09-03 18:08:28 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
This works because in the shell script above we installed Apache and
|
|
|
|
setup the default `DocumentRoot` of Apache to point to our `/vagrant`
|
|
|
|
directory, which is the default synced folder setup by Vagrant.
|
|
|
|
|
|
|
|
You can play around some more by creating some more files and viewing
|
2016-01-19 18:08:53 +00:00
|
|
|
them from the terminal, but in the next step we will cover networking
|
2013-09-03 18:08:28 +00:00
|
|
|
options so that you can use your own browser to access the guest machine.
|
2014-12-19 15:38:56 +00:00
|
|
|
|
2016-01-19 18:08:53 +00:00
|
|
|
-> **For complex provisioning scripts**, it may be more efficient to package a
|
|
|
|
custom Vagrant box with those packages pre-installed instead of building them
|
|
|
|
each time. This topic is not covered by the getting started guide, but can be
|
|
|
|
found in the [packaging custom boxes](/docs/boxes/base.html) documentation.
|
|
|
|
|
|
|
|
## Next Steps
|
|
|
|
|
|
|
|
You have successfully provisioned your first virtual machine with Vagrant. Read
|
2017-03-28 01:19:52 +00:00
|
|
|
on to learn about [networking](/intro/getting-started/networking.html).
|