So we are about to improve our Vagrantfile.
We can go through and make sure that we've got a
provisioning script to install to the web server,
we can map directories so we're able to display the contents of our current
web server content inside our development web server,
and map ports, making it easy to be able to access our web server.
So here we are on the command line of our system.
We're in the Apache directory where the Vagrantfile is,
and if I go through and check the status of my virtual machines,
we had started it before, we can see it's still currently running.
And we can show you some of the real benefits of Vagrant
because I can go through and destroy, delete,
this copy of the virtual machine.
So I can go ahead and delete that.
But once it's deleted, I can re‑clone it again very easily,
and especially if I've got provisioning scripts,
we can make sure it's configured exactly the way that we need,
and that's really what we're going to be taking a look at.
Running the ls command in my Apache directory will show that I've
just got the Vagrantfile in the web directory.
The web directory is currently empty,
but I can copy across a web page or create a simple web page,
but I'm going to copy from the comptia‑automation
and apache and web subdirectory.
We've got this all copied down already for you.
This is still a very simple web page,
but it gives you the idea of some of the things that you could do,
and we can move it across to the web directory.
Now when I move into the web directory,
we can take a look at the page that we've created.
Now in an ideal world, I would suggest this web directory,
apache/web,
should be a git repository itself so we can be synchronizing our development
content with our production content in a very easy mechanism.
But here we go, we've got our production web page,
albeit very simple at the moment, ready in our host file system.
But we've got to be able to make use of this,
as well as adding in other elements that we need to our Vagrantfile.
If we go through then clearing the screen and make sure that I'm in
the Apache directory where the Vagrantfile is,
I'll go ahead then and edit our Vagrantfile.
I can insert a new line.
We can see my auto‑indent working,
taking me back to the config indentation level,
and I can add in a new configuration, so config.vm.provision.
Provision then is going to go through and run some form of
configuration management when our virtual machine boots.
We can use different types of provisioners.
We're going to stick with shell scripting because that's
all we've really looked at so far.
So I'm setting the provisioner type to shell,
and then we set the properties for our shell configure.
We go through and have a comma and then have inline and colon.
That just specifies then that the script is
contained within the Vagrantfile itself.
And we're specifying its value to be $script.
Next up, we can go ahead and add in our synced folders.
This means then that our host folder, web,
can be made accessible through to the virtual machine guest itself.
So we specify our host folder, web, in relationship to the Vagrantfile,
and that's going to be mapped through to var/www/html in the guest,
and that's where the Apache web server's going to expect to
find its content on an Ubuntu system.
For synced folders to work though, we have to have our plugin,
and we'll see how we can install the plugin in a few moments.
The final configuration element that we are adding is going to be our,
oh, look at this, I've got vim.
Every time I type vm I think of vim,
but let's just change that and then add in our config.vm,
and we're adding in our network's settings so I can have a forwarded port.
So this means then I can access a port on my host machine and have
that forwarded to port 80 on our guest machine.
So I'm going through and specifying the guest port to forward to,
so that's got to be port 80, our Apache web server on HTTP,
and the host port.
As long as it is a port that's not being used on the system,
I can add that in, and I'm putting 8080.
Once we've got then our port mappings in place,
we just need to make sure that the $script variable that we've made reference to
for our inline script is available within our Vagrantfile.
Remember, this is all Ruby code,
so we just go through and configure our normal Ruby variable,
$script is assigned the value of,
and we can read in using the Ruby heredoc until we see
the occurrence of the word SCRIPT.
Now it doesn't matter what word that I use,
but we specify the word to look out for,
I'm specifying uppercase SCRIPT,
we just need to make sure that doesn't actually
appear anywhere in my shell script.
With my ending block in place, I can then go through and do a simple apt update,
to ensure then the package cache is up to date,
and then an apt install, using sudo, ‑y and then the apache2 web server.
Now in this case, because it's Ubuntu,
I don't have to worry about starting and enabling my service
because they are done through the package,
but obviously I could write as many lines into this script as
was necessary to get a reliable install.
With the Vagrantfile saved, I can turn my attention to the Vagrant plugin.
If I do a plugin list,
we're able to see the plugins that I currently have installed for Vagrant.
The vagrant‑libvirt was added in by the Vagrant package from Ubuntu.
We don't have the plugin that I want,
so I'm going to do a plugin install and vagrant‑vbguest.
Now we require this plugin to ensure that all guest machines
have the guest additions installed that are required to
allow the synced folders to work.
And once that then has installed, I am ready to go.
I'm working in the Apache directory, so I can then do a vagrant up.
So this is then going to go through the same process as before bringing the
virtual machine up by cloning the existing virtual box.
But during the process, as we start booting up,
we're detecting the guest additions installation was found,
but no additional tools were available to rebuild the kernel to work with them,
so we install those additional tools before making
sure we've got a full virtual box, guest additions installed.
Once they go through, they compile the kernel and drivers necessary.
And now we're onto the process of installing our Apache web server,
we're running our provisioning scripts.
With our provisioning scripts running through,
we should be ready to access our system very shortly.
So we're just allowing this to finish off,
we're enabling and starting our services,
and we are ready to go.
Now I can go through and use the command vagrant
port to show me my port mappings,
so we can see then that the port mappings are in
place both for SSH and for port 80.
So I can then go through and use curl.
Curl's not so much a web browser,
but is a simple connection through to a URL connection,
through to URL, and we're going to go through to http on the localhost.
We're still working on the virtual machine host here and port 8080,
and we can see then the contents of my web page coming back.
But we can see how useful automation can be in our DevOps and CI/CD environment,
and we're actually seeing a very practical use of it even though it's extremely simplified.