Deploying a Web Server with Virtual Hosts and SSL
1. Setting up the Environment
It's time to put everything together. Today, we're deploying a server with multiple virtual hosts and connecting them with HTTPS. We'll test, debug, and set up everything for the final configuration.
Our Goal
In this project, we will deploy two websites on one web server:
- example.com
- test.com
For each site, we will configure a virtual host, enable HTTPS, and demonstrate the debugging and verification steps. Ready? Let's go!
Preparation
Before we start, let's make sure everything is set up properly:
You already have a web server (Nginx or Apache) installed.
If you're using Nginx:
sudo apt-get update sudo apt-get install nginx
Or Apache:
sudo apt-get update sudo apt-get install apache2
The Certbot package for working with SSL is installed.
For Nginx:
sudo apt-get install certbot python3-certbot-nginx
For Apache:
sudo apt-get install certbot python3-certbot-apache
You have access to a server where DNS is set up for the domains example.com and test.com, or you can use
/etc/hosts
for testing.
2. Setting Up Directories for Each Site
Let's start by creating directories and files for our websites.
Step 1: Create Folders for Websites
sudo mkdir -p /var/www/example.com
sudo mkdir -p /var/www/test.com
Step 2: Create Test HTML Files
Let's create a simple HTML file for each website:
example.com
sudo nano /var/www/example.com/index.html
Content:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to example.com</title>
</head>
<body>
<h1>This is example.com</h1>
<p>Welcome to example.com! The site is up and secure with SSL!</p>
</body>
</html>
test.com
sudo nano /var/www/test.com/index.html
Content:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to test.com</title>
</head>
<body>
<h1>This is test.com</h1>
<p>Welcome to test.com! The site is running and secure with SSL!</p>
</body>
</html>
3. Setting Up Virtual Hosts
We'll create a separate configuration file for each site.
Step 1: Configuration for Nginx
example.com
sudo nano /etc/nginx/sites-available/example.com
Content:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
test.com
sudo nano /etc/nginx/sites-available/test.com
Content:
server {
listen 80;
server_name test.com www.test.com;
root /var/www/test.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Activate configuration:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/test.com /etc/nginx/sites-enabled/
Restart server:
sudo nginx -t
sudo systemctl reload nginx
Step 2: Configuration for Apache
example.com
sudo nano /etc/apache2/sites-available/example.com.conf
Content:
<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>
test.com
sudo nano /etc/apache2/sites-available/test.com.conf
Content:
<VirtualHost *:80>
ServerName test.com
ServerAlias www.test.com
DocumentRoot /var/www/test.com
<Directory /var/www/test.com>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Activate configurations:
sudo a2ensite example.com.conf
sudo a2ensite test.com.conf
sudo systemctl reload apache2
4. Setting Up HTTPS for Websites
Now we’ll enable SSL for both sites using Let’s Encrypt.
sudo certbot --nginx
# or for Apache:
sudo certbot --apache
The process will prompt you to choose which sites to issue certificates for. Select example.com
and test.com
. Certbot will automatically update the server configurations.
5. Testing the Websites
Once setup is complete, visit your websites:
You should see the created HTML pages. Make sure the browser displays "Secure" in the address bar.
6. Debugging and Checking
If something goes wrong, check the logs:
For Nginx:
sudo tail -f /var/log/nginx/error.log
For Apache:
sudo tail -f /var/log/apache2/error.log
Or check availability with curl
:
curl -I http://example.com
curl -I https://example.com
7. Renewing Certificates
Certbot automatically updates certificates using the system's cron
. To check, you can manually run:
sudo certbot renew --dry-run
Step by step, we’ve set up a web server with support for multiple sites and HTTPS. You just went through the entire process from an empty server to one running with virtual hosts and SSL. Now your server is ready to show the world its sites. Isn’t that awesome?
GO TO FULL VERSION