How to Setup Apache Virtual Hosts on Ubuntu 22.04

Apache Virtual Hosts offer a powerful way to manage multiple websites on a single server, allowing you to host multiple domains on one physical machine. This article will guide you through the process of setting up Apache Virtual Hosts on Ubuntu 22.04, ensuring you can efficiently manage your web hosting environment.

Apache Virtual Hosts is a feature that enables a single Apache web server to serve multiple websites simultaneously. Moreover, each website, known as a virtual host, has its separate configuration, allowing for greater flexibility and organization.

Prerequisites to Setup Apache Virtual Host on Ubuntu 22.04

Before setting up Apache Virtual Hosts on Ubuntu 22.04, ensure you have the following prerequisites in place:

  • Ubuntu 22.04 is installed and running on your server.
  • Apache web server installed. 

The apache2 generally comes pre-installed on Ubuntu and you can check its version by executing the below command:

$ apache2 -v
Setup Apache Virtual Hosts on Ubuntu

If for any reason it’s not installed then you can install it using the following command:

$ sudo apt install apache2
Setup Apache Virtual Hosts on Ubuntu

Next, you can check the status of apache2 web server if it’s up and running by executing:

$ systemctl status apache2
Virtual Hosts on Ubuntu

You can see in the above image that the apache2 web server is currently active.

Another method to verify this is by opening your desired web browser on Ubuntu and typing “localhost”:

Ubuntu

When you visit “localhost” in your browser, the Apache2 server responds to your request by serving up its default web page. So, seeing Apache2 means that the server is up and running correctly, and ready to serve web pages.

How to Set up Apache Virtual Host on Ubuntu 22.04

When you’re setting up a web server using Apache2, you’ll often want to put your website files in a location where the server can access them. By navigating to “/var/www”, you’re positioning yourself within the directory where Apache2 expects to find web content. This means that when you add or create web files here, Apache2 can serve them to visitors who access your website through their web browsers.

Step 1: Creating a directory structure

Execute the following command to navigate there:

cd /var/www
Setup Apache

You need to create a directory for each website you want to host. For example:

sudo mkdir -p /var/www/example.com/
Virtual Hosts on Ubuntu

The purpose of this command is to create a directory named “example.com” inside the “/var/www” directory on the file system. The “-p” option ensures that if the “/var/www” directory doesn’t exist, it and any intermediate directories needed to reach it will be created.

By creating the directory “/var/www/example.com/”, you’re preparing a location to store files specific to that domain within the web server’s file system.

Step 2: Changing Ownership

Next, you need to change the ownership of the directory “/var/www/example.com” and all its contents to the user and group “www-data.” By doing so, you are ensuring that the Apache2 web server process running on your system has the necessary permissions to access and serve the files within this directory.

sudo chown -R www-data:www-data /var/www/example.com
Setup Apache on Ubuntu

Step 3: Creating a Web Page

After that, you need to set up the HTML home page for your apache2 web server which is “index.html” in this case.

sudo nano /var/www/example.com/index.html

The nano text editor has been used for this purpose and then we need to write the following HTML code inside this file:

<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1> This guide is about setting up apache2 web server</h1>
</body>
</html>
Setup Apache

Step 4: Creating an Apache Virtual Host file

When setting up a website on an Apache2 web server, it’s essential to configure Apache2 to serve the website correctly. This includes defining how Apache2 should handle requests for specific domains, such as “example.com” in this case:

$ sudo nano /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

The configuration file specified by this command allows you to define settings such as the document root directory, server name, virtual hosts, and other directives related to how Apache2 should serve the website.

 Virtual Hosts on Ubuntu

Step 5: Configure the Main Apache2 File

Next, you need to open the Apache configuration file with superuser privileges, allowing you to view and modify the global settings for your Apache web server:

sudo nano /etc/apache2/apache2.conf

After opening this file add the following line and exit the file after saving it:

ServerName example.com
Setup Apache Virtual Hosts on Ubuntu

Adding ServerName example.com ensures that Apache accurately identifies itself and handles requests appropriately. Here, you need to provide your desired name in place of example.com.

Step 6: Enabling Virtual Host Configuration

Now, you need to enable a virtual host configuration file that allows Apache to serve content for the associated domain or website. Once enabled, Apache will read the configuration settings from this file during server startup or reload:

sudo a2ensite example.com.conf
Apache Virtual Hosts on Ubuntu

Step 7: Disabling Default Virtual Host Configuration 

You also need to disable the default virtual host configuration to stop the Apache from serving content for the associated domain or website:

sudo a2dissite 000-default.conf
Setup Apache Virtual Hosts on Ubuntu

After disabling a virtual host configuration file, you usually need to reload Apache for the changes to take effect:

sudo systemctl reload apache2
Setup Apache

Step 8: Error testing

Lastly, if you need to verify that all the configurations that you made are correct and there is no error that can occur then you can do that by executing:

sudo apache2ctl configtest
Setup Apache Virtual Hosts on Ubuntu

Running this command after modifying your Apache configuration files is recommended to guarantee the continued smooth operation of your web server.

You can see that there is no error showing in the output which means that the Apache virtual host has been set up successfully on Ubuntu 22.04.

Benefits of Setting up Apache Virtual Hosts on Ubuntu 22.04

1 – Efficient Resource Utilization:

  • Centralized hosting of multiple websites on a single server.
  • Optimizes server resources by managing multiple domains efficiently.

2 – Flexibility and Organization:

  • Each website has its own configuration.
  • Enhances flexibility in managing different websites.
  • Organizes server setup for streamlined maintenance.

3 – Seamless Hosting of Multiple Domains:

  • Simplifies the process of hosting multiple domains.
  • Enables easy management and configuration of various websites.

4 – Cost Reduction and Simplified Infrastructure:

  • Reduces infrastructure costs by consolidating hosting.
  • Minimizes complexity by hosting multiple websites on a single server.

5 – Streamlined Website Management:

  • Centralizes website management tasks.
  • Simplifies server administration and maintenance processes.

Conclusion

Setting up Apache Virtual Hosts on Ubuntu 22.04 provides a robust solution for efficiently managing multiple websites on a single server. By following the outlined steps, you can establish separate configurations for each virtual host. Also, ensuring flexibility and organization in your web hosting environment.

Setting up Apache Virtual Hosts on Ubuntu 22.04 enables efficient management of multiple websites on a single server, optimizing resource utilization. Moreover, each website can have its own configuration, enhancing flexibility and organization. Additionally, Apache Virtual Hosts facilitate seamless hosting of multiple domains, simplifying website management tasks. 

Apache provides value flexibility and extensive customization. Ultimately, the best web server for you depends on your unique project requirements. Rent a VPS server reliable to you with scalability that adapts your resources to meet your growing needs.

FAQ

What is Apache Virtual Host?
Why should I use Virtual Hosts?
How do I install Apache on Ubuntu 22.04?
Where are Apache Virtual Host configuration files located?

Related Post

How to Check Ubuntu Version via Terminal and

Understanding your Ubuntu version is important for stay...

How to Install and Configure NFS on Ubuntu

NFS, which stands for Network File System, is a tool th...

How to Install CyberPanel on Ubuntu

CyberPanel is a control panel designed to simplify the ...

How to Install TensorFlow on Ubuntu

TensorFlow is a powerful open-source machine-learning l...

How to Install OpenJDK on Ubuntu

OpenJDK the Open Java Development Kit is a free and ope...

How To Install the OpenLiteSpeed Web Server o

OpenLiteSpeed is a lightweight and powerful open-source...

Leave a Comment