Skip to content

Wealthiscertain/Automating-Website-Hosting-on-a-CentOS-VM-with-Vagrant

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

Automating-Website-Hosting-on-a-CentOS-VM-with-Vagrant

image

In my previous article, I provided a manual guide on hosting a website on a CentOS VM. In this post, I'll walk you through the process of automating the hosting of an HTML template on CentOS using httpd and Vagrant. We will create a Vagrantfile that provisions the website automatically, leveraging the power of Infrastructure as Code (IaC).

Tools used

  • Ready-made template: Free HTML website templates from tooplate.com
  • Vagrant: Automates the creation and management of virtual environments.
  • CentOS: A free, enterprise-class Linux distribution, ideal for server environments.
  • Git Bash: A command-line tool that allows users to run Git commands and other Unix-based utilities on Windows.
  • VsCode: A source-code editor for development

Understanding Infrastructure as Code (IaC)

Infrastructure as Code (IaC) is the practice of managing and provisioning computing infrastructure through machine-readable configuration files. This approach allows for consistent, repeatable, and automated deployment processes, eliminating the need for manual setup.

Step-by-Step Setup

1. Create a CentOS VM with Vagrant First, create a CentOS VM using Vagrant. For guidance, refer to my previous article for a detailed walkthrough.

2. Prepare Your Workspace in VSCode

  • Open Visual Studio Code

  • Click on File > Open Folder

  • Navigate to the folder of the CentOS VM you created

    image

3. Rename the Project Folder

  • Rename the Folder:
  • Change the name of your project folder (e.g., from finance) to financeIAC.
  • This signifies that the project now utilizes Infrastructure as Code principles.

image

image

4. Remove the .vagrant Directory

  • Delete the .vagrant Folder:
  • In the financeIAC directoru, delete the .vagrant directory to ensure a clean environment. This folder contains state information that Vagrant uses, and removing it allows Vagrant to recreate the VM from scratch.

image

5. Modify the Vagrantfile a. Update Network Configuration

  • Open Vagrantfile in VSCode.
  • Change the IP Address to 192.168.56.28:

image

b. Add Provisioning Script

  • Add the Following Script at the bottom of the Vagrantfile:
  yum install httpd wget unzip vim -y
  systemctl start httpd
  systemctl enable httpd
  mkdir -p /tmp/finance
  cd /tmp/finance
  wget https://www.tooplate.com/zip-templates/2135_mini_finance.zip
  unzip -o 2135_mini_finance.zip
  cp -r 2135_mini_finance/* /var/www/html/
  systemctl restart httpd
  cd /tmp/
  rm -rf /tmp/finance
SHELL

image

  • Save the Vagrantfile (use Ctrl + S).

6. Launch the VM Using Git Bash a. Open Git Bash:

  • Navigate to the financeIAC directory:
  • Use the command cd /vagrant-vms/financeIAC

image

b. Start the VM, using the vagrant up command.

  • Vagrant will create the VM and automatically run the provisioning script defined in the Vagrantfile.

image

image

7. Access the Deployed Website

  • Open a Web Browser, and Navigate to http://192.168.56.28/

  • You should see the hosted website loaded from the VM.

    image

Conclusion

By automating the provisioning of the CentOS VM and the deployment of the website, we've transformed a manual process into a reproducible and efficient workflow. Leveraging Vagrant and the principles of Infrastructure as Code, you can easily manage and deploy environments consistently.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published