Have you ever wanted to run more than one website on the same computer or server? That’s where a virtual host comes in.
I remember the first time I heard about virtual hosts. I thought it sounded complicated. But once I learned how to set one up in Apache, it wasn’t so bad. In fact, I use them all the time now.
Let me walk you through what I’ve learned. I’ll keep it simple and clear so you can do it too.
What is a Virtual Host?
A virtual host lets you run multiple websites on one machine. You don’t need a new computer for each website. Apache, which is a popular web server, can tell the difference between websites using settings in a file.
For example, you can have:
www.catsite.com
www.dogworld.net
www.myschoolproject.local
All running on the same server. Cool, right?
There are two types of virtual hosts:
- Name-based: Uses the website name to tell which site to show.
- IP-based: Uses a different IP address for each site (this is less common for beginners).
Most people use name-based virtual hosts.
What You Need First
Before we start, let me ask you:
Do you have Apache installed already?
If not, you’ll need to install it. Apache works on Linux, macOS, and Windows. But for this guide, I’ll focus on Linux. That’s what I use most.
Here’s what you should have:
- A Linux server or computer (Ubuntu is a good choice)
- Apache installed
- Access to terminal or command line
- Root or sudo access (this means you can make system changes)
If you don’t have these, you might want to ask someone to help or read how to install Apache first.
Step-by-Step: Setting Up a Virtual Host
Let’s say you want to set up a website called mytestsite.local
.
1. Create a Directory for Your Website
Apache needs a folder where your site’s files will live. You can make it like this:
sudo mkdir -p /var/www/mytestsite.local/public_html
This creates a folder for your website.
Now give yourself permission to use it:
sudo chown -R $USER:$USER /var/www/mytestsite.local/public_html
2. Add an HTML File
Create a test file so you know it works:
echo "<html><body><h1>Hello from MyTestSite!</h1></body></html>" > /var/www/mytestsite.local/public_html/index.html
3. Create a Virtual Host File
Apache stores settings for each site in files called configuration files. Let’s make one:
sudo nano /etc/apache2/sites-available/mytestsite.local.conf
Then paste this:
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName mytestsite.local
DocumentRoot /var/www/mytestsite.local/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save and close the file (press Ctrl + O, Enter, then Ctrl + X).
4. Enable the Virtual Host
Now tell Apache to use this new file:
sudo a2ensite mytestsite.local.conf
Then reload Apache:
sudo systemctl reload apache2
5. Update Your Hosts File
Apache is ready, but your computer doesn’t know where to find mytestsite.local
. We fix that like this:
sudo nano /etc/hosts
Add this line at the bottom:
127.0.0.1 mytestsite.local
Save and close.
Now open a browser and go to:
http://mytestsite.local
You should see your test page. If not, go back and check each step.
Why Use Virtual Hosts?
Here are some good reasons:
- Save money: Run many websites on one server
- Organize projects: Great for developers working on different sites
- Test locally: Try websites before putting them online
I use virtual hosts for school projects, client sites, and personal tests.
Common Mistakes to Avoid
Here are some things that tripped me up when I started:
- Forgetting to reload Apache after making changes
- Misspelling folder names
- Leaving out
ServerName
in the config file - Not updating the hosts file
Take your time. Slow is fast when you’re learning.
Want to Do More?
Now that you know how to set up one virtual host, try adding another. Use a different folder name and a new config file. You’ll get faster each time.
Also, if you want to go live with your site, you’ll need a domain name and real server. That’s a topic for another post.
Quick Review
To set up a virtual host in Apache, you:
- Make a folder for your site
- Add a test HTML file
- Create a virtual host config file
- Enable the site and reload Apache
- Update your hosts file
With practice, it becomes second nature.
Final Thoughts
Learning to use virtual hosts made a big difference for me. I stopped making a mess of my main website folder. Everything got cleaner. More organized.
If you’re working on more than one website, give virtual hosts a try. It’s a simple skill that saves a lot of time.
So, what site will you set up first?