What is Apache Virtual Host?
A virtual host allows you to run more than one website or rather multiple websites from a single machine/physical server or virtual private server.There are 2 types of virtual hosting on Apache
- IP-based Virtual Hosting
It uses the IP address of the connection to determine the correct virtual host to serve. Therefore you need to have a separate IP address for each host.
- Name-based Virtual Hosting
Requirements
- A computer running Ubuntu Operating System
- A user account with sudo or root privileges
- Firewall enabled
- A domain pointing to a public server IP
Apache server by default has one server enabled and configured to serve documents from the main directory located at /var/www/html. But this set up is suitable if you are working with a single site. In the event that you are hosting multiple sites, the server can become too large or disorganized to function efficiently.
So instead of modifying the default directory at /var/www/html we can create a directory structure within /var/www for our site(s) while leaving /var/www/html in place as the default directory to be served if a client request doesn't match any other sites.
How To setup a name-based virtual host
Step 1
- Open your terminal (Ctrl Alt T) and create a directory for your domain as follows
sudo mkdir -p /var/www/website1.com/public
sudo mkdir -p /var/www/website2.com/public
The p command stands for parent directory and allows us to create sub-directories of a directory. But it will create the parent directory first if it doesn't exist. If the directory you wish to create already exists, it will present you with an error letting you know that the directory exists.Within the directories, we also created public directories which are going to store website files for the domains.
- Next, create a sample index.html page for each domain using your favourite text editor.
Visual Studio Code use code
Nano editor use nano)
We'll start with the first domain. Type this in your terminal
sudo subl /var/www/website1.com/public/index.html
For Sublime Text use sublVisual Studio Code use code
Nano editor use nano
Add the following HTML(copy & paste)
<html>
<head>
<title>website1.com</title>
</head>
<body>
<h1>Yaaaaaay!!!Website1 setup successfully</h1>
</body>
</html>
Save the file and exit- Create a sample page for the second domain
sudo subl /var/www/website2.com/public/index.html
Add the following HTML (copy and paste) <html>
<head>
<title>website2.com</title>
</head>
<body>
<h1>Yaaaaaay!!!Website2 setup successfully</h1>
</body>
</html>
Save and close the file when you are finished. Step 2
Create a virtual host configuration file
In order for Apache to serve this content, it is necessary to create a virtual host file with the correct directives. So instead of modifying 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/website1.com.conf:
Write the following command in your terminal
sudo subl /etc/apache2/sites-available/website1.com.conf
Now add the following configuration block (copy & paste) to create a basic configuration file which is similar to the default but updated for our new directory and domain name. <VirtualHost *:80>
ServerAdmin webmaster@website1.com
ServerName website1.com
ServerAlias www.website1.com
DocumentRoot /var/www/website1.com/public
ErrorLog ${APACHE_LOG_DIR}/website1.com-error.log
CustomLog ${APACHE_LOG_DIR}/website1.com-access.log combined
</VirtualHost>
Save and close the file when done.
Notice we've updated the DocumentRoot to our new directory and ServerAdmin to an email that website1.com site administrator can access.
We've also added 2 directives:
ServerName which establishes the base domain that should match for this virtual host definition and ServerAlias which defines further names that should match as if they were the base name.
There is no established format but naming your configuration files based on your domain name is the recommended practice.
Repeat the same process for the rest
For domain 2 run the following in the terminal:
sudo subl /etc/apache2/sites-available/website2.com.conf
Now add the following configuration block (copy & paste) to create a basic configuration file which is similar to the default but updated for our new directory and domain name.
Use the correct domain name for your website
<VirtualHost *:80>
ServerAdmin webmaster@website2.com
ServerName website2.com
ServerAlias www.website2.com
DocumentRoot /var/www/website2.com/public
ErrorLog ${APACHE_LOG_DIR}/website2.com-error.log
CustomLog ${APACHE_LOG_DIR}/website2.com-access.log combined
</VirtualHost>
Enable Virtual Host Configuration File
Enable the virtual host file for the first domain using this command sudo a2ensite website1.com
For the second site use sudo a2ensite website2.com
Disable the default site defined in 000-default.conf:
using sudo a2dissite 000-default.conf
Next, we'll test for configuration errors using sudo apache2ctl configtest
You should see the following output SyntaxOK
Restart Apache to implement your changes sudo systemctl restart apache2
Apache should now be serving your domain name. So navigate to http://website1.com and http://website2.com where you should see the message we put in the index.html files of each website.
After setting up your virtual host successfully, you can host your domain somewhere if you have a public IP address and anyone can find your site using your domain name via a web browser.
If you encounter any problems while following this guide, please leave a comment down below or send me an email at nuffdailies@gmail.com.
Let me know if it worked for you too. Enjoy!!
This is awesome
ReplyDeleteThank you. Please try it if you haven't already and let me know if it works
Deletethis very informative
ReplyDeletegotta try this
Thanks. Would love to know if it worked or you had any problems
Delete