[8 Minutes] Apache Virtual Host Ubuntu Setup Tutorial for Beginners

[8 Minutes] Apache Virtual Host Ubuntu Setup Tutorial for Beginners

Introduction

When you buy a normal hosting package, you will have limitation for everything. Since, you dont know the management, you have to go with their plans and pay them frequently.

But:

If you have little knowledge about handling Ubuntu Server, then you are lucky enough to save more than 900% of money that you are spending away. There are a lot of micro management needed to handle this.

However, they are not such a heavy tasks.

Today, Here in this article, i am going to show you how to use apache virtual host on Ubuntu to host multiple sites at the same time.

Let me tell you about the benefits of this one.

A basic single server from DO or Vultr can install more than 7000 visitors in real time (for static sites only). And that too for 5$.

Also, they give you 1TB bandwidth, it is enough to serve minimum of 5,00,000 Users.

You can take advantage of this and host multiple sites on a single server to handle the traffic efficiently.

You can do this easily with Apache web server. It is a simple web server. Incase if you dont know the basics of Apache, learn here about How to install Apache on Ubuntu server.

Apache is easy to configure and there is functionality called virtual hosts.

It will help you to host multiple sites on Ubuntu server.

Requirements

  • A basic ubuntu server configured according to the initial server setup guide.

Install Apache on Ubuntu 16.04

You can install the Apache directly from Ubuntu repositories using the apt-get utility. First, update the package index.

Then, install the apache web server using the below command.

Once you are done with the above step, then next step is to setup two virtual hosts files with example.com and example1.com.

Here, you have to use your own domain name to work on further. Point two of the domains to your server.

If you are using a CDN, make sure that your settings are correct and domains are properly pointing to the server IP address.

Make directories for Virtual Hosts

Here, in this step, we are going to create a directories under the document root of Apache web server.

The document root is /var/www/. We will create two files under this folder to hold the virtual host files.

Basically Apache is configured to serve web content from the Document Root. you can change this in apache configuration file.

Inside this directory, we will create two files and inside that, we will create public_html file.

Use the below command to create the directories.

You have to use your actual domain names here.

Once you have created the files, the next step would be granting the permission for it.

Change File Permission to Normal User

The file which you have created is owned by root user. Now, you have to make the regular user access it. For that execute the below command.

Considering that, you have logged into other account, execute this command.

The above command will give permission to the normal user.

Now, the public_html page is accessible by the regular user.

The one more step here is verify the user has permissions to read files of web root. For that, use the below command.

After that, web server will have access to the content to serve and then your user account will be able add content to the web root directory.

Make a Test Page for Virtual Host

Now, we have to create index.html file for both virtual host files. For that, use the below command.

The nano editor will open and add the code below to that file.

Then, save and close the file. Instead of creating a new file, let us just copy the first file to the second file by executing the below command.

Now, open the second file and make changes to match the output for second domain.

Make changes in the file as mentioned below. It is easy. We are just going to replace the domain name.

Creating Virtual Host Files

After creating the sample pages, now, it is time to create the configuration file for two virtual hosts.

By default, Apache has a virtual Host file called 000-default.conf.

Since, we have two virtual host files, we have to create two configuration files to notify apache how to work with both of those files.

First, let us copy the default configuration file to the first domain configuration file.

Now, you have to edit the copied file to make modification.

You can see the below information in the file.

Here, in the above file, we are going to create two additional directives and make changes to the one directive.

The two additional directories are

The server name refers to the domain. All the request from port 80 will be accepted for this domain. And the server alias refers to another form of domain representation.

Here, it means both example.com and www.example.com are same.

In the ServerAdmin directive, we have to add our email to send mail.

I will change that here as

We also need to change the document root to our newly created document root file for the example.com domain.

That should look like this. After you make the changes, the file will look like below.

Then, Save and close the file.

Now, you have to create the configuration file for the second domain also. For that, you just need to copy the example.com configuration file and then edit it.

Now, open the second configuration file using nano editor.

Make modification  to the code to look like below.

After that, save and close the file.

Enable the Virtual Host Configurations

Once you have create the new virtual Host files, now its time to enable them.

When you enable the newly created configuration file, you have to disable the default configuration file.

To make the changes to work, restart the apache web server.

Now, it is time to test what we have done.

Go to the browser and type example.com

You should see the below output.

Now, put the example2.com in browser.

Then, you can see the new output as mentioned here.

That’s all. Now, you have configured the apache virtual hosts.

Conclusion

Here, in this article you have learned how to setup apache virtual hosts to host multiple site under same web server.

Subscribe for our news letter to get notified for our next tutorial.

Comment here, if you have any doubts.

 

Leave a Reply

Your email address will not be published. Required fields are marked *