Set up Apache virtual hosts on Ubuntu server

When you own your own server, or you pay a hosting provider for a VPS or a dedicated server, you probably want to get the most out of your server. When you only run a small website from that server, you’re probably not pushing it to its limits. So then it might be useful to host some more websites or services on that server. In this tutorial I’m going to explain how to point multiple domain names to your single server. By doing this you can host multiple websites on one physical, or even virtual, server.

To be able to set up multiple virtual hosts on Ubuntu server, you first need to have Apache installed. You can follow this tutorial to install Apache on your Ubuntu server.

After having installed Apache, we’ll first create two folders which will be used to store the content for both websites. I’m going to use two free dot.tk domains to illustrate the process of setting up multiple websites on one server.

If you want to know how to create a free domain name and point it to your server, follow this tutorial.

The domain names I’m using are ‘test1daan.tk’ and ‘test2daan.tk’. I already pointed the domains to my server IP. Keep in mind that you are required to be able to open port 80 on your internet connection. Because some internet service providers (ISP’s) still block that port.

First I’m going to create the individual folders where the content of each website will reside with the following commands:

The “-p” argument is used to create the parent directories as well, at the same time. Now we’re going to set the permissions for the folder, so the contents can be accessed by other users.

The “-R” argument is used. “755” means that the files in this directory can be read, written to, and executed by the file’s owner. All other users can read and execute the files inside the directory.

Now the folders are created, we can create an index page for each virtual host. To do this, use the following command:

The Nano editor will open an empty document. Inside the document you can type the following text:

Then save the document by pressing ctrl + X, Y and enter. The first file is created. Now do the same for the second virtual host by entering the following command:

In this document you should type the following text:

Save the file by pressing ctrl + X, Y and enter.

After doing that, we can create our virtual host files for the two domain names. You can do this by copying the original virtual host file that was created while installing Apache. To copy this file, run the following commands:

The configuration files are created. Time to open the first file and edit its contents.

Originally, the file will look like this:

1

We will make some changes to this file. First of all we’re going to change the ServerAdmin email address. For me it’ll look like this, but you should fill in your own email address of course.

After updating the email address, it’s time to enable – and edit – the ServerName. Currently the ServerName is disabled, so it will respond to all domain names that point to this server for which no other virtual host is set. So uncomment the “#ServerName www.example.com”. Also set the server alias so that the server will respond as well when you put “www” in front of the domain name. It will become this:

The last thing we have to change is the public directory. Currently it’s set to “/var/www/html”, but it has to be set to “/var/www/test1daan.tk/public_html”.

The file will look like this:

1Now you can save the file by pressing ctrl + X, Y and enter.

Open the second VirtualHost file.

Add the following information to the second file:

The files are edited, so we can enable them.

And restart apache.

The virtual hosts are installed, and you can start hosting multiple websites or services on one Ubuntu server! If I visit “http://test1daan.tk”, I can see the configured index page:

Untitled

2 Comments

Leave a Reply

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

Protected with IP Blacklist CloudIP Blacklist Cloud