How to Setup a Static Website on a CentOS 7 Virtual Machine
Tags: virtualmachine, centos7, tutorial, beginners
Published: 2022-09-25 14:36:53

As defined by Wikipedia, a [virtual machine](https://en.wikipedia.org/wiki/Virtual_machine) is the virtualization of a computer system. Virtual machines have their CPU, memory, and operating system and can act independently of the host.

Virtual machines are especially useful in this age of large multi-user applications that require continuous updates from various teams across various departments. Virtual machines help mitigate complications due to the difference in the host machine's hardware specifications.

## Prerequisites
To get started with this article, we must have the following:
- Virtualization Technology (VT) enabled in BIOS. To understand how to enable VT, check out this [article](https://www.thewindowsclub.com/disable-hardware-virtualization-in-windows-10#:~:text=ON%20the%20System.-,Press%20F2%20key%20at%20startup%20BIOS%20Setup.,changes%20and%20Reboot%20into%20Windows.)
- A Command line interface (CLI) installed on the computer. For Windows users, it is advisable to use GIT Bash to run the vagrant commands. To download GIT Bash, check out this [page](https://git-scm.com/downloads)
- A virtualization product like [VirtualBox](https://www.virtualbox.org/), install VirtualBox from [here](https://www.virtualbox.org/wiki/Downloads)
- A code editor

## Creating a Centos7 virtual machine
To create our virtual machine, we run this terminal command to create a directory that will house our project.
```bash
    mkdir <name of our project>
```
Next, we change the current directory to our newly created directory.
```bash
    cd <name of our project>
```
After creating our project directory, we create a directory that will hold our website template.
```bash
    mkdir <name of our website>
```
We then change our current directory to our website directory.
```bash
    cd <name of our website>
```

**Creating virtual machines using vagrant box**
[Vagrant boxes](https://phoenixnap.com/kb/vagrant-beginner-tutorial#ftoc-heading-4) are packaged ready-made vagrant environments that can be downloaded and used to create virtual machines. We discover vagrant boxes from the [Vagrant cloud.](https://app.vagrantup.com/boxes/search) 

In this article, we will use the [**geerlingguy/centos7**](https://app.vagrantup.com/geerlingguy/boxes/centos7) virtual box. 

![CentOS 7 Vagrant box](https://paper-attachments.dropbox.com/s_5FAFC89FCE8CECD27751E142759497D7F390C247FBD92D0ACEE3EBAF05A26A40_1664004755843_Screenshot+589.png)


This terminal command creates a vagrant file with the 'geerlingguy/centos7' virtual box.
```bash
    vagrant init geerlingguy/centos7
```
We should see this in our terminal.

![Vagrant box download Output](https://paper-attachments.dropbox.com/s_5FAFC89FCE8CECD27751E142759497D7F390C247FBD92D0ACEE3EBAF05A26A40_1664050017411_Screenshot+590-1.png)


Next, we go to the  newly created `Vagrantfile` file on our laptop and open it up with any available code editor.

![CentOS 7 Vagrantfile](https://paper-attachments.dropbox.com/s_5FAFC89FCE8CECD27751E142759497D7F390C247FBD92D0ACEE3EBAF05A26A40_1664005188408_Screenshot+591.png)


The `Vagrantfile` contains this information.

![Vagrantfile information](https://paper-attachments.dropbox.com/s_5FAFC89FCE8CECD27751E142759497D7F390C247FBD92D0ACEE3EBAF05A26A40_1664005347205_Screenshot+592.png)


 Next, we uncomment the following lines: 
-  **line 35** : The `config.vm.network "private_network", ip: "192.168.33.10"` line contains the IP address that we use to access our website
- **line 40**: The `config.vm.network "public_network"` line gives us a dynamic IP address
- **line 52, line 57, line 58**: These lines give our virtual machine more RAM as the default CentOS 7 RAM is 512MB.

Here is how our `Vagrantfile` looks:
![Vagrantfile](https://paper-attachments.dropbox.com/s_5FAFC89FCE8CECD27751E142759497D7F390C247FBD92D0ACEE3EBAF05A26A40_1664051678111_Screenshot+610.png)
![Vagrantfile](https://paper-attachments.dropbox.com/s_5FAFC89FCE8CECD27751E142759497D7F390C247FBD92D0ACEE3EBAF05A26A40_1664051678133_Screenshot+611.png)


Next, we save the `Vagrantfile` and start the virtual machine. Start up the virtual machine with this terminal command.
```bash
    vagrant up
```
![Start up a virtual machine output](https://paper-attachments.dropbox.com/s_5FAFC89FCE8CECD27751E142759497D7F390C247FBD92D0ACEE3EBAF05A26A40_1664050080215_Screenshot+593-2.png)

We then log in to the virtual machine.
```bash
    vagrant ssh
```
![Login to a Virtual Machine](https://paper-attachments.dropbox.com/s_5FAFC89FCE8CECD27751E142759497D7F390C247FBD92D0ACEE3EBAF05A26A40_1664050122600_Screenshot+594.png)


Next, we change to the root user.
```bash
    sudo -i
```
![Root User](https://paper-attachments.dropbox.com/s_5FAFC89FCE8CECD27751E142759497D7F390C247FBD92D0ACEE3EBAF05A26A40_1664050150518_Screenshot+595.png)


Next, we install the **httpd**, **wget**, and **unzip** packages with this command.
```bash
    yum install httpd wget unzip -y
```
These packages do the following:

- **httpd:** The httpd package plays the role of a server in our application
- **wget:** The wget package will download the website template
- **unzip** : The unzip package will unzip the downloaded template

After installing our packages, we start our server and enable it so that the server will come up when the virtual machine boots up.
```bash
    systemctl start httpd 
    systemctl enable httpd
```
![Restarting the httpd server](https://paper-attachments.dropbox.com/s_5FAFC89FCE8CECD27751E142759497D7F390C247FBD92D0ACEE3EBAF05A26A40_1664050240593_Screenshot+597.png)

To access our website IP address, we run this terminal command.
```bash
    ip addr
```
![Access our IP address](https://paper-attachments.dropbox.com/s_5FAFC89FCE8CECD27751E142759497D7F390C247FBD92D0ACEE3EBAF05A26A40_1664053738775_Screenshot+599.png)

Next, we copy the 'inet' IP address and paste it into our browser to see the default page.

![IP address](https://paper-attachments.dropbox.com/s_5FAFC89FCE8CECD27751E142759497D7F390C247FBD92D0ACEE3EBAF05A26A40_1664050291988_Screenshot+598.png)

Use the 'inet' IP address in the "3:" section. In the picture above, we use [192.168.33.10](http://192.168.33.10).

We should see this page in our browser.

![Apache Default Page](https://paper-attachments.dropbox.com/s_5FAFC89FCE8CECD27751E142759497D7F390C247FBD92D0ACEE3EBAF05A26A40_1664052363934_180325104132958289.jpg)

**Getting the website template**
In this section of the tutorial, we are going to get the download link to a website template and download the template in our project.

To get the download link, follow these steps:

- Go to the [tooplate website](https://www.tooplate.com/) and choose a template
![Tooplate Website template](https://paper-attachments.dropbox.com/s_5FAFC89FCE8CECD27751E142759497D7F390C247FBD92D0ACEE3EBAF05A26A40_1664050845122_Screenshot+608.png)

- Click **ctrl** **shift** **i** to open up the chrome console
![Google Chrome Console](https://paper-attachments.dropbox.com/s_5FAFC89FCE8CECD27751E142759497D7F390C247FBD92D0ACEE3EBAF05A26A40_1664050864258_Screenshot+609.png)

- Navigate to the **Network** tab and click the **download** button on the template page.
![Chrome Network Console](https://paper-attachments.dropbox.com/s_5FAFC89FCE8CECD27751E142759497D7F390C247FBD92D0ACEE3EBAF05A26A40_1664050883859_Screenshot+600.png)

- In the **Network** tab, click on the zip file, and copy the **Request URL.** This **Request URL** is the download link to the website template
![Chrome Network Console](https://paper-attachments.dropbox.com/s_5FAFC89FCE8CECD27751E142759497D7F390C247FBD92D0ACEE3EBAF05A26A40_1664050969577_Screenshot+602.png)

**Downloading the website template in our project**
We change the current directory to the `/tmp/` directory in our project. 
```bash
    cd /tmp/
```
The `/tmp/` directory is a temporary landing place for files, and it will be responsible for holding our website template in the meantime.

To download the website template, run this terminal command.
```bash
    wget <link to the website template we copied>
```
![Downloading Website Template](https://paper-attachments.dropbox.com/s_5FAFC89FCE8CECD27751E142759497D7F390C247FBD92D0ACEE3EBAF05A26A40_1664051381045_Screenshot+601.png)

To verify that we have successfully downloaded the website template, run this terminal command.
```bash
    ls
```
We then unzip this file with this command.
```bash
    unzip <name of the zip file>
```
![Unzip Website Template](https://paper-attachments.dropbox.com/s_5FAFC89FCE8CECD27751E142759497D7F390C247FBD92D0ACEE3EBAF05A26A40_1664050999418_Screenshot+604.png)

To see our unzipped file, run this command.
```bash
    ls
```
![Unzipped files](https://paper-attachments.dropbox.com/s_5FAFC89FCE8CECD27751E142759497D7F390C247FBD92D0ACEE3EBAF05A26A40_1664051421006_Screenshot+604.png)

To see all the files and directories in our unzipped file, we change the directory to our unzipped file and run the `ls` command.
```bash
    cd <name of your unzipped website template>
    ls
```
![List of files in the unzipped template folder](https://paper-attachments.dropbox.com/s_5FAFC89FCE8CECD27751E142759497D7F390C247FBD92D0ACEE3EBAF05A26A40_1664051504811_Screenshot+605.png)


**Hosting the website in our virtual machine**
We store the HTML files we see when we navigate to our `inet` IP address in the `/var/www/html` directory. In the `/var/www/html` directory, the first file the server renders by default is the `index.html`.

To see our website in the browser, we copy the files in the unzipped template to our `/var/www/html` directory. Run this command to copy each file and directory to the `/var/www/html` directory.
```bash
    cp -r * /var/www/html
```
To verify that we successfully copied the files and directories, change the current directory to `/var/www/html` and run the `ls` command.
```bash
    cd /var/www/html
    ls
```
![The html directory](https://paper-attachments.dropbox.com/s_5FAFC89FCE8CECD27751E142759497D7F390C247FBD92D0ACEE3EBAF05A26A40_1664051550362_Screenshot+606.png)

Next, we restart our server to see the changes in our browser.

![Website in our CentOS 7 ](https://paper-attachments.dropbox.com/s_5FAFC89FCE8CECD27751E142759497D7F390C247FBD92D0ACEE3EBAF05A26A40_1664051583358_Screenshot+607.png)

## Conclusion
This article discussed downloading and hosting a website template on a CentOS 7 virtual machine.

## Resources

The following resources may be helpful:
[](https://dev.to/amaraiheanacho/understanding-linux-and-its-commands-48fa)
- [Understanding Linux and Its Commands](https://dev.to/amaraiheanacho/understanding-linux-and-its-commands-48fa)
- [Introduction | Vagrant by HashiCorp](https://www.vagrantup.com/intro)
- [What is a virtual machine (VM)?](https://azure.microsoft.com/en-us/resources/cloud-computing-dictionary/what-is-a-virtual-machine/#overview)