How to Create Apache Virtual Host for Your Website

Create Apache Virtual Host

Usually, when you set up your web server you want to have the capability to host more than one website or web application should the need arise. Similar to server blocks in Nginx, here we will show you how to create a virtual apache host for your website.

In this tutorial, we will be using Ubuntu 20.04. Other Linux distros or even older Ubuntu versions have similar instructions. The major difference is the default folder locations in other Linux distros.


This guide assumes you have already set up a server installation of Ubuntu 20.04 with a firewall configured, SSL certificate installed and configured, and Apache web server installed. You can learn how to install a LAMP stack on Ubuntu by following this tutorial. LAMP stack will get you Apache, MySQL, and PHP on your system.

You will also need to have a user added to your system with sudo administrative privileges.

Steps to Create Apache Virtual Host

By default Apache on Ubuntu 20.04 is configured to serve documents from the /var/www/html directory. This is well and good but suitable for only single websites. This needs to change to accommodate multiple websites.

The convention we will use here is to set each virtual host site as a child of /var/www. That way your site will be hosted within domain_name giving us a structure such as /var/www/domain_name.

Create the directory for domain_name using the following command:

Read Also  Meet the Date, the Definitive Fruit of the Sultans

$ sudo mkdir /var/www/domain_name

Next, assign ownership of the directory. In this case, we will set the ownership with the $USER environment variable, which will reference your current user:

$ sudo chown -R $USER:$USER /var/www/domain_name

In some cases the user would set to the Apache server user www-data, however, this is not always the best idea with multiple sites setups. if one site is compromised using this account then the unauthorized user has privileged access to all the sites.

Next, you will need to open a new configuration file in Apache’s sites-available directory using your preferred editor. In this case we will be using nano:

$ sudo nano /etc/apache2/sites-available/domain_name.conf

Enter following base content into your configuration file:

<VirtualHost *:80>
    ServerName domain_name
    ServerAlias www.domain_name 
    ServerAdmin [email protected]_name
    DocumentRoot /var/www/domain_name/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Save and close the file when you’re done by pressing CTRL+S the CTRL+X, or simply CTRL+X then Y and ENTER.

Definitions in the configuration file:

  • VirtualHost *:80 block: This is the block that holds the configuration for the virtual host. The ’80’ is the port number. In this case, it signifies insecure (HTTP) requests to the server. HTTPS port would normally be ‘443‘ which will hold more configurations for the SSL certificate.
  • ServerName: This is the valid domain name that is entered into the web browser in order to reach this site.
  • ServerAlias: This is an alias that you can use to reach this site. In the example above the alias includes adds ‘www‘ to the domain name.
  • ServerAdmin: The email address of the server administrator
  • DocumentRoot: this is path to the root folder of your site. in this case, we have set it one level in to allow other apps and configurations to be kept away from the relative path that way they can’t be accessed from the web browser
  • ErrorLog: The path to the error log
  • CustomLog: The path to the access log
Read Also  CMake 3.14.2 Release has Been Announced for General Availability

In the above, ErrorLog and CustomLog use the default server log locations. If you have multiple websites it is a good idea to have separate files for access and error logs. This will help when it comes to troubleshooting and checking stats on a site to site bases.

Change the following lines:

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined


    ErrorLog /var/www/domain_name/error.log
    CustomLog /var/www/domain_name/access.log combined

You can now use a2ensite to enable the new virtual host:

$ sudo a2ensite domain_name

You can optionally disable the default site that is set up by Apache. This default site will be shown if you access your server using an IP address.

$ sudo a2dissite 000-default

You can run the following command anytime to make sure your configuration file doesn’t contain syntax errors:

$ sudo apache2ctl configtest

Finally, reload Apache so that these changes take effect:

$ sudo systemctl reload apache2

Your new website is now active. Because the webroot /var/www/domain_name/public_html is still empty we need to create an index.html file in that location to test that the virtual host works like so:

$ nano /var/www/domain_name/public_html/index.html

Now add the following content in this file:

    <title>domain_name website</title>
    <h1>Hello World!</h1> 
    <p>This is the home page of domain_name.</p>

Save and close the file.

Open your web browser and enter your server’s domain name and you should see the above page served to your web browser. That is how to create an Apache virtual host with basic details.

Add SSL to your Apache Virtual Host

Even though your website is live you will not be able to serve secure requests. To do this you will need to create another virtual host block to serve secure pages. Open the site’s configuration file:

Read Also  Brushing up on Your Sesame Seeds Moments

$ sudo nano /etc/apache2/sites-available/domain_name.conf

Enter following content at the end of your configuration file:

<IfModule ssl_module>
  <VirtualHost *:443>
    ServerName domain_name
    ServerAlias www.domain_name 
    ServerAdmin [email protected]_name
    DocumentRoot /var/www/domain_name/public_html
    ErrorLog /var/www/domain_name/error.log
    CustomLog /var/www/domain_name/access.log combined

    SSLEngine on
    SSLCertificateFile /path/to/domain_name/certificate_file
    SSLCertificateKeyFile /path/to/domain_name/certificate_key_file 
    SSLCertificateChainFile /path/to/domain_name/certificate_chain_file


Save and close the file.

The Secure block adds four basic components as follows:

  • IfModule ssl_module block: This ensures the virtual host that handles secure requests is only triggered only if the Apache SSL module is enabled.
  • SSLEngine on: This enables the processing of SSL requests:
  • SSLCertificateFile: The path to the SSL Certificate file
  • SSLCertificateKeyFile: The path to the SSL Certificate Key file
  • SSLCertificateChainFile: The path to the SSL Certificate Chain file

The values for SSLCertificateFile, SSLCertificateKeyFile, SSLCertificateChainFile depend on how you acquired your SSL certificate and where it is placed on your system. You can follow this guide to learn how to install an SSL certificate on your Linux system and how to get the path.

You can now reload Apache so these changes take effect:

$ sudo systemctl reload apache2

Go to your web browser and access your domain_name again but this time use HTTPS instead of HTTP. You should see the secure padlock on your web browser confirming that your server is now configured to server secure requests.


There are more tasks you would need to complete to create an Apache virtual host for your website. You may want to harden your server security and even protect it against DDOS attacks. You may also want to force https and install an SSL/TLS certificate, create aliases to non-public facing applications, and so on. All these are beyond the scope of this tutorial but all-in-all you still have a working site running off an Apache virtual host.