How to set up Apache virtual hosts on Ubuntu 20.04

s2 0

A virtual host is an Apache configuration statement that allows you to run more than one website on a single server. With Virtual Hosts you can specify the Site Document Root (the directory containing the website files), create a separate security policy for each site, use different SSL certificates, and much more.

This article describes how to set up Apache Virtual Hosts on Ubuntu 20.04.

requirements

Make sure you meet the following requirements before proceeding with the guide:

  • One or more domain names that point to your public server IP.
  • Apache installed on your Ubuntu system.

  • You are logged in as root or as a user with sudo rights.

Create the directory structure

The document root is the directory in which the website files for a domain name are stored and made available in response to requests. You can put the document root directory anywhere, in this example we’ll use the following directory structure:

/var/www/
├── domain1.com
│   └── public_html
├── domain2.com
│   └── public_html

For each domain hosted on the server, the document root directory is set to /var/www/<domain_name>/public_html.

First, create the root directory for the domain:

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

We also create one index.html File in the root of the domain document that appears when you visit the domain in your browser:

/var/www/domain1.com/public_html/index.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Welcome to domain1.com</title>
  </head>
  <body>
    <h1>Success! domain1.com home page!</h1>
  </body>
</html>

Since the above commands are run as the sudo user, the newly created files and directories are owned by root. To avoid permission problems, change ownership of the domain document root directory and all files within the directory to the Apache user (www-data):

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

Create a virtual host

On Ubuntu systems, the Apache Virtual Hosts configuration files are located in /etc/apache2/sites-available Directory. They can be activated by adding symbolic links to the /etc/apache2/sites-enabled Directory that Apache read when it started.

Open the text editor of your choice and create the following basic virtual host configuration file:

/etc/apache2/sites-available/domain1.com.conf
<VirtualHost *:80>
    ServerName domain1.com
    ServerAlias www.domain1.com
    ServerAdmin [email protected]
    DocumentRoot /var/www/domain1.com/public_html

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

    ErrorLog ${APACHE_LOG_DIR}/domain1.com-error.log
    CustomLog ${APACHE_LOG_DIR}/domain1.com-access.log combined
</VirtualHost>
  • ServerName: The domain that should correspond to this virtual host configuration. This should be your domain name.
  • ServerAlias: Any other domains or subdomains that should match this virtual host, such as www Subdomain.
  • DocumentRoot: The directory from which Apache provides the domain files.
  • Options: This directive
    controls which server functions are available in a particular directory.

    • -Indexes: Prevents directory listings.
    • FollowSymLinks: When this option is enabled, Apache follows the symbolic links.
  • AllowOverride: Specifies which directives in the. are declared .htaccess The file can overwrite the configuration instructions.
  • ErrorLog, CustomLog: Specifies the location for log files.

You can name the configuration file anything you want, but it is best to use the domain name as the name of the virtual host configuration file.

To activate the new virtual hosts file, use the a2ensite Auxiliary script that contains a symbolic link from the virtual host file to the sites-enabled Directory:

sudo a2ensite domain1.com

The other option is to manually create a symlink as shown below:

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

Then test the configuration for syntax errors with:

sudo apachectl configtest

If there are no errors, you will see the following output:

Syntax OK

Restart the Apache service for the changes to take effect:

sudo systemctl restart apache2

Finally, to check that everything is working as expected, open https://domain1.com in your browser and you will see the contents of the index.html Page:

diploma

You learned how to create an Apache virtual host configuration to host multiple domains on a single Ubuntu server.

Repeat the steps above to create additional virtual hosts for all of your domains.

If you have any problems, please feel free to leave a comment.

This post is part of the How to install the LAMP stack on Ubuntu 20-04 Series.
Further articles in this series:


How to install MySQL on Ubuntu 20.04

How to install Apache on Ubuntu 20.04

How to install PHP on Ubuntu 20.04

How to set up Apache virtual hosts on Ubuntu 20.04

Secure Apache with Let’s Encrypt on Ubuntu 20.04

This post is part of the How to install the LAMP stack on Ubuntu 20-04 Series.
Further articles in this series:


How to install MySQL on Ubuntu 20.04

How to install Apache on Ubuntu 20.04

How to install PHP on Ubuntu 20.04

How to set up Apache virtual hosts on Ubuntu 20.04

Secure Apache with Let’s Encrypt on Ubuntu 20.04

How to install MySQL on Ubuntu 20.04

How to install Apache on Ubuntu 20.04

How to install PHP on Ubuntu 20.04

How to set up Apache virtual hosts on Ubuntu 20.04

Secure Apache with Let’s Encrypt on Ubuntu 20.04
Tags: