How to setup Virtual host in Apache and Ubuntu
December 13, 2025 at 11:20:00 AM
pawan
Setting Up a Virtual Host in Ubuntu/WSL with Windows
Prerequisites
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:
- Open the hosts file in Windows:
C:\Windows\System32\drivers\etc\hosts
- Find your WSL IP address:
hostname -I
- 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.
SSH
Ubuntu
0 comments