In our previous article, we took look at how to install Apache HTTP Web Server on Ubuntu 20.04 LTS In this guide we will learn how to configure Apache Virtual Host to host several domains. When using the Apache web server, you can use virtual hosts (referred to as server blocks in Nginx) to hold the configuration files and run more than one domain from a single server. We will set up a domain called techdirectarchive_com but you will have to replace this with your own domain name.
You also be interested in the following guides: How to Install Jenkins Automation Server on Ubuntu 20.04 LTS, how to fix critical Veeam Backup and Replication 9.5, 10, and 11 vulnerabilities, how to deploy a React Application to AWS S3, how to set up and configure Route 53 for your Domain in AWS, how to use Command-Line on Git Bash and GitHub Desktop to PUSH local code to GitHub, how to fix Sudo: unknown uid 1000: who are you? Error on Ubuntu, and how to create Multiple Users and Set Password for each User in Linux.
On Ubuntu 20.04, Apache has only one server block enabled by default, which is set to serve contents from the
From the above, we will not change anything in this directory
/var/www/html, instead we will create a new directory structure within
techdirectarchive_com domain, leaving the
/var/www/html intact as the default directory to be served if a client request doesn’t match any other sites.
To have a hands-on experience and follow along in this session, you need to have Ubuntu 20.04 LTS installed on your system with a regular user or non-root user with
Sudo privileges configured on your server. You will also need a registered domain name from an authorized domain name registrar. While registering a domain name is not compulsory, especially if you’re just configuring Apache Virtual Host for a test or demo purpose, it is compulsory if you’re configuring it for a real-time or production environment. You can purchase a domain from Namecheap, Godaddy or any other domain and hosting company known to you. You should also ensure that basic firewall rules to block non-essential ports are properly configured on your server.
Once the above prerequisites are in place, sign in to your system to begin.
Step 1- Create New Directory
Let’s create the directory for
techdirectarchive_com as follows:
sudo mkdir /var/www/techdirectarchive_com
Next, you need to assign ownership of the directory with the
$USER environment variable:
sudo chown -R $User:$User /var/www/techdirectarchive_com
If you haven’t changed your
umask setting, which determines default file permissions, the permissions of your web roots should be correct. Run the command below to confirm that your permissions are right, allowing the owner to read, write, and execute files while only read and execute permissions are allowed to groups and others:
sudo chmod 755 -R /var/www/techdirectarchive_com
The next thing is to create a sample index.html page using nano or any of your favourite editors, you can run something similar to this:
sudo nano /var/www/techdirectarchive_com/index.html
Save and close the file when you are done.
For Apache to serve this content, it’s necessary to create a virtual host file with the correct directives. Instead of altering the default configuration file located at
/etc/apache2/sites-available/000-default.conf directly, we will make a new one at
/etc/apache2/sites-available/techda.com.conf with this command:
sudo nano /etc/apache2/sites-available/techdirectarchive_com.conf
As shown in the screenshot below, create a configuration block, which is similar to the default Apache configuration block but adjusted for our new directory and domain name. Remember to use your own domain name instead of the generic one used here. When you’re finished, save and close the file.
We’ve changed the DocumentRoot to our new directory and the ServerAdmin to an email address that the techdirectarchive_com site administrator can access. We’ve also added two directives: ServerName, which specifies the base domain that this virtual host definition should match, and ServerAlias, which specifies additional names that should match as if they were the base name.
When you’re finished, save and close the file
Next, let’s enable the file with the a2ensite tool, run this:
sudo a2ensite techdirectarchive_com.conf
You will be required to restart Apache in order to activate the new configuration after enabling the a2ensite utility for your domain. Ignore the prompt until you’ve finished everything, then restart the server immediately.
Disable the default site defined in 000-default.conf:
sudo a2dissite 000-default.conf
It’s time, to test for configuration errors by running:
sudo apache2ctl configtest
Testing for configuration errors should return “Syntax Ok” as an output:
Restart Apache to implement your changes:
sudo systemctl restart apache2
Apache should now be serving your domain name. You can test this by navigating to http://yourdomainname.com, where you should see something like this:
To add another domain name go ahead and repeat the steps by creating another directory in
/var/www and configuring all others steps.
Congrats, you can now host several domains on the Apache Web Server instead of the usual one single directory
/var/www/html provided for hosting a single domain by default.