Pawan Labs Pawan Labs

How to setup Virtual host in Apache and Ubuntu

Setting Up a Virtual Host in Ubuntu/WSL with Windows

Prerequisites

  • Apache2 installed

Step 1: Create the Virtual Host Directory

Create a directory to store your website files:

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

Add some content to the index.html file and save it. This directory will serve as the root for your virtual host.

Step 2: Create and Configure the Virtual Host File

Copy the default Apache configuration file and customize it:

cd /etc/apache2/sites-available
sudo cp 000-default.conf example.com.conf
sudo nano example.com.conf

Update the following lines in the configuration file:

DocumentRoot /var/www/html/example.com
ServerName example.com

Save and exit the file.

Step 3: Enable the Virtual Host

Enable your new virtual host:

sudo a2ensite example.com.conf

Step 4: Update the Hosts File

Map your domain to localhost so your system routes traffic correctly.

For WSL on Windows:

  1. Open the hosts file in Windows: C:\Windows\System32\drivers\etc\hosts
  2. Find your WSL IP address:
    hostname -I
    
  3. Add this line to the end of the hosts file:
    172.18.245.123 example.com
    

For Ubuntu:

sudo nano /etc/hosts

Add this line to the end of the file:

127.0.0.1 example.com

Step 5: Restart Apache

Apply the changes by restarting Apache:

sudo systemctl restart apache2

Your virtual host is now ready to use. Visit http://example.com in your browser to access it.

Sign in to publish a comment

0 comments

Be the first to comment on this post.

Adobe Certified Expert
Adobe Certified Expert
Adobe Commerce contributor
Green Software for practitioners
mongodbinc
Adobe Certified Professional