What are the Sub-domains?Why are Domains and Sub-Domains required?
Domains are well website name or the identity or a website, that one buys from a domain registrar website like google or GoDaddy and that is the address of your personal or professional website.
And subdomains are the subset or part of a personal or professional website, where users can add and extend the main domain's functions and features.
For example, a user wants to start his Food tech business and also wants to write blogs about food, and also wants to tracks the business transactions of his business.
Each feature has different functionality on the application side, the calculation, tracking, and analysis on the dashboard, the text editor and content management for the blog, etc.
Here, comes the subdomains part, why we need it.
The user will create his home page on a different directory, blog features have different code and stores on the different directory and same for dashboard also, and now the apache webserver will be able to manage all these directories and able to handle the requests on domains and sub-domains using configuration on the virtual host.
Configuring VirtualHost to Enable Domains and Sub-Domains
Change directory to the /etc/apache2/
and select the configuration file which you want to edit for adding subdomains.
There must be two files one is my-domain.conf
and the other is default configuration 000-default.conf
$ sudo vi /etc/apache2/sites-available/my-domain.conf
OR
$ sudo vi /etc/apache2/sites-available/000-default.conf
Add Wildcard Subdomain
Home Page - mysite.com (Main Domain)
<VirtualHost *:80>
DocumentRoot /var/www/home
ServerName mysite.com
</VirtualHost>
Blog Page - blog.mysite.com (Sub-Domain)
<VirtualHost *:80>
DocumentRoot /var/www/blog
ServerName blog.mysite.com
</VirtualHost>
Dashboard - dashboard.mysite.com (Sub-domain)
<VirtualHost *:80>
DocumentRoot /var/www/dash
ServerName dashboard.mysite.com
</VirtualHost>
Wildcard Domain
<VirtualHost *:80>
ServerAlias *.mysite.com
DocumentRoot /var/www/html/wildcard/
</VirtualHost>
Final Configuration
## HomePage
<VirtualHost *:80>
DocumentRoot /var/www/home
ServerName mysite.com
</VirtualHost>
## Blog Page
<VirtualHost *:80>
DocumentRoot /var/www/blog
ServerName blog.mysite.com
</VirtualHost>
## Dashboard Page
<VirtualHost *:80>
DocumentRoot /var/www/dash
ServerName dashboard.mysite.com
</VirtualHost>
## Wild Card
<VirtualHost *:80>
ServerAlias *.mysite.com
DocumentRoot /var/www/html/wildcard/
</VirtualHost>
Testing and Dry Run the Configuration
Check the configuration by running the command:
sudo apachectl configtest
Reload and Restart the server
# Reload
$ sudo service apache2 reload
# Restart
$ sudo service apache2 restart
Top comments (0)