Setup Virtual Host using Apache On ubuntu 22.10

thelinuxterminal

Author - Sanjay

Introduction

Virtual hosts in Apache allow you to host multiple websites on a single server, each with its own unique domain name. This is a useful feature if you want to host multiple websites, but don't have the resources or budget to set up separate servers for each one.

Virtual hosts in Apache are configured using the Apache HTTP Server software, which is a popular web server software used to host websites. Apache supports both IP-based and name-based virtual hosts, which means you can use either the IP address of the server or a domain name to access the website.

Have a look at Installing and Setup Apache on Ubuntu 22.10

Table of Contents

Pre-Requisistes

A Ubuntu server or a Desktop with root privilages or you can get a free vps hosting from Vultr or DigitalOcean

Why do we need to to Set Up Virtual Hosts in Apache

There are several reasons why you might want to set up virtual hosts in Apache:

You want to host multiple websites on a single server: Virtual hosts allow you to host multiple websites on a single server, which can be more cost-effective than setting up separate servers for each website.

You want to test websites before making them live: Virtual hosts can be used to test websites before making them live to the public. This allows you to make sure everything is working correctly before you make the website available to everyone.

You want to host websites for multiple clients: If you are a web developer or designer, virtual hosts can be a useful tool for hosting websites for multiple clients on a single server. This can be more efficient than setting up separate servers for each client.

Pre Requisites for Setting Up Virtual Hosts in Apache2

Prerequisites for Setting Up Virtual Hosts in Apache

Before you can set up virtual hosts in Apache, you will need to make sure you have the following prerequisites:

  • A server running Apache HTTP Server: Virtual hosts are configured using the Apache HTTP Server software, so you will need to have this installed on your server.

  • A domain name for each website you want to host: Each virtual host will need its own unique domain name. If you don't already have a domain name, you will need to purchase one or use a subdomain of an existing domain.

  • Access to the Apache configuration file: You will need to edit the Apache configuration file to set up virtual hosts. This typically requires root access or administrative privileges on the server.

  • A basic understanding of Apache configuration: You will need to know how to edit the Apache configuration file and create virtual host configuration files. Some familiarity with Apache configuration is recommended, but there are many resources available to help you get started.

Steps for Installing Virtual Hosts In Apache

  • Test if http server running properly . Use below command to verify
sudo systemctl status apache

Install apache2

  • Create a virtual Host Configuration for your domain

in my case domain name is thelinuxterminal.com We will copy the apache default configuration and edit it in order to create our own config working .

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/thelinuxterminal.com.conf'

We created a virtual host file named as thelinuxterminal.com.conf.

  • Create a Directory where all the files related to your application on domain will be kept .
sudo mkdir /var/www/html/thelinuxterminal.com
  • Create a Index.html file in the directory for testing with following contents .
<!DOCTYPE html>
<html>
<body>

<h1>Apache  Server</h1>
<p>My first Server.</p>

</body>
</html>
  • Configuring Virtual Host File on Apache

One Of the first step we need to do is open your config file in any of your favourite editor on linux box.

sudo vi /etc/apache2/sites-available/thelinuxterminal.com.conf
  • Enable the Virtual Host Configuration

In order to enable the virtual host configuration execute the following command

sudo a2ensite example.com.conf

Disable the default configuration

sudo a2dissite 000-default.conf
        ServerName leandomainfinder.com
        ServerAlias www.leandomainfinder.com
        ServerAdmin webmaster@leandomainfinder.com
        DocumentRoot /var/www/html/leandomainfinder.com

Our final Virtual Host Config will look something similar to this .

Virtual Host Config

  • Test Virtual Host Configuration

We have enabled virtual host on the domain thelinuxterminal.com.You should be now able to access the virtual host .

Common Issues and Troubleshooting

1. Virtual host redirection not working in Laravel.

Make sure you have the below configuration added in your .conf .

<Directory "/var/www/html/leandomainfinder.com">
    AllowOverride All
    Order allow,deny
    allow from all
    Require all granted
</Directory>

2. Multiple virtual hosts not working properly

If Multiple virtual hosts are not working most probably you are missing on disabling default site config. We can disable this by executing command sudo a2dissite 000-default.conf

3. Virtual host giving a 403 Forbidden error

If you are getting 403 error then you need to check if the follders and user having full read write permissions .

Watch the video and connect us on Telegram.

Conclusion

We have done Virtual host configuration on our Ubuntu 22.10 . Setting up Virtual host is always an issue if you are a beginner . Hope you find this article helpful . Let us Know in comments below .