Basic Virtual Host Configuration
Set up virtual hosts (or server blocks) to host multiple websites on a single server using different domain names.
Basic Virtual Host Configuration is a free Linux Server Deployment & SSH Mastery lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Linux Server Deployment & SSH Mastery learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Hosting Multiple Websites
Imagine you have one powerful server, but you want to host several different websites, each with its own domain name (like myshop.com and myblog.com).
How can one server handle this efficiently without needing a separate machine for each site?
What are Virtual Hosts?
This is where Virtual Hosts (Apache) or Server Blocks (Nginx) come in!
- They are configuration units that allow a single web server to host multiple domain names.
- Each domain can have its own document root (where its files live), logs, and specific settings.
- This saves resources and simplifies server management.
Apache's Virtual Host Setup
In Apache, virtual host configurations are typically stored in .conf files within specific directories:
/etc/apache2/sites-available/: Contains all available virtual host configurations./etc/apache2/sites-enabled/: Contains symbolic links to configurations that are currently active.
We use commands like a2ensite to enable them.
Apache: Basic Virtual Host
Here's a simple Apache virtual host configuration. This file would be created in sites-available.
<VirtualHost *:80>
ServerAdmin webmaster@example.com
ServerName myfirstsite.com
ServerAlias www.myfirstsite.com
DocumentRoot /var/www/myfirstsite
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>Apache: Steps to Configure
To set up an Apache virtual host for myfirstsite.com:
- Create a directory for your website's files.
- Create the virtual host configuration file.
- Enable the site and reload Apache.
Let's look at the commands for the first two steps:
sudo mkdir -p /var/www/myfirstsite/html
sudo chown -R $USER:$USER /var/www/myfirstsite/html
sudo nano /etc/apache2/sites-available/myfirstsite.confApache: Enable & Reload
After creating and saving your .conf file, use these commands to activate it and apply the changes:
sudo a2ensite myfirstsite.conf: Creates a symbolic link insites-enabled.sudo apache2ctl configtest: Checks for syntax errors in your Apache configuration.sudo systemctl reload apache2: Applies the new configuration without restarting the server.
sudo a2ensite myfirstsite.conf
sudo apache2ctl configtest
sudo systemctl reload apache2Nginx's Server Block Setup
Nginx uses Server Blocks, which are very similar to Apache's virtual hosts. They also use a similar directory structure:
/etc/nginx/sites-available/: Stores all server block configurations./etc/nginx/sites-enabled/: Contains symbolic links to active server blocks.
Instead of a helper command, we manually create symbolic links.
Nginx: Basic Server Block
Here's a basic Nginx server block configuration for mysecondsite.com. This file would be created in sites-available.
server {
listen 80;
listen [::]:80;
root /var/www/mysecondsite/html;
index index.html index.htm;
server_name mysecondsite.com www.mysecondsite.com;
location / {
try_files $uri $uri/ =404;
}
}Nginx: Steps to Configure
To set up an Nginx server block for mysecondsite.com:
- Create a directory for your website's files.
- Create the server block configuration file.
- Create a symbolic link and reload Nginx.
Here are the commands for the first two steps:
sudo mkdir -p /var/www/mysecondsite/html
sudo chown -R $USER:$USER /var/www/mysecondsite/html
sudo nano /etc/nginx/sites-available/mysecondsiteNginx: Enable & Reload
After creating and saving your server block file, use these commands to activate it and apply the changes:
sudo ln -s /etc/nginx/sites-available/mysecondsite /etc/nginx/sites-enabled/: Creates the symbolic link.sudo nginx -t: Tests the Nginx configuration for syntax errors.sudo systemctl reload nginx: Applies the new configuration without restarting.
sudo ln -s /etc/nginx/sites-available/mysecondsite /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginxVirtual Host Check
You've learned how to configure web servers to host multiple sites. Let's check your understanding.
Recap: Multiple Sites, One Server
Great job! You've learned the essentials of configuring virtual hosts (Apache) and server blocks (Nginx).
- These configurations allow you to efficiently host multiple websites on a single server.
- You now know how to set up distinct configurations for different domain names.
- This is a fundamental skill for web server administration and deploying multiple applications.
Frequently asked questions
Is the “Basic Virtual Host Configuration” lesson free?
Yes — the full text of “Basic Virtual Host Configuration” is free to read here on the web, and the Linux Server Deployment & SSH Mastery course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Linux Server Deployment & SSH Mastery course, upgrade to CoddyKit PRO.
What will I learn in “Basic Virtual Host Configuration”?
Set up virtual hosts (or server blocks) to host multiple websites on a single server using different domain names. You practise Linux Server Deployment & SSH Mastery with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Linux Server Deployment & SSH Mastery?
No prior experience is required. Linux Server Deployment & SSH Mastery on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Basic Virtual Host Configuration” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Linux Server Deployment & SSH Mastery lesson?
Yes. Every Linux Server Deployment & SSH Mastery lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Installing Nginx/Apache
- Hosting Static Websites
- Basic Virtual Host Configuration
- Enabling HTTPS with Let's Encrypt