Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Set Up Apache Virtual Hosts on Ubuntu 20.04

This guide shows you how to host more than one website on one Ubuntu 20.04 server using Apache virtual hosts. It needs root or sudo access.

VPS and Cloud Servers2 min read13 steps

Tip: Ubuntu 20.04 is an older release. Newer Ubuntu versions use the same steps.

What is a virtual host?

Apache is a web server program. A virtual host is a set of rules for one website. It lets one server serve many sites, like one apartment block with many flats. Each flat has its own name on the door.

Steps

  1. Log in to your server over SSH.
  2. Install Apache:
    sudo apt update
    sudo apt install apache2 -y
  3. Create a folder for your site's files:
    sudo mkdir -p /var/www/example.com/public_html
  4. Give your user ownership of it:
    sudo chown -R $USER:$USER /var/www/example.com/public_html
    sudo chmod -R 755 /var/www/example.com
  5. Create a test page:
    nano /var/www/example.com/public_html/index.html
    Type a short line such as <h1>Welcome to example.com</h1>. Press Ctrl+O, then Enter to save, and Ctrl+X to leave.
  6. Create the virtual host file:
    sudo nano /etc/apache2/sites-available/example.com.conf
  7. Put this inside it:
    <VirtualHost *:80>
        ServerAdmin webmaster@example.com
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /var/www/example.com/public_html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    Change example.com to your own domain.
  8. Turn on your new site:
    sudo a2ensite example.com.conf
  9. Turn off the default Apache site, if you do not need it:
    sudo a2dissite 000-default.conf
  10. Check for mistakes:
    sudo apache2ctl configtest
    You should see "Syntax OK".
  11. Restart Apache:
    sudo systemctl restart apache2
  12. If the firewall UFW is on, allow web traffic:
    sudo ufw allow 'Apache Full'
  13. Point your domain's A record to the server IP. An A record links a domain name to a number address. Then open your domain in a browser.

For a second website, repeat steps 3 to 11 with another domain name and folder.

Tip: Add free HTTPS later with Certbot. It adds a second virtual host for port 443 for you.

Warning: the command a2dissite removes a site from the active list. Check which site you are turning off, so you do not take a live site offline.

Quick recap

  • A virtual host lets one server show many sites.
  • Make a folder and a .conf file for each site.
  • Use a2ensite to switch it on.
  • Run apache2ctl configtest, then restart Apache.
  • Point the domain to your server.