Name-Based Virtual Hosting Using Apache: Host Multiple Websites on a Single Server (Ec2)

Ashique AntonyAshique Antony
2 min read

Author: Ashik Antony
Published: June 2025
Category: DevOps | Web Server | Apache

Name Based Virtual Hosting

In this type of hosting, several domains or websites can be hosted on a single server using just a single IP address. Simply put, all domains will share a single IP address. This is the easiest virtual hosting type to configure since all you need to do is map the domain names to the IP address and then configure Apache virtual hosts for each of the domain names.

Setup Name-Based Virtual Hosting

The first step in setting up name-based virtual hosting is to set up domain name resolution in the /etc/hosts file in the local mechine ( In windows it will be in system32/drivers/etc/hosts).

$ echo '192.168.2.105 www.mydomain1.com' >> /etc/hosts
$ echo '192.168.2.105 www.mydomain2.com' >> /etc/hosts

1.Lets install httpd in server

yum install httpd -y

sudo systemctl enable httpd

sudo systemctl start httpd

i used this Website: https://www.tecmint.com/apache-ip-based-and-name-based-virtual-hosting/ for reference

the website have all the detailled steps

2. Create Directories for Each Website

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

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

Give permissions:

sudo chown -R ec2-user:ec2-user /var/www/domain

Add sample index pages:

echo "Welcome to mydomain1.com" > /var/www/domain1.com/public_html/index.html

echo "Welcome to mydomain2.com" > /var/www/domain2.com/public_html/index.html

3. Create Virtual Host Config Files

For Domain1:

sudo vi /etc/httpd/conf.d/domain1.com.conf
<VirtualHost *:80>
    ServerName www.domain1.com
    DocumentRoot /var/www/domain1.com/public_html
    ErrorLog /var/log/httpd/domain1.com-error.log
    CustomLog /var/log/httpd/domain1.com-access.log combined
</VirtualHost>

For Domain2:

sudo vi /etc/httpd/conf.d/domain2.com.conf
<VirtualHost *:80>
    ServerName www.domain2.com
    DocumentRoot /var/www/domain2.com/public_html
    ErrorLog /var/log/httpd/domain2.com-error.log
    CustomLog /var/log/httpd/domain2.com-access.log combined
</VirtualHost>

5. Restart Apache

sudo systemctl restart httpd

OUTPUT

Search the browser with domain name like

*mydomain1.com

*mydomain2.com

0
Subscribe to my newsletter

Read articles from Ashique Antony directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ashique Antony
Ashique Antony

Passionate DevOps Engineer eager to drive innovation by automating workflows, optimizing cloud infrastructure, and enhancing CI/CD pipelines. Seeking an opportunity to collaborate with forward-thinking teams to streamline development and deployment processes for maximum efficiency and scalability.scalability.