One of the reasons why you would consider Vagrant is because you seek to deploy your application so that other users can have access to it. It is a traditional method of deployment in the sense that it you need to install your application manually or through a scripted installer on a stock server machine.
There are other servers that can be used for deployment. Vagrant, unlike the rest, is free.
To have vagrant running on your machine, you will need two tools:
- VirtualBox and
- Vagrant (itself)
You will also need:
- Root previlidges (sudo) and
- A terminal window
If you are not sure whether you have vagrant or not, run this command on your terminal:
$ vagrant --version
VirtualBox
is a software utility that allows you to run an operating system inside an operating system. It does this by creating a virtual environment. Since Vagrant creates virtual operating systems, it needs a tool like VirtualBox
to manage the virtual operating systems.
To install VirtualBox
, run:
$ sudo apt install virtualbox
To check that you actually have VirtualBox
installed, run:
$ which virtualbox
Successful installation will show you a PATH to VirtualBox
.
There are two ways you can follow to install vagrant
:
$ sudo apt install vagrant
Your system will download and install the software. However, the challenge with this is that there is a likelihood your system is not up to date with the latest version of the software.
To check for the latest update, visit the official Vagrant Download Page here and download the appropriate one for your OS.
To check the version installed in your system, run:
$ vagrant --version
From the official HashiCorp Download Page, you can find the latest version of vagrant
. Before you download the latest version of vagrant
, it is good practice to look for any updates you might have. Then download your version by running these two commands:
$ sudo apt update
$ curl -O https://releases.hashicorp.com/vagrant/2.2.x/vagrant_2.2.x_x86_64.deb
Note: Change the 2.2.x to the appropriate version you are downloading
We are using curl
to get the file and download it. For more information on curl
, visit:
Further reading on:
To install vagrant
, you need to run:
$ sudo apt install ./vagrant_2.2.X_x86_64.deb
To verify that the installation was successful, run:
$ vagrant --version
You should be able to see the exact version you downloaded and installed.
Since you are going to use vagrant
to run your project locally, the first thing you want to do is to create a directory that will host your application. To do this, you can:
$ mkdir my_new_project
$ cd my_new_project
Now that you are inside your new project, you need to initialize vagrant
. You can accomplish this in two ways:
First, run:
$ vagrant init ubuntu/bionic64
This will create an Ubuntu disk image within your current working directory.
Alternatively,
You can choose to create a vagrant
configuration file before running Vagrant. This can be accomplished by creating an empty file:
$ touch Vagrantfile
Using your favorite text editor, you can populate the file with your configurations. For example,
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
config.vm.network "private_network", ip: "192.168.33.10"
config.vm.provider "virtualbox" do |vb|
vb.memory = "1024"
end
end
This file will be useful when you try to create and configure the virtual machine as specified in your config file.
Now that you have a configuration file, run:
$ vagrant up
After completion, vagrant
will mount the project directory at /vagrant in the virtual machine which allows you to work on your project's files on your host machine.
NOTE: What are vagrant boxes?
You have seen me use:
$ vagrant init ubuntu/bionic64
What exactly is <ubuntu/bionic64>
?
Boxes are the package formats for vagrant
environments. Factually, the list is endless on available boxes, as seen here. You can search for a box of your choice. In this guide, I am using the disk image of Ubuntu 18.04 LTS.
Here are the official Ubuntu Cloud Images that have been customized to run on public clouds that provide Ubuntu Certified Images, Openstack and more.
Thing is, you do not have an app on your desktop to connect to your server. You will need to work through the command-line/terminal and connect to your server through an SSH
client.
If you are using a Vagrant machine, (which you are now) you can simply run:
$ vagrant ssh
You can also choose to create a user to connect to your server. To do this, you will need to log into your server's root account using ssh
and do the following:
$ adduser --gecos "" ubuntu
$ usermod -aG sudo ubuntu
$ su ubuntu
This creates an ubuntu user. What you should note is, at this point, if you ever logout
of your server, you will need to repeat the above steps (summarized below) to get to your user:
$ vagrant up
$ vagrant ssh
$ sudo su ubuntu
Alternatively, you can just run this command to get to your new user:
$ vagrant ssh -c "/home/ubuntu; sudo su ubuntu"
To minimize the risk of your server being compromised, you need to tighten available loopholes an attacker might use to gain access. We will use the inbuilt vim
editor (or another of your choice) to help us modify our config
file as follows:
$ sudo vim /etc/ssh/sshd_config
Make the following changes by editing the original file to:
PermitRootLogin no
PasswordAuthentication no
After you are done editing the ssh
configuration, the service needs to be restarted of the changes to take effect:
$ sudo service ssh restart
Block access to the server on any ports that are not explictly enabled:
$ sudo apt-get install -y ufw
$ sudo ufw allow ssh
$ sudo ufw allow http
$ sudo ufw allow 443/tcp
$ sudo ufw --force enable
$ sudo ufw status