How to create Virtual Hosts on Ubuntu 22.04 with Apache

How to create Virtual Hosts on Ubuntu 22.04 with Apache

Creating a virtual host in Ubuntu 22.04 involves several steps, and I'll guide you through the process using Apache as the web server. Make sure you have Apache installed on your system before proceeding. If not, you can install it by running:

sudo apt update
sudo apt install apache2

Once Apache is installed, follow these steps to create a virtual host:

Step 1: Create a Directory for Your Website

sudo mkdir /var/www/

Replace "" with your domain or the name of your project.

Step 2: Set Permissions

sudo chown -R $USER:$USER /var/www/
sudo chmod -R 755 /var/www/

Step 3: Create an HTML File

nano /var/www/

Add some content to the file (e.g., a simple "Hello, World!" message).

Step 4: Create a Virtual Host Configuration File

sudo nano /etc/apache2/sites-available/

Add the following configuration:

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted

Make sure to replace "" with your domain or project name.

Read More E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)

Step 5: Enable the Virtual Host

sudo a2ensite

Step 6: Disable the Default Site (Optional)

sudo a2dissite 000-default.conf

Step 7: Restart Apache

sudo systemctl restart apache2

Step 8: Update Hosts File (Optional)

If you are working on a local machine, you may want to update your hosts file to map the domain to your server's IP address. Open the host's file:

sudo nano /etc/hosts

Add the following line:

Replace "" with your domain.

Now, you should be able to access your virtual host by entering the domain in your web browser. I hope, it will help you generate a virtual host step by step. Thank you for your time.

Get The latest Coding solutions.

Subscribe to the Email Newsletter