Set Up Apache Virtual Hosts on Ubuntu 22.04 LTS

Introduction

Apache Web server can handle multiple domain request on single IP this type of configuration says virtualhost by using virtualhost we can host multiple website with single instance of apache web server.

In this post, we will configure the Apache virtual host using an Ubuntu 22.04 LTS machine.

Prerequisites

We need a root access on ubuntu machine to order to install and configure apache virtualhost on it.

Step 1: Install Apache Web Server

We need up and running apache web server, If you have already installed it So then you can ignore step 1, Use the following for the same.

To update the system repository.

sudo apt-get update

To install the Apache2 package on ubuntu machine.

sudo apt-get install apache2 -y

The above commad install the Apache web server on your machine, We need validate the apache server is ruuning or not, Use the follwing command for the same.

sudo systemctl status apache2.service

We should get apache service up and running state, After this we need to open the port 80 in our firewall if we have enabled it.

Step 2: Setting up Virtual host

Create a directory that will be used to store data on the website in order to set a virtual host in Apache. We will use the “cd” command to move to the “/var/www” directory for this purpose:

Create a DocumentRoot direcotry to store the project deployment code, In my case i am using mydomain.com as a domain diretory for your apache virtualhost.

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

Updating the required permission on it.

sudo chown -R $USER:$USER /var/www/mydomain.com/public_html
sudo chmod -R 755 /var/www

copy of the existing default virtualhost config file create a new virtualhost file named mydomain.com.conf by using given command.

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

Open the newly created virtualhost configuration file named mydomain.com.conf using vim or nano text editor.

sudo vim /etc/apache2/sites-available/mydomain.com.conf

Paste the following configuration like this.

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName mydomain.com
    ServerAlias www.mydomain.com
    DocumentRoot /var/www/mydomain.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

We need to validate and update the following configuration as per our senario.

  • On line 1, We need to validate the IP address or we can use the * to attach all the NIC’s IP address with apache web server, We should get * by fault and port 80, We need to leave on default setting.
  • On line 2, We need to update the ServerAdmin email id over thier.
  • On line 3, We must update the domain name our domain name.
  • On line 4, We need to update ServerAlias with our sub-domain.
  • On line 5, We need to update the DocumentRoot path where our deployment code going to deploy.
  • On line 6 and 7, We need to specify the logs direcoty if we want to generate somewhere else otherwise We can use default setting.
  • On line 8, Its a virtualhost closing statement, No need any changes.

Enable the mydomain.com virtualhost in apache web server.

sudo a2ensite mydomain.com.conf

After this, We need to reload or restart the apahe web server service to get new changes on it.

sudo systemctl reload apache2.service

Now we have the apache virtualhost configuration semilier we can add more virtualhost, As per our need, Lets open the apache web server using our custom doman name in browser.

We need to use the following endpomnits for the same.

http://mydomain.com and http://www.mydomain.com

Conclusion

We have successfully installed and configure Apache virtualhost on ubuntu 22.04 LTS, If you still have questions, please post them in the comments section below.

Author

Set Up Apache Virtual Hosts on Ubuntu 22.04 LTS

8 thoughts on “Set Up Apache Virtual Hosts on Ubuntu 22.04 LTS

  1. I just could not leave your site before suggesting that I actually loved the standard information an individual supply for your guests? Is going to be again regularly in order to inspect new posts

  2. будут полезны для вашего SEO-продвижения. Успехов в продвижении вашего сайта! says:

    This is to easy to understand.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top