• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Set Up Apache Virtual Hosts on Ubuntu 20.04

Written by Admin, Updated On May 9, 2020
a2ensite, apache, ubuntu
How to Setup Apache Virtual Hosts on Ubuntu 20.04

Apache Virtual Hosts allows you to host multiple websites on a single server. Using Virtual Hosts, you can customize each website by create separate document root, security policy and different SSL certificates. The basic unit that describes an individual site or domain is called a virtual host. This tutorial explains how to set up Apache Virtual Hosts on Ubuntu 20.04 Focal Fossa.

Prerequisites#

  • A Ubuntu 20.04 server with a non-root user with sudo privileges.
  • Apache should installed and configured, as shown in How to Install Apache on Ubuntu 20.04.
  • A domain name should pointing to your server IP address.

Create Apache Virtual Host on Ubuntu#

Apache virtual hosts are similar to server blocks of Nginx web server. Apache on Ubuntu has one server block enabled by default that is configured to serve documents from the /var/www/html directory. When you have multiple sites on a single server you should create separate Apache Virtual Hosts for each on Ubuntu 20.04. Perform the below steps to continue:

Creating the Directory Structure#

First, we will make document root directory where the website data will be stored and serving to visitors for a domain. You can set the document root to any location that you want. It’s best practice to set in directory structure. Commonly, stored at /var/www.

/var/www/
├── example1.com
│   └── public_html
├── example2.com
│   └── public_html

Let’s create document root directory for first domain. Execute following command to create directory :

sudo mkdir -p /var/www/example1.com/public_html

Create a index.html file under the domain document root directory for testing purpose. By default, this page will display to visitor.

Run the below command to create a new index.html file using your favorite text editor :

sudo nano /var/www/example1.com/public_html/index.html

Add the following lines into it:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Welcome!!</title>
  </head>
  <body>
    <h1>Success! example1.com set up completed!</h1>
  </body>
</html>

Since, we created the directory and files with sudo user so they are owned by our root user. We will change document root directories ownership to avoid permission issue for our regular user.

sudo chown -R www-data: /var/www/example1.com

Now, regular user can modify files in our web directories without any issues.

Create Virtual Host Files#

By default on Ubuntu machine, Apache Virtual Host configuration files are located at /etc/apache2/sites-available directory. We will create Virtual Host configuration files at this location.

Create a new file using your choice text editor by typing :

sudo nano /etc/apache2/sites-available/example1.com.conf
<VirtualHost *:80>
    ServerName example1.com
    ServerAlias www.example1.com
    DocumentRoot /var/www/example1.com/public_html

    <Directory /var/www/example1.com/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example1.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example1.com-access.log combined
</VirtualHost>
  • ServerName: This should be your domain name and match with virtual host configuration.
  • ServerAlias: All other domains or subdomains that should match for this virtual host as well, usually the www subdomain.
  • DocumentRoot: Path of virtual host directory that from which Apache will serve the domain files.
  • Options: This directive controls which server features are available in a specific directory.
  • -Indexes: It will prevent directory listings.
  • FollowSymLinks: Apache will follow the symbolic links if this option is enabled.
  • AllowOverride: Specifies which directives declared in the .htaccess file can override the configuration directives.
  • ErrorLog, CustomLog: Specifies the location for log files.

You can give any names to your configuration file. It’s recommended to give file name same as domain name.

Now, We will enable virtual host file by creating symbolic links to the /etc/apache2/sites-enabled directory.

In Ubuntu systems you can create symbolic links either by a2ensite tool or can create manually. To create using a2ensite execute following command :

sudo a2ensite example1.com

To create symbolic link manually type :

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

Now check the syntax by type :

sudo apachectl configtest

It will show below output if there are no errors:

Syntax OK

You must restart apache2 service to get changes effect by below command :

sudo systemctl restart apache2

You can verify by opening your http://example1.com to your web browser and it will show you as following :

Apache Virtual Hosts

We have shown you how to set up Apache Virtual Hosts on Ubuntu 20.04 system. In this tutorial we created a virtual host for a single domain, you can repeat steps for multiple domains on a single machine.

If you have any question or feedback, feel free to leave a comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Install LEMP Stack on Ubuntu 20.04
Next Article   How to Set Up SSH Keys on CentOS 8

Related Posts

  • How to Install and Use PHP Composer on Ubuntu 22.04

    How to Install Composer on Ubuntu 22.04

    January 31, 2023
  • How to Install Nginx on Ubuntu 22.04

    How to Install Nginx on Ubuntu 22.04

    January 28, 2023
  • How to Install Puppet Agent on Ubuntu 22.04

    How to Install Puppet Agent on Ubuntu 22.04

    January 22, 2023

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install SSH Keys on Ubuntu 22.04
    How to Set up SSH Keys on Ubuntu 22.04 January 7, 2023
  • How to Install Mongodb on Debian 11
    How to Install MongoDB on Debian 11 Linux January 11, 2023
  • How to Install Puppet Agent on Ubuntu 22.04
    How to Install Puppet Agent on Ubuntu 22.04 January 22, 2023
  • How to Install Python 3.11 on Debian 11
    How to Install Python on Debian 11 January 25, 2023
  • How to Change-Hostname Ubuntu 22.04
    How to Change Hostname on Ubuntu 22.04 January 19, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook