Android

Introduction

Wordpress

The Apache web server is the most popular way of serving web content on the internet. It accounts for more than half of all active websites on the internet and is extremely powerful and flexible.

Apache breaks its functionality and components into individual units that can be customized and configured independently. The basic unit that describes an individual site or domain is called a virtual host.

These designations allow the administrator to use one server to host multiple domains or sites off of a single interface or IP by using a matching mechanism. This is relevant to anyone looking to host more than one site off of a single VPS.

Each domain that is configured will direct the visitor to a specific directory holding that site’s information, never indicating that the same server is also responsible for other sites. This scheme is expandable without any software limit as long as your server can handle the load.

In this guide, we will walk you through how to set up Apache virtual hosts on an Ubuntu 16.04 VPS. During this process, you’ll learn how to serve different content to different visitors depending on which domains they are requesting.

Before you begin this tutorial, need to have Apache installed in order to work through these steps.

If you haven’t already done so, you can get Apache installed on your server through apt-get:

After these steps are complete, we can get started.

For the purposes of this guide, our configuration will make a virtual host for example.com

Step One — Create the Directory Structure

The first step that we are going to take is to make a directory structure that will hold the site data that we will be serving to visitors.

I am here creating one directory called workplace and will create directory to hold my site data.

So, example directory by typing below command ,

Step Two — Grant Permissions

ow we have the directory structure for our files, but they are owned by our root user. If we want our regular user to be able to modify files in our web directories, we can change the ownership by doing this:

Step Three — Create Demo Pages for Each Virtual Host

We have our directory structure in place. Let’s create some content to serve.

We’re just going for a demonstration, so our pages will be very simple. We’re just going to make an index.html page for site.

We can open up an index.html file in our editor by typing:

In this file, create a simple HTML document that indicates the site it is connected to. My file looks like this:

Save and close the file when you are finished.

Step Four — Create New Virtual Host Files

Virtual host files are the files that specify the actual configuration of our virtual hosts and dictate how the Apache web server will respond to various domain requests.

Apache comes with a default virtual host file called 000-default.conf that we can use as a jumping off point. We are going to copy it over to create a virtual host file for each of our domains.

The default Ubuntu configuration requires that each virtual host file end in .conf.

Create the Virtual Host File :

Open the new file in your editor with root privileges:

The file will look something like this (I’ve removed the comments here to make the file more approachable):

Remove all the lines and just paste below content into your file,

Step Five — Enable the New Virtual Host File

Now that we have created our virtual host files, we must enable them. Apache includes some tools that allow us to do this.

We can use the a2ensite tool to enable each of our sites like this:

After that just restart the service,

Step Six — Set Up Local Hosts File

Open /etc/hosts file by typing below command,

Enter our website,

Save and close the file.

Step Seven — Test your Results

Now that you have your virtual hosts configured, you can test your setup easily by going to the domains that you configured in your web browser:

You should see a page that looks like this:

There is no software limit on the number of domain names Apache can handle, so feel free to make as many as your server is capable of handling.

Happy Codding !!!

 

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here