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).
- 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
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.
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 VMyou created
3. Rename the Project Folder
- Rename the Folder:
- Change the name of your project folder (e.g., from
finance) tofinanceIAC. - This signifies that the project now utilizes Infrastructure as Code principles.
4. Remove the .vagrant Directory
- Delete the
.vagrantFolder: - In the
financeIAC directoru, delete the.vagrantdirectory to ensure a clean environment. This folder contains state information that Vagrant uses, and removing it allows Vagrant to recreate the VM from scratch.
5. Modify the Vagrantfile a. Update Network Configuration
- Open
VagrantfileinVSCode. - Change the
IP Addressto192.168.56.28:
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
- Save the
Vagrantfile(use Ctrl + S).
6. Launch the VM Using Git Bash a. Open Git Bash:
- Navigate to the
financeIACdirectory: - Use the command
cd /vagrant-vms/financeIAC
b. Start the VM, using the vagrant up command.
- Vagrant will create the VM and automatically run the provisioning script defined in the
Vagrantfile.
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.
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.










