How to set up Apache Virtual Hosts on Ubuntu 18.10

Virtual Hosts enables hosting multiple numbers of websites on the same Apache HTTP server.

-Advertisement-

Apache HTTP server, formerly known as Apache Web Server is free and open-source software developed and maintained by Apache Software Foundation. It is a powerful and one of the most popular web servers worldwide.

In this tutorial, we will guide you on setting up Apache Virtual Hosts on Ubuntu 18.10. Generally, Virtual Host is used for hosting a number of websites on the same web server. If you are interested in setting up the Apache HTTP Server, head over to our step-by-step guide on setting up Apache HTTP Web server on Ubuntu.

Setting up Apache Virtual Hosts on Ubuntu

There are two types of virtual hosts that we can configure on Apache.

  • Single Apache server, One IP, and multiple websites => Name-based virtual hosts
  • Single Apache server and unique IP for each web site => IP based virtual hosting

IP based virtual host we can only configure one website on one IP address. If you need to host multiple websites you should have multiple IPs for that web server. The name-based virtual host is commonly used to host multiple websites on a single server.

-Advertisement-

In this tutorial, Here we are going to discuss how to create name-based virtual hosts.

Configuring Name-based Apache Virtual Hosts

The document root is a directory to store website files for a domain name for serving in response to requests.

1. Setting Directory Structure

In our guide, we will be using the following directory structure, but you can choose to change it to any location you want.

/var/www/
├── fosslinuxexample1.com
│ └── public_html
├── fosslinuxexample2.com
│ └── public_html

-Advertisement-

Since we are going to host two websites, we will need to create two directories under /var/www directory.

Launch the Terminal and let’s start to create a directory for fosslinuxexample1.com

sudo mkdir -p /var/www/html/fosslinuxexample1.com/public_html

Create a directory for fosslinuxexample2.com

-Advertisement-
sudo mkdir -p /var/www/html/fosslinuxexample2.com/public_html

Change directory ownership to the apache user (www-data) 

sudo chown -R www-data: /var/www/html/fosslinuxexample1.com/public_html
sudo chown -R www-data: /var/www/html/fosslinuxexample2.com/public_html

2. Setting Folder Permissions

sudo chmod -R 755 /var/www/html/fosslinuxexample1.com/public_html
sudo chmod -R 755 /var/www/html/fosslinuxexample2.com/public_html

3. Creating web pages

Now, let’s create web pages for each host. In my example, I will create a webpage for fosslinuxexample1.com

sudo vim /var/www/html/fosslinuxexample1.com/public_html/index.html

Add following contents to the index.html file. This is basically an HTML content.

<html>
 <head>
 <title>fosslinuxexample1.com Test Page</title>
 </head>
 <body>
 <h1>Hello, This is a test page for fosslinuxexample1.com website</h1>
 </body>
</html>

Time to save and close the file.

Similarly, we will create a web page for the fosslinuxexample2.com.

sudo vim /var/www/html/fosslinuxexample2.com/public_html/index.html

Add following contents to the file.

<html>
 <head>
 <title>fosslinuxexample2.com Test Page</title>
 </head>
 <body>
 <h1>Hello, This is a test page for fosslinuxexample2.com website</h1>
 </body>
</html>

Save and close the file.

4. Creating Virtual Hosts

Generally, Apache Virtual Hosts configuration files are stored in /etc/apache2/sites-available directory and /etc/apache2/sites-enabled directory. Start with creating a virtual host for fosslinuxexample1.com.

sudo vim /etc/apache2/sites-available/fosslinuxexample1.com.conf

Add below contents to the file.

<VirtualHost *:80>
    ServerName fosslinuxexample1.com
    ServerAlias www.fosslinuxexample1.com
    ServerAdmin webmaster@fosslinuxexample1.com
    DocumentRoot /var/www/html/fosslinuxexample1.com/public_html

<Directory /var/www/html/fosslinuxexample1.com/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/fosslinuxexample1.com-error.log
    CustomLog ${APACHE_LOG_DIR}/fosslinuxexample1.com-access.log combined
</VirtualHost>


Save and exit the file. In the similar lines, create a virtual host for fosslinuxexample2.com

sudo vim /etc/apache2/sites-available/fosslinuxexample2.com.conf

Add below contents to the second website.

<VirtualHost *:80>
    ServerName fosslinuxexample2.com
    ServerAlias www.fosslinuxexample2.com
    ServerAdmin webmaster@fosslinuxexample2.com
    DocumentRoot /var/www/html/fosslinuxexample2.com/public_html
    <Directory /var/www/html/fosslinuxexample2.com/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/fosslinuxexample2.com-error.log
    CustomLog ${APACHE_LOG_DIR}/fosslinuxexample2.com-access.log combined
</VirtualHost>

Save and exit the file.

5. Enabling Virtual Host Configuration Files

There are different ways to enable virtual hosts. The simplest way is to enable the virtual host is by using the a2ensite command. Alternatively, you can enable it by creating symbolic links to the /etc/apache2/sites-enabled directory.

Now we should disable 000-default.conf and enable newly created virtual host files

Use this command line to disable the default virtual host.

sudo a2dissite 000-default.conf

The following commands will enable new virtual hosts.

sudo a2ensite fosslinuxexample1.com.conf
sudo a2ensite fosslinuxexample2.com.conf

Enable virtual host creating symbolic links.

If you enabled virtual host using a2ensite command no need to run below commands:

sudo  ln -s /etc/apache2/sites-available/fosslinuxexample1.com.conf /etc/apache2/sites-enabled/
sudo  ln -s /etc/apache2/sites-available/fosslinuxexample2.com.conf /etc/apache2/sites-enabled/

Check configuration files syntax.

sudo apachectl configtest

Restart Apache to activate the new configuration.

sudo systemctl restart apache2

6. Testing Virtual Hosts

Before testing the web pages, we need to modify /etc/hosts file to configure the server IP.

sudo vim /etc/hosts

Add the following line to the hosts file. Remember to replace 10.94.12.217 in the below command lines with your server IP. When you are done, save and close the file.

10.94.12.217 fosslinuxexample1.com
10.94.12.217 fosslinuxexample2.com

Finally, launch your favorite Web Browser and start browsing the websites!

fosslinuxexample1.com

Browse Website from Apache Virtual Host
Browse Website from Apache Virtual Host

fosslinuxexample2.com

Browse Website from Apache Virtual Host
Browse Website from Apache Virtual Host

If you can see the web pages you created, congrats you have successfully created the virtual hosts. Got any questions? How did it work for you?

-Advertisement-
Darshana
Hey! I'm Darshana, a Linux / DevOps Engineer and also a contributor to FOSS Linux. I enjoy working on various kind of Linux distributions and cloud technologies. During my free time, I love to swim and hike across nature trails. Linux is my love and I'm here to share all my learnings with all of you! Hope you enjoyed reading my article.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,631FansLike
360FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

Top 20 Git Commands with Practical Examples

If you are here reading this post, there is a high probability that you have heard or interacted with Github, and you now want to learn Git. Before we continue with showing you some of the cool Git commands, let's understand the difference between Git and GitHub.

Top 10 New Features in Linux Kernel 5.7

Linus Torvalds has announced the release of Linux Kernel 5.7 after seven weeks of development. The release announcement comes as a piece of exciting news as it brings a host of new features for the hardware manufacturers as well as the developers.

How to install CMake on Ubuntu

CMake is a cross-platform free and open-source software tool designed to build, test, and package the software. CMake uses a simple platform and compiler-independent configuration files to control the software compilation process.

How to install Lightworks on Ubuntu

Even though Linux may not get a native installer of video editing software like Adobe Premiere or Final Cut Pro, that doesn't mean there are no industry standards tools available. Lightworks is non-linear editing (NLE) video mastering app for Windows, Linux, and macOS. Installing it on Ubuntu is simple due to deb package availability.

How to install DaVinci Resolve on Fedora

Davinci Resolve is a professional application used for color correction, video editing, visual effects, and motion graphics. It is one of the extensively used software by movie industries located in Hollywood.

The 10 Best Programming Languages for Hacking

One of the significant entities we have in Cyber Security is Ethical Hacking (ETH). It is the process of detecting and finding flaws or vulnerabilities in a system that a hacker would exploit.

MUST READ

Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.
Elementary OS 5.1 Hera has received a point release with a handful of new features and bug fixes, and we will be reviewing the significant changes in this article. For those new to elementary OS, this Ubuntu-based Linux distribution uses their inhouse built Pantheon desktop environment and AppCenter.

What is FOSS, and how does it differ from Freeware

The rise of the Linux operating system, in all its various distributions, over the past few decades has catapulted the popularity of Free or Open Source Software (FOSS). Let's guide you in understanding what is FOSS, how it differs from freeware and is Linux a FOSS.

How to clone hard disk on Linux using Clonezilla

Disk cloning refers to the process of copying data from one disk to another, thus creating a one-to-one copy of the drive. Technically, this process is possible using the copy-and-paste method.

5 ways to send emails using the command-line in Linux

Did you master using the command-line in Linux? There is no limit to what one can do via the Linux Terminal. One of such things we are going to discuss today are methods of sending an email using the command-line.

Scrcpy – Control Android devices from a Linux desktop

Scrcpy is a desktop program that can be used to access your Android phone's system and interface through your computer. The app is quite convenient, and some of its best features are highlighted below.