How To Create Apache Virtual Host on Ubuntu 22.04 LTS

Create Apache Virtual Host on Ubuntu 22.04

In this tutorial, we will show you how to create Apache Virtual Host on Ubuntu 22.04 LTS. For those of you who didn’t know, The term “Virtual Hosting” refers to the hosting of many domains on a single server. The virtual hosting concept is used by companies to host multiple websites using a single machine. It can be achieved either by an ‘IP-based’ method or a ‘domain-name-based’ approach. In ‘IP-based’ hosting we have distinct IP addresses for each website. In the case of ‘name-based’ hosting, we have multiple names running on each IP address.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step set up Apache Virtual Host on Ubuntu 22.04 (Jammy Jellyfish). You can follow the same instructions for Ubuntu 22.04 and any other Debian-based distribution like Linux Mint, Elementary OS, Pop!_OS, and more as well.

Prerequisites

  • A server running one of the following operating systems: Ubuntu 22.04, 20.04, and any other Debian-based distribution like Linux Mint.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo useror access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Create Apache Virtual Host on Ubuntu 22.04 LTS Jammy Jellyfish

Step 1. First, make sure that all your system packages are up-to-date by running the following apt commands in the terminal.

sudo apt update
sudo apt upgrade

Step 2. Installing Apache HTTP Server on Ubuntu 22.04.

By default, Apache is available on Ubuntu 22.04 base repository. Now run the following command below to install the latest version of Apache to your Ubuntu system:

sudo apt install apache2

After successfully installation, enable Apache (to start automatically upon system boot), start, and verify the status using the commands below:

sudo systemctl enable apache2
sudo systemctl start apache2
sudo systemctl status apache2

You can confirm the Apache2 version with the below command:

apache2 -v

Step 3. Configure Firewall.

Now we set up an Uncomplicated Firewall (UFW) with Apache to allow public access on default web ports for HTTP and HTTPS:

sudo ufw allow OpenSSH
sudo ufw allow 'Apache Full'
sudo ufw enable

Step 4. Creating a Virtual Host.

First, we create the website directory inside /var/www/:

mkdir -p  /var/www/idroot.us

Set permission and ownership of the domain document root directory and all files within the directory to the apache user:

sudo chown -R www-data: /var/www/idroot.us

Next, we create a new Virtual Host file using the nano text editor to hold all the configuration data:

sudo nano /etc/apache2/sites-available/idroot.us.conf

Add the following file:

<VirtualHost *:80>
    ServerName idroot.us
    ServerAlias www.idroot.us
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/idroot.us
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file, then restart the Apache webserver so that the changes take place:

sudo a2ensite idroot.us.conf
sudo a2dissite 000-default
sudo systemctl restart apache2

Let’s confirm that your new website’s configuration is working (you should see the message syntax OK):

sudo apache2ctl configtest

Step 5. Creating a Test HTML Page.

Now create a new HTML document inside your websites root folder:

sudo nano /var/www/idroot.us/index.html

Add the following file:

<html>
  <head>
    <title>Welcome to idroot</title>
  </head>
  <body>
    <h1>Hello World!</h1>

    <p>Welcome to my new website <strong>idroot.us</strong>.</p>
  </body>
</html>

Save and close the file, Now that you have your virtual hosts configured, you can test your setup easily by going to the domains that you configured in your web browser:

http://idroot.us

Congratulations! You have successfully set up Apache Virtual Host. Thanks for using this tutorial for creating Apache Virtual Host on Ubuntu 22.04 LTS Jammy Jellyfish system. For additional help or useful information, we recommend you check the official Apache website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!