Engineer Jagat

IT & Engineering Stuff

How to Set Up Apache Virtual Hosts on Linux?

May 11, 2021
How to Set Up Apache Virtual Hosts on Linux?

If you are System Administrator or Developer, this article really gonna beneficial for you. In this article, you will learn to set up Apache Virtual Hosts on Linux.

Follow the Step-by-Step Guide

Step 1: Install and Update

The very first thing is to install Apache on your System, if you have not installed yet try this link to install Apache on Linux.

If you have already installed, then update your system using this command

$ sudo apt update

Step 2: Create Directory

After updating the system, you need to create directory of your project where it goes to be hosted. So, to do that you need to execute this command.

$ sudo mkdir -p /var/www/

Step 3: Grant Permission

While we create the project directory using sudo then the project directory has access permission under root, which means the only root creates files on that directory so we have to grant permission to other users. To do that we have to execute this command.

$ sudo chown -R youruser /var/www/

Step 4: Create Sample Page

You need to create sample page, you can create sample page using nano editor, sublime or any text editor which is installed on your system but nano is by default installed so if you do not have installed another one then try using Nano.

$  nano /var/www/

So, in index.html file you have create code like this

<!DOCTYPE html>
<html lang="en">

    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello World</title>

    <h1>Hello World</h1>


Step 5: Create Virtual Host Files

Linux system has by default Virtual Host file which is named 000-default.conf , so you need to create new Virtual Host File. To create new Virtual Host File use this command.

$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/

After creating a New Virtual Host File, you need to open it and need to change some lines of code that are highlighted.

$ sudo nano /etc/apache2/sites-available/

Step 6: Enable the New Virtual Host Files

After editing the code, you need to enable the new Virtual Host Files.So, to do that you have to execute
this command.

$ sudo a2ensite

After changing, anything on Apache, we need to restart the Server, So execute this code to restart

$ sudo systemctl restart apache2

Step 7: Configure Local Hosts File

Configuring on Local Hosts File is really needed because we do not have domains for testing so
because of set up in Local Hosts File it creates temporary domains. To do that execute this command.

$ sudo nano /etc/hosts

Step 8: Test Your Sample Site

Finally, we have done all operations to set up Apache Virtual Host on Linux. Now you can test your
experiment, so to do that just hit the site name which you are set up in virtual host.

Share with Friends
Copyright © 2020-2021 Engineer Jagat All rights reserved. Developed by Ravindra Rana