Disclaimer: This is a user generated content submitted by a member of the WriteUpCafe Community. The views and writings here reflect that of the author and not of WriteUpCafe. If you have any complaints regarding this post kindly report it to us.

The Apache HTTP server has gained popularity for its flexibility, power, and wide support among developers. An Apache server configuration does not happen in one monolithic file but rather through a modular design that enables adding and modifying new files as necessary. An individual domain or site can be created within this modular design.

Apache Virtual Hosts allows you to serve multiple websites with a single instance. When the domain is integrated and all set to leverage the potential of an Apache web server, the visitor will be redirected to a website that contains the information for the particular domain or site. As a result, visitors might not experience that the same server is set to serve multiple websites. This results in driving top notch user experience and is ready to scale without any software limitations.

Here we are going to cover everything about Apache virtual hosts configuration and key steps to host multiple websites using the Apache Virtual Hosts.

What is a Web Server?

A web server stores and provides website content such as text, images, videos, and application data. These contents can be used by clients by way of web servers. Users typically use a web browser to download files from your website when clicking links.

How Does a Web Server Work?

Web servers use the client-server model. Servers respond to client requests for resources or services. Requests from web clients are processed in a few steps by web servers:

  • Users load content into websites by making HTTP requests through their web browsers.
  • Web browsers search for IP addresses of requested websites. The URL of the web page is translated through the use of the Domain Name System (DNS) or the cache. As a result, you can locate the server that hosts the website's files.
  • The web server processes the HTTP request through its HTTP server after receiving the HTTP request.
  • Upon accepting the request, its HTTP server will search the server files for relevant information.
  • Web servers return site files to browsers that request them. The user then sees the content of the website.
  • If the HTTP server has difficulty finding or processing the requested files, the browser will display an error message. Once you understand the basics of web servers, hosting multiple web servers on the same server will be a breeze.

Must read- Importance of Apache Hadoop in the Retail Industry

Types of Apache Virtual Hosting

Apache Virtual Hosting lets users host more than one website on one physical server (or a cloud-based server). This virtual host offers users the advantage of using any virtual host within a single server. Every domain's requests are mapped into a document root, where the sites' files are stored. Virtual hosting functions are generally available in Shared web hosting and Reseller web hosting.

Hosting based on IP addresses or name-based hosting is the most common type of virtual hosting. There are multiple virtual hosts, each with its IP address and name.

  • Name-Based Virtual Hosting

The name-based virtual hosting technology uses multiple domain names on a single server to host multiple websites. With name-based virtual hosting, every domain will have the same IP address. By mapping all domains to a single IP address, you can manage all domains. Users generally prefer name-based virtual hosts to IP-based hosts since they find them more convenient.

  • IP Based Virtual Hosting

IP-based virtual hosting allows multiple websites to be hosted on a single server using multiple IP addresses. Using multiple NICs (Network Interface Cards) and assigning different IP addresses to each NIC will be necessary. As a result, you can specify different directives based on IP address.

Also read- PHP for Software Development

Steps to Host Multiple Websites on One Server using Apache

An Apache virtual host configuration file allows you to specify which domain must be used if you want to host more than one site on the same server. An example of a virtual host used to resolve site requests is a name-based host. In response to a request for your site, the server will determine which site's files to serve according to the domain name. The technique allows you to host multiple websites on the same IP address on one server.

Create the Directory Structure

Site data is stored in the directory structure. The visitors then receive it. Usually, the top-level directory is called the Document root. For visitors, Apache searches for content in the document root.

/var/www is set as the document root. Hosting websites requires you to create a directory for each one.

The public_html folder should be created within each of these directories. More flexibility will be provided by storing the actual site files.

The document's root location is in the /var/www folder. You can subdomain domain.com and domain2.com

mkdir -p /var/www/domain.com/public_html

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

Configure permissions

Create the directories and grant the appropriate permissions. Directory ownership may be assigned to the root user.

The files in the web directories should be accessible to regular users. Permissions can be changed by allowing the following rules:

chmod -R 755 /var/www

Create an index page

You can see some content by creating a demo index page. The index.html file should be created for each valid domain.

vim /var/www/domain.com/public_html/index.html

It's possible to set something such as domain.com testing in the file.

Example testing for domain.com

To exit, use the ESC tab and type:wp to save and close the file

For the second domain, repeat the steps. Here is the command you can use:

vim /var/www/domain2.com/public_html/index.html

Copy the Config File for Each Site

There is a default virtual host configuration file offered by Apache called 000-default.conf

It must be copied for each website's virtual host file. The default copy ensures that websites are always copied.

cp/etc/apache2/sites-available/000-default.conf/etc/apache2/sites-available/domain.com.conf

cp/etc/apache2/sites-available/000-default.conf/etc/apache2/sites-available/domain2.com.conf

Configure each site's configuration file

The first domain allows you to modify items. Additional directives can also be added. The server name directive needs to be added along with two other directives. It helps establish the base domain.

The second is Server Alias, which is an alternative name to the base domain. As you define hosts, such as www:, it helps match them

ServerName: domain.com Server Alias: www.domain.com

vim /etc/apache2/sites-available/domain.com.conf

This is what the Apache virtual hosts configuration nextcloud file will look like after completion:

ServerAdmin admin@example.com

ServerName domain.com

Server Alias www.domain.com

DocumentRoot /var/www/domain.com/public_html

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

You can save changes and exit by typing :wq. Afterward, you repeat the process for domain2.com.conf.

Make sure your second domain has a ServerName, Server Alias, and DocumentRoot.

Enable the Config File

Here are the steps you need to take to enable virtual host configuration files.

The first step is to disable the 000-default.conf default site:

a2dissite 000-default.conf

The new virtual host files can be enabled with Apache's tools. A2ensite's documentation provides instructions for enabling the new .conf files.

a2ensite domain.com.conf

a2ensite domain2.com.conf

The changes will be visible after restarting the Apache service. Systemctl status can be used to verify the changes.

systemctl restart apache2

Verify and test the Apache configurations

After restarting Apache, you should check that the configurations are working. You can edit the /etc/host file or the Apache virtual host subdomain file to accomplish this.

Upon opening the browser, the index.html pages previously created should display.

The steps are complete if both sites work correctly. The server has now been configured with two virtual hosts.

Conclusion

Multiple websites can be hosted on one server for your business, and the server will promptly respond to requests from the specified URLs. By following the steps outlined, you will be able to run multiple websites on a single server using Apache. A single server should have enough resources to accommodate multiple domains. To bring expert attention to the development project and make the most of Apache server, reaching out to top software development companies in India makes an ideal choice. 

https://www.hiddenbrains.com/

Login

Welcome to WriteUpCafe Community

Join our community to engage with fellow bloggers and increase the visibility of your blog.
Join WriteUpCafe