Managing multiple websites on a single Ubuntu server? And you need to keep it organized while ensuring that each one has a secure HTTP connection. That’s where a virtual host can be a game changer. Using a virtual host, you can manage and serve multiple domains easily from the same machine, eliminating potential risks. But in today’s digital landscape, serving websites over HTTP alone is not enough anymore—you need an SSL certificate for security, trust, and SEO benefits. To address this need, a free, automated SSL certificate service like Let’s Encrypt comes into play, which keeps your website protected without any interruption.
This guide explains the step-by-step process of setting up virtual hosts in Apache and securing them with the powerful extension—Lets Encrypt Certificates—making your website functional and secure.
Why Use Virtual Hosts?
Suppose instead of using separate servers—you are running multiple websites on a single server and managing everything efficiently with its own domain. Thanks to virtual hosts! It makes your server management more efficient and streamlines the overall process, whether you are hosting client websites, personal blogs, or testing environments.
But wait—there is more! Adding an SSL certificate is not just an option—it's a necessity. Google emphasizes HTTPS-enabled websites in search rankings, and visitors trust secure connections. Even though browsers identify unencrypted sites as “Not Secure,” it can break the trust of visitors and keep them away. With the Let’s Encrypt extension, you can secure your website by setting up SSL encryption-free, ensuring data privacy, improving SEO rankings, and most importantly, reducing the stress of manual certificate renewals.
Prerequisites
Before diving into the setup, make sure you have:
- A server running Ubuntu (mostly 20.4 or later recommended).
- Ensure Apache is installed and running effectively.
- A registered domain that targets your server’s IP address.
- Let's Encrypt installed via CertBot.
If you have not installed CerBot, do it with:
sudo apt update
sudo apt install certbot python3-certbot-apache
How to Set Up Virtual Host File on Ubuntu
Step 1: Create Your Virtual Host File
To start, you need to create a virtual host configuration file for your specific domain. This file allows Apache to access the website files and tells how to handle incoming requests. Navigate the Apache site configuration directory:
cd /etc/apache2/sites-available
Now create another new virtual host using Nano where to replace your_domain.com with your actual domain.
nano your_domain.conf
Now copy and paste the following configuration into your file and save it.
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName your_domain.com
ServerAlias www.your_domain.com
DocumentRoot /web/html
<Directory /web/html>
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow From All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
This configuration guides Apache to serve requests for your domain from the /web/html directory.
Step 2: Use Let’s Encrypt to Secure Your Site
The next step is to integrate SSL encryption—run this command to obtain and configure a free SSL certificate:
sudo certbot --apache -d your_domain.com -d www.your_domain.com
By running this prompt, Cerbot will automatically upgrade your virtual host file to redirect with HTTPS and protect your website.
Edit your virtual host file with (nano your_domain.conf) and add the following code:
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/your_domain.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/your_domain.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/your_domain.com/chain.pem
ServerAdmin webmaster@localhost
ServerName your_domain.com
ServerAlias www.your_domain.com
<Directory /web/html>
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow From All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Step 3: Enable Your Virtual Host
To activate the newly created virtual host, run the following prompt:
a2ensite your_domain.conf
Then restart Apache to apply the changes.
service apache2 restart
Step 4: Verify Your Set Up
Now is the time to test and set up the virtual host. Open your browser and go to https://your_domain.com. If everything is configured successfully, your website will run securely with HTTPS.
What is the Need for Virtual Host?
Through conventional methods, obtaining SSL certificates might be costly and complicated. Various small business owners either skipped it completely or used paid certificates. Now, using Let’s Encrypt, the process becomes simple and ensures that the security of the website is accessible to anyone. This allows users to secure their website free with a few easy commands.
By setting up virtual hosts along with SSL certificates, you can not only make your web server efficient but also ensure your website is protected from cyberattacks. However, as you know, Google prioritizes secure websites—having HTTPS can boost your search engine rankings, strengthen trust, and ensure sensitive data stays protected.
Conclusion
At the initial stage, integrating SSL certificates and virtual hosts might seem technical and complex, but with the right approach, you can streamline the process. Before Let’s Encrypt, conventional methods required expensive certificates and complex integrations. Once all steps are done successfully, you can now relax, getting your website secure, optimized, and well organized for better productivity and improved performance.
So what are you waiting for? Go ahead—and give it a try! Virtual hosting is not only about securing the website but also enjoying the confidence that comes with professionally configured web servers. Improve website security, trust, and SEO—all is possible with a few simple steps!