The purpose of this post is to offer direction on configuring different domains using Apache Virtual Host, so that multiple websites can be hosted on a single Apache server in Ubuntu.
This comprehensive step-by-step guide provides clear instructions on how to configure Apache Virtual Host to host multiple websites on a single Apache server in Ubuntu.
Whether you’re a beginner or an experienced developer, this guide will walk you through the process with ease, helping you to optimize your server setup and improve your website performance.
Follow these instructions to achieve a seamless multi-site hosting experience.
How to Host Multiple Domains on One Server
If your business is expanding, you may consider incorporating new websites. It would be cost-effective to utilize a single server for hosting all your domains.
With virtual hosting, it becomes feasible to host several domains on a solitary server, enabling an efficient allocation of resources such as CPU, RAM, HDD space, and others, which can be ample on a server.
Multiple sites can share the same server resources.
This facilitates the hosting of several websites through a single web server instance. Define it in a conf file with the URL & IP addresses.
Upon receiving a request, the traffic will be served from the Document Root.
Setup for hosting multiple websites on one server using Apache.
Install Apache Web Server
In this guide, I will provide instructions on how to install Apache on Ubuntu 22.04, which is the most commonly used web server globally.
An Overview of the Apache Web Server.
Apache is a widely used open-source HTTP server and one of the most popular in the world. It powers a significant portion of the Internet, offering powerful features such as dynamic load modules, robust media support, and seamless integration with other widely-used software.
Installing Apache.
$ sudo apt update
To make sure we have the latest updates, we’ll refresh the local package index as Apache is obtainable from Ubuntu’s pre-installed software repositories.
Then, install the apache2 package.
$ sudo apt install apache2
After installation, you can verify the version of Apache installed using the following command:
$ sudo apache2ctl -v
Upon successful installation of Apache, the Apache web server is automatically started in Ubuntu 22.04. To verify the status of Apache, execute the following command:
$ sudo systemctl status apache2
$ sudo systemctl status apache2
Access the link using your browser.
http://your_server_ip
You can see the default Ubuntu 22.04 Apache web server page.
Managing Apache
To stop web server:
$ sudo systemctl stop apache2
To start Apache server:
$ sudo systemctl start apache2
To restart the web server:
$ sudo systemctl restart apache2
When making configuration modifications, Apache can frequently reload without disrupting connections.
$ sudo systemctl reload apache2
To disable the default automatic startup behavior of Apache, enter the following command:
$ sudo systemctl disable apache2
Create the Directory Structure
Site data is stored in the directory structure and served to visitors, with Apache searching for content in the top-level directory, known as the Document Root.
The document root is configured with specific directories such as /var/www, which can be utilized to create a dedicated directory for each website that you intend to host.
For increased flexibility, make sure to create a “public_html” folder in each of these directories to store the actual site files.
During this step, you should create the /var/www directory, which will serve as the document root location. Substitute domain names, such as domain.com and domain2.com.
$ sudo mkdir /var/www/site1
$ sudo mkdir /var/www/site2
Set up Permissions
Once you have created the directories, it is important to grant the appropriate permissions. It is possible for the root user to own the directories.
To enable a regular user to modify the files in the web directories, you will need to adjust the permissions according to the following rules:
$ sudo chmod -R 755 /var/www
Set up index Page
You can create demo web page. Make us index.html file for each valid domain to website
$ sudo nano /var/www/site1/index.html
You can set something as a
<html>
<head>
<title>domain.com</title>
</head>
<body>
<h1>Welcome to domain.com website</h1>
</body>
</html>
Save and close the file to exit by using the ctrl+x, y for yes and enter
Please copy and paste the following content into the file.
$ sudo nano /var/www/site2/index.html
<html>
<head>
<title>domain.com</title>
</head>
<body>
<h1>Welcome to domain2.com website</h1>
</body>
</html>
Save and close the file to exit by using the ctrl+x, y for yes and enter
Copy the Conf File for Each Site
Apache provides a default virtual host file named 000-default.conf
You must create a virtual host file for each website by duplicating the default copy.
$ sudo cp /etc/apache2/site-available/000-default.conf /etc/apache2/sites-available/site1.conf
$ sudo cp /etc/apache2/site-available/000-default.conf /etc/apache2/sites-available/site2.conf
Modify the configuration file for each website.
You may edit the entry for the primary domain and include additional directives, such as serverName, which aids in establishing the root domain.
The second directive is serverAlias, which provides an alternate name for the root domain and aids in matching the defined hosts, such as www.
ServerName: domain.com ServerAlias: www.domain.com
$ sudo nano /etc/apache2/sites-available/site1.conf
Once finished, the Apache virtual host file should resemble the following:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/site1
</VirtualHost>
Save and close the file to exit by using the ctrl+x, y for yes and enter
Generate an Apache virtual host configuration file for domain1.com:
$ sudo nano /etc/apache2/sites-available/site2.conf
Add the following lines:
<VirtualHost *:80>
ServerAdmin webmaster@ localhost
ServerName domain.com
ServerAlias www.domain2.com
DocumentRoot /var/www/site2
</VirtualHost>
Save and close the file to exit by using the ctrl+x, y for yes and enter
Activate the configuration file.
The virtual host configuration files can be enabled by following these steps:
Initially, disable the default site that is defined in 000-default.conf.
$ sudo a2ensite 000-default.conf
The Apache tools used to activate the new virtual host files.
Next, enable the virtual host configuration file using the following command:
$ sudo a2ensite site1.conf
$ sudo a2ensite site2.conf
To implement the changes, restart the Apache service.
$ sudo systemctl restart apache2
Verify and test the Apache configurations.
When visiting the website at http://domain.com, you should observe results similar to the following:
Welcome to domain.com website
Similarly, when visiting the website at http://domain2.com, you should see results like:
Welcome to domain2.com website
Conclusion:
One server can be used to host an unlimited number of websites for your business, and it will efficiently respond to requests from the specified URLs. Using Apache, you can follow the steps outlined in the tutorial to host two websites.
By following these steps, you can effectively establish the sites on a single server. It is important to ensure that the server has sufficient resources to accommodate both domains.