How to Create a Virtual Host: A Step-By-Step Guide
Let’s create a virtual host by using the Apache hosts and the Ubuntu OS. We use both of these because they are user-friendly, reliable, and most trusted.
Step 1: Update The Softaware
- The prerequisites for the creation of a virtual host are Ubuntu OS and Apache installation. You can do it using the following commands if you don’t have Apache installed.
sudo apt-get update
Step 2: Install Apache Software
Now, install the apache software on the operating system such as Ubuntu with the following command:
sudo apt-get install apache2
- Let’s start with the step-by-step process.
Step 3: Directory structure creation
- In the first step, we will create a directory structure to store the website data or accept data from visitors. You need to use the following command under the document root – /var/www directory.
sudo mkdir -p /var/www/app2.pranshtech.com/public_html
Step 4: Give Permissions
- Generally, root users have ownership of the directory structure. However, you can change it using the following command.
sudo chown -R $USER : $YOUR_USER_NAME/var/www/app2.pranshtech.com/public_html
Step 5: Creation of the demo page for every virtual host
- Now, we will create a sample test page for pranshtech.com. To create it, you can use vi or any other editor.
- Type the following command to create the demo page for pranshtech.com
vi /var/www/app2.pranshtech.com/public_html/index.html
- In the index.html file, you need to type the following basic HTML code.
<html>
<head>
<title>Home Page of pranshtech.com <title>
</head>
<body>
<h1>Hurrah! The virtual host of app2. pranshtech.com is working properly. </h1>
</body>
</html>
Step 6: New file setup
- Apache contains the default host configuration file named 000-default.conf. We will use the same to set up the new file for our virtual host.
- So, let’s copy this file to pranshtech.com using the following command.
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sitesavailable/pranshtech.conf
- Once the file is copied, you can open it using your preferred editor.
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
- Now, change the email address. ServerAdmin admin@pranshtech.com , In the next step, add ServerName and ServerAlias using the following command.
ServerName pranshtech.com
ServerAlias www.app2.pranshtech.com
- Now, the final step is to change the document root’s location. Use this command for it.
DocumentRoot /var/www/app2.pranshtech.com/public_html.
Step 7: File Enabling
- Let’s enable our virtual files through the following command.
sudo a2ensite app2.pranshtech.com.conf
- Once it is done, you need to restart the Apache server.
sudo service apache2 restart
Step 8: Test Web Hosting
It is result time. Open your web browser and enter pranshtech.com. You will see the result like this –
Hurrah! The virtual host of pranshtech.com is working properly.
How Does Virtual Hosting Works?
Your tech stack’s performance and speed greatly matter in today’s tech-centric digital era. Investing in these tools is quite expensive as it costs thousands of bucks to maintain them. That’s why, as a business owner, you can make a prudent decision to find some middle ways to get things done in zero-to-nominal investments.
One such way is to opt for virtual web hosting, as it will help you cut your IT costs. And the best thing about virtual hosting is that you don’t need to compromise on quality. Virtual web hosting lets you have your business’s online presence without much tech knowledge and resources. Without further ado, let’s dive into it.