How to Set Up Apache Virtual Hosts on Ubuntu
In this guide, I’ll show you how to set up Apache Virtual Hosts on Ubuntu. Virtual Hosts allow you to run multiple websites on a single Apache server, making it a powerful feature for anyone managing multiple domains or projects.
Whether you’re hosting several sites on a single machine or setting up development environments, this step-by-step guide will help you configure and manage virtual hosts efficiently. By the end of this tutorial, you’ll have the skills to host multiple websites on your Ubuntu server with ease. Let’s get started!
What Are Apache Virtual Hosts?
Apache Virtual Hosts is a feature that allows you to host multiple websites on a single server. Instead of needing a separate physical or virtual server for each website, Apache Virtual Hosts lets you configure multiple domain names and websites to run independently, all from the same Apache installation. This is especially useful for developers, businesses, and hosting providers who manage multiple domains or projects.
Prerequisites
Before we begin, make sure you have the following:
- Ubuntu Server: Ensure you have a running version of Ubuntu with a user who has sudo privileges
- A terminal or SSH client to connect to your server
- Apache Installed: Apache must be installed on your Ubuntu server. If it’s not already installed, you can follow our guide on How to Set Up Apache Web Server on Ubuntu
- Domain Names: You should have one or more domains or subdomains you want to use for your virtual hosts. For testing purposes, you can also use dummy domains or local domains.
Step 1: Create the Directory Structure for Your Site
By default, Apache comes with a basic site enabled found in /var/www/html
.
With virtual hosts we will create our own folder, which will encapsulate configuration details for our domain.
Remember to replace example.com
with your actual domain name. If you plan on hosting more websites, you’ll need to create separate directories for each site you plan to host.
sudo mkdir /var/www/example.com/
The example.com
folder will be where you store the website files for your domain.
Go to that file location.
cd /var/www/example.com/
Assign the ownership to your current user.
sudo chown -R $USER:$USER /var/www/example.com
sudo chmod -R 755 /var/www/example.com
We will create a simple website that we can use to play around with. You can always update the code later.
Step 2: Create a Demo Page for Your Site
Create a new file index.html
in the /var/www/example.com/
folder.
sudo nano index.html
Paste in the following code.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Simple Website</title>
</head>
<body>
<h1>Welcome to My Simple Website</h1>
<p>This is a very basic webpage with Virtual Hosts Enabled.</p>
<p>© 2024 My Simple Website</p>
</body>
</html>
This will create a basic HTML file to confirm that the virtual host is functioning properly.
Step 3: Create Virtual Host Configuration Files
In order for Apache to serve the code in the index.html
file, we need to create a virtual hosts configuration file.
By default, these files are stored in the /etc/apache2/sites-available/
directory.
As we are setting up our own virtual hosts configuration, go to this directory and create a new file for yout domain.
cd /etc/apache2/sites-available/
sudo touch example.com.conf
Add the following configuration (replace example.com
with your domain name):
<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Step 4: Enable the Virtual Hosts
Now, you need to enable the newly created virtual host file using the a2ensite
command:
sudo a2ensite example.com.conf
sudo systemctl reload apache2
To ensure no conflicts, you should disable the default Apache site:
sudo a2dissite 000-default.conf
Finally, reload Apache to apply the changes.
sudo systemctl reload apache2
Step 5: Test Your Configuration
Apache will now be serving your example.com
configuration files instead of the default site. To test if the virtual hosts are set up correctly, open a web browser and enter the domain name http://example.com
.
It should look similar to this:
Step 6: Secure Your Server with SSL (Optional)
If you plan to use HTTPS, you’ll need to enable SSL for each virtual host. Apache provides a simple way to do this using Let's Encrypt
:
Since almost all modern websites use HTTPS for secure and encrypted communication over the internet, this step is highly recommended if you plan to use your website for other than testing purposes.
First, install Certbot
:
sudo apt install certbot python3-certbot-apache
Once Certbot is installed, get an SSL certificate for your domain:
sudo certbot --apache -d example.com -d www.example.com
Certbot will ask you for an email address (for renewal notifications and urgent security notices). You will need to agree to the Let’s Encrypt terms of service.
Automatic Certificate Renewal:
Let’s Encrypt certificates are valid for 90 days, but Certbot automatically configures your system to renew them. You can test the renewal process by running:
sudo certbot renew --dry-run
Restart apache
sudo systemctl restart apache2
Check the example.com-le-ssl.conf file
Certbot should have created the file example.com-le-ssl.conf
in the folder /etc/apache2/sites-available/example.com-le-ssl.conf
. Lets check it has been configured correctly. Again, remember to change example.com
with your actual domain.
Open the file:
sudo nano /etc/apache2/sites-available/example.com-le-ssl.conf
The file might contain something already. If its empty, just paste in the following, if it isn’t empty, remove the content and paste in the following:
<VirtualHost *:443>
ServerAdmin admin@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
The final thing we need to do, is to go back to the file /etc/apache2/sites-available/example.com.conf
, and add a redirect command.
This will redirect all attempts to access http://example.com
to https://example.com
.
Open the file:
sudo nano /etc/apache2/sites-available/example.com.conf
Add the following highlighted line:
<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
Redirect permanent / https://example.com/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Conclusion
You’ve now successfully set up Apache Virtual Hosts on Ubuntu! This configuration allows you to host multiple websites or domains efficiently from a single server. Whether you’re managing different projects, testing environments, or live domains, Apache Virtual Hosts provides a flexible and scalable solution. Remember to periodically update and maintain your server to keep everything running smoothly.