Welcome to the second blog post about Vagrant. If you have not read it please do because in this part we will pick up where we left off the last time and finish with fully configured PHP, Apache and MySQL development environment.
When using the default
Vagrantfile that we created previously a VM gets only 512MB of RAM, one network interface and one shared folder. It’s not bad but we want to customize it to better suit our needs.
For example, the name of the VM doesn’t mean anything. It might be nothing but if you work with few projects and have many VMs it’s convenient to have them named in some meaningful way.
So to change it lets open the
Vagrantfile we created previously and scroll to the
Provider-specialfic configuration section:
# Provider-specific configuration so you can fine-tune various # backing providers for Vagrant. These expose provider-specific options. # Example for VirtualBox: # # config.vm.provider "virtualbox" do |vb| # # Display the VirtualBox GUI when booting the machine # vb.gui = true # # # Customize the amount of memory on the VM: # vb.memory = "1024" # end # # View the documentation for the provider you are using for more # information on available options.
Provider specific configuration section is where you configure
VirtualBox specific details for the VM. Let’s remove comments and edit some of the configuration options.
It should look like this:
# Provider-specific configuration so you can fine-tune various # backing providers for Vagrant. These expose provider-specific options. # Example for VirtualBox: # config.vm.provider "virtualbox" do |vb| # Display the VirtualBox GUI when booting the machine vb.gui = false # Set the name for the VM vb.name = "MyProject" # Customize the amount of memory on the VM: vb.memory = "1024" end # # View the documentation for the provider you are using for more # information on available options.
We instructed the
VirtualBox that our VM:
- is not using GUI,
- should be named “MyProject”,
- and have at least 1024MB of RAM.
Now lets change the network configuration for the VM. By default it’s configured in a way that can be accessed only through port forwarding. To change the way network is configured scroll to network configuration section:
# Create a private network, which allows host-only access to the machine # using a specific IP. # config.vm.network "private_network", ip: "192.168.33.10" # Create a public network, which generally matched to bridged network. # Bridged networks make the machine appear as another physical device on # your network. # config.vm.network "public_network"
Here you have two choices:
private_network– which will make the VM accessible only by the host OS
public_network– which will make the VM appear as additional device on the network the host OS is connected to. For this tutorial we will use the
private_networkwith IP address set the same way as in the example. So remove the comment from line where the
The last thing we want to change is the guest OS hostname. Right now it’s something like this:
It’s also not very meaningful.
To change it, scroll up
Vagrantfile to where
config.vm.box line is and add the following line below it:
config.vm.hostname = "guest"
While we are there we will also uncomment the line:
config.vm.box_check_update = false
Which will make starting the VM a bit faster because Vagrant won’t check if the box we are using has updates.
Those are all the changes we want to do for now. Save and close the
Because the VM is already running to see our changes we must reload the VM with:
$ vagrant reload
halt the VM and then start it
After stating the VM you should see the the changes in the startup log and VirtualBox GUI.
OK we have the VM but what about the PHP, Apache and MySQL?
We could install all of them with
apt-get install but then VM would have to be configured by hand each time it’s started for the first time by us or some other developer for that matter. This is not what we want.
We want VM to have an ability to setup our development environment with one simple command.
This is what provisioning is for.
Provisioning is basically a script or series of scripts Vagrant calls as
root user when the VM starts for the first time. You can configure what script to run in
config.vm.provision configuration option like this:
config.vm.provision :shell, :path => "provision.sh"
Add it just before the final
end keyword and save the file.
Now we have to create the file in the same directory where
Vagrantfile is. The path to
provision.sh file is relative to it.
Create the file with contents like this:
#!/bin/bash apt-get update apt-get upgrade
Then make it executable with:
$ chmod +x provision.sh
That’s it. Now every time you start this VM for the first time it will run this script.
But if you try to issue:
$ vagrant halt $ vagrant up
The script will not run. This is because it’s not the first time the VM was started. To make sure script has been executed do:
$ vagrant destroy $ vagrant up
$ vagrant halt $ vagrant up --provision
Setting up the environment.
Now you know enough about Vagrant and
Vagrantfile to create your own development environments that your projects require. But to get you started I created for you a bootstrap project on Github.
I won’t be going into the details how the provisioning scripts were created but I’ll show you enough to get you started creating your own scripts.
First we have to clone the git repository. To do that go to my repository on GitHub and copy the URL for the repository.
I will clone it to my home directory with:
$ cd ~
$ git clone https://github.com/rzajac/vagrant-prov
$ cd vagrant-prov
Lets look at it:
$ tree . ├── LICENSE.txt ├── README.md ├── Vagrantfile ├── project │ ├── index.html │ └── info.php └── provision ├── config.sh ├── core-admin.sh ├── core-core.sh ├── core-swap.sh ├── main-provision.sh ├── mysql-seed.sql ├── prov-apache.sh ├── prov-memcached.sh ├── prov-mysql.sh ├── prov-nginx.sh ├── prov-php5-apache.sh ├── prov-php5-nginx.sh └── prov-php5.sh
The repository has
Vagrantfile which is very similar to what we created in this tutorial and multiple provisioning scripts.
The script that interests you the most is
provision/config.sh this is where you can decide how the development environment should be configured. All the scripts have extensive comments to help you learn how to do it yourself.
For more information please read
That’s it! Now you should be able to create development environments for your projects and share the “recipe” of how to make it with other developers.