After completing Vagrant Networking we will continue with Vagrant Provisioning.
In this section we will see how to automate the first configuration and installation of software on our virtual machines. Remember that we before installed Apache Web Server. Now lets do it by provisioning in an automated way. To do this we will write a simple script. We only need “
apt-get update” to update software lists and after that “
apt-get -y install apache2“. Our final shell script will be like the one below:
Create a file under your vagrant project folder with name provision.sh and write the details above. Now lets check the contents of the Vagrantfile:
In the Vagrantfile we are creating an Ubuntu 14.04 virtual machine. There is also port forwarding defined and lastly our shell code is defined. Now run “
vagrant up” and see what happens: You will see the “
apt-get update" output and after that the installation of apache. Check if it is really installed and running.
Lastly from your host machine check the apache server gives and output by checking URL http://localhost:8080
Install MySQL Server and PHP:
Now lets make the scenario a bit complicated. We will install Mysql server and php5 and the php5-mysql package. This will create a fully running LAMP (Linux-Apache-Mysqlserver-Php) server for us.
Above we added two new lines: line 2 and 3. These lines are needed to answer the question that mysql-server asks for the root password. We are setting the question answers so the installation will complete without any problem. The root password for mysql-server will be mypass. In the last line we added “mysql-server, php5 and php5-mysql” details. Change the provision.sh file accordingly. Run “
vagrant destroy --force” to destroy the previously created virtual machine. Run again “
vagrant up” and see what is happening.
Now lets see if we can connect to mysql server and if php5 is installed:
1- We checked if mysql service is running with “
service mysql status”
2- With “
mysql -u root -p” we tried to connect the mysql server and entered password and saw that it is working
3- With “
sudo apt-cache policy php5” we checked if php5 installation is successful.
We have completed “Vagrant Provisioning” here. As a final notice this section only covers provisioning with shell script. There are also other possibilities like Puppet and Chef.
Next Post: Vagrant ? Create a Puppet Server and Agent