Virtual Host Configuration
1. What are virtual hosts?
Imagine you're opening a cozy café. Most customers come for coffee ☕, but sometimes someone orders tea 🍵. Instead of opening a separate place for tea lovers, you just set aside a small corner for the tea fans. In the world of web servers, this concept is called virtual hosts. They let your server handle multiple websites (with different domains) while keeping a single physical server.
Types of virtual hosts
- IP-based — each site gets its unique IP address. It's like a VIP section at a concert: every client with a VIP ticket gets their own spot.
- Name-based — multiple sites can share a single IP address. Here, the server uses the host name (domain name) from the client's request to figure out which site to show. This is the more common and convenient option for most projects.
2. Setting Up a Virtual Host in Nginx
1. Creating a New Virtual Host
To set up a virtual host, you first need to create a configuration file. This file will contain info on how the server should handle requests for a specific domain.
Create a file for your site, for example, example.com
:
sudo nano /etc/nginx/sites-available/example.com
Add the following configuration block:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Let’s break down the code:
listen 80;
: Specifies that the server will listen on port 80 (by default used for HTTP).server_name example.com www.example.com;
: Defines what domain names the server should respond to.root /var/www/example.com;
: This directive specifies the root folder where the site files are stored.index index.html;
: Defines the file that will be used as the "homepage" of the site.location / { try_files $uri $uri/ =404; }
: Handles routing requests. If the file or directory is not found, a 404 error is returned.
2. Activating the Configuration
Now we need to “enable” our virtual host. In Nginx, this is done through symbolic links:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Then check to make sure there are no errors in the configuration:
sudo nginx -t
If everything’s good, restart the server:
sudo systemctl reload nginx
3. Testing It Out
Create a folder for your site’s files:
sudo mkdir -p /var/www/example.com
sudo nano /var/www/example.com/index.html
Add the following simple HTML code:
<h1>Welcome to example.com!</h1>
<p>Your Nginx server is ready to serve.</p>
Save the file, then open a browser: http://example.com
. If you see a welcome message, that means the virtual host is working!
4. Symbolic Links in Linux
Symbolic links (or soft links, symlinks) are special file objects that point to another file or directory. They work like “shortcuts” in Windows, letting you access a target file or folder via an alternative path.
Symbolic links contain the path to the original object, not the object itself. If the original file or folder is moved or deleted, the link becomes “broken,” as it no longer points to an existing object.
Command to create a symbolic link:
ln -s target link_name
target
— path to the original object.link_name
— name of the link.
Example:
ln -s /var/log/syslog log_link
Now you can access the file /var/log/syslog
via log_link
.
Usage:
- Making navigation easier.
- Creating alternative access points to files.
- Organizing directories.
3. Setting up a Virtual Host in Apache
1. Creating a Configuration File
Apache uses a different approach. Here, configurations for virtual hosts are located inside the folder /etc/apache2/sites-available/
. Let's create a file for our site:
sudo nano /etc/apache2/sites-available/example.com.conf
Add the following code:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
<Directory /var/www/example.com>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
A quick explanation:
<VirtualHost *:80>
: Specifies that this configuration applies to all requests on port 80.ServerName
andServerAlias
: Define the main and additional domain names for the site.DocumentRoot
: Points to the root folder of the site.<Directory>
: Block that sets access permissions and behavior for the specified directory.
2. Activating the Configuration
To activate the configuration, run the command:
sudo a2ensite example.com.conf
Restart the server to apply the changes:
sudo systemctl restart apache2
3. Testing the Setup
Again, create a folder and a file for the site:
sudo mkdir -p /var/www/example.com
sudo nano /var/www/example.com/index.html
Fill it with the following content:
<h1>Welcome to example.com on Apache!</h1>
Save it, and then open it in a browser: http://example.com
. If you see the welcome page — congrats, you've set up a virtual host in Apache!
4. Checking and Managing Multiple Hosts
Setting Up a Second Virtual Host
Say you wanna serve a second site: test.com
. The process is the same:
- Create a new configuration file.
- Specify a unique domain name and root folder.
- Activate the configuration and restart the server.
For Nginx:
sudo nano /etc/nginx/sites-available/test.com
sudo ln -s /etc/nginx/sites-available/test.com /etc/nginx/sites-enabled/
sudo systemctl reload nginx
For Apache:
sudo nano /etc/apache2/sites-available/test.com.conf
sudo a2ensite test.com.conf
sudo systemctl restart apache2
Debugging Issues
If your virtual hosts aren't working, start by checking for errors. For example:
For Nginx:
sudo tail -f /var/log/nginx/error.log
For Apache:
sudo tail -f /var/log/apache2/error.log
You can also use the curl
command to test the site directly:
curl -I http://example.com
Now you can set up virtual hosts and run multiple sites on one server. Alongside this comes HTTPS configuration to make your sites secure. But hey, that's a topic for the next lecture!
GO TO FULL VERSION