IaC — Website setup on ubuntu Server.

IaC — Website setup on ubuntu Server.

Introduction

Infrastructure as Code (IaC) and manual provisioning are two different approaches to managing and provisioning computing infrastructure.

In manual provisioning, administrators manually configure and manage individual servers and services. This approach is time-consuming, error-prone, and difficult to scale as the number of servers and services grows. It also lacks consistency and repeatability, which can cause issues when trying to recreate the same infrastructure in different environments.

In contrast, IaC treats infrastructure like code, meaning that infrastructure components are defined in configuration files and scripts that can be managed, version-controlled, and automated. IaC tools like Terraform and Ansible enable teams to create and manage infrastructure components in a repeatable, scalable, and consistent way.

In this demo, we will host a website on an ubuntu machine using vagrant.

Prerequisites:

  1. Vagrant: click here to get started with installing and using vagrant.

  2. Website files: I will be using the same single page website of my CV from the Automating code deployment using Amazon CodePipeline demo.

  3. Patience 😄.

Follow the steps below to achieve this task:

Step 1: create directory and VagrantFile for the project.

i. Create a file, ‘website_script.sh’ and paste the commands.

#create directory
mkdir vms
cd vms
mkdir cv_website
cd cv_website

#website files
cd cv_website
mkdir html_files
cd html_files
git init
git clone https://github.com/Kelechi-DE/Automating-code-deployment-through-CI-CD.git
cd ..

#create VagrantFile
vagrant init

ii. Run the commands below to execute the script.

#give execution permissions
chmod u+x website_script.sh

#execute script
. ~/website_script.sh

Step 2: specify the configuration of the infrastructure in the Vagrantfile.

Make sure you are in the ‘cv-website’ directory created by the script in the previous step.

i. Run the command below to open the vagrantfile in the vs code editor.

code Vagrantfile

Fundamental configurations:

  • choose the box for vm — we will use spox/ubuntu-arm for this demonstration.

  • an ip address to access our website

  • (optional) Boost RAM

  • install apache2 — Apache2 is a free, open-source web server software that is widely used to serve websites on the internet.

To declare the above configuration, copy and paste the following in the vagrantfile.

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|


  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "spox/ubuntu-arm"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
   config.vm.network "private_network", ip: "192.168.57.11"

  # 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"


  config.vm.provider "vmware_desktop" do |vmware|
    # Display the VirtualBox GUI when booting the machine
    vmware.gui = true

    # Customize the amount of memory on the VM:
    vmware.memory = "1024"
  end

  config.vm.provision "shell", inline: <<-SHELL
    apt update
    apt install -y apache2
    cp /vagrant/html_files/Automating-code-deployment-through-CI-CD/* /var/www/html/
    systemctl restart apache2

  SHELL
end

Note:

/var/www/html/ is a common directory used in Linux and Unix-based systems to store website files served by the Apache web server. It stores HTML, PHP, and other web-related files, while the /var and /www directories store variable files and website files respectively. The web server looks for files in /html and serves them to users starting from the index.html. Files are typically owned by the www-data user and require read and execute permissions for the web server to serve them.

In Vagrant, the sync directory is a feature that allows a directory on the host machine to be shared with the virtual machine, allowing files to be easily shared between the two environments. the files in the directory where the Vagrantfile is created is usually synced with /vagrant/ directory.

Hence, the reason why we wrote commands to copy the html files from to /vagrant/html_files/Automating-code-deployment-through-CI-CD/* to /var/www/html/.

ii. Run the command to build infrastructure:

vagrant up

Step 3: Verify the VM setup and website hosting.

i. We run the command below to login to the virtual machine via ssh.

vagrant ssh

ii. Then we run the command below to get the ip address for the virtual machine.

ip addr show | grep eth1

IP address for VM

iii. copy and paste the ip address in a web browser.

CV website

Summary

Overall, while manual provisioning can work for small-scale infrastructure management, IaC provides a more efficient, reliable, and scalable way to manage computing infrastructure, particularly in complex and dynamic environments. This article discusses the benefits of using Infrastructure as Code (IaC) for managing computing infrastructure. It walks through a demo of setting up a website on an Ubuntu virtual machine using Vagrant and shows how to define infrastructure components in configuration files and scripts for consistent and repeatable deployments. The article concludes that IaC provides a more efficient, reliable, and scalable way to manage computing infrastructure, particularly in complex and dynamic environments.

Thanks for reading.