Prerequisites
Basic Linux knowledge
A Linux Server with Apache Installed
Root or sudo privileges
It's helpful to understand basic DNS concepts
You should be comfortable using a text editor like nano, vim, or vi to edit Apache configuration files
Introduction
If you're managing multiple websites or web applications on a single server, Apache Virtual Hosts are a game changer. With Virtual Hosts, you can configure Apache to serve different websites based on domain names, subdomains, or even IP addresses, all while using the same server. This powerful feature is essential for sysadmins looking to optimize server resources and streamline web hosting.
In this guide, we'll walk you through the process of setting up Apache Virtual Hosts on a Linux server. Whether you're new to Apache or looking to refine your server management skills, this step-by-step tutorial will provide you with the knowledge to host multiple sites efficiently. Let's dive in and master Apache Virtual Hosts!
Create Directories for Your Websites
Next, create directories for each of the websites you want to host. Each directory will contain the web files for the site.
For example, let's create two websites: example.com and anotherexample.com
sudo mkdir -p /var/www/example.com/public_html
sudo mkdir -p /var/www/anotherexample.com/public_html
Set the correct permissions for these directories:
sudo chown -R $USER:$USER /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/anotherexample.com/public_html
Create Index Pages for Each Website
Create a basic index.html page in each directory to verify the websites later.
echo "Welcome to Example.com!" | sudo tee /var/www/example.com/public_html/index.html
echo "Welcome to AnotherExample.com!" | sudo tee /var/www/anotherexample.com/public_html/index.html
Obtain SSL Certificates
Before you can enable HTTPS, you'll need SSL certificates for your domains. You can use Let's Encrypt for free SSL certificates or purchase them from a certificate authority (CA). For the sake of this example, we'll assume you already have the certificates.
Configure Apache Virtual Hosts
On Ubuntu/Debian
Create a Virtual Host Configuration File for Example.com
sudo vim /etc/apache2/sites-available/example.com.conf
Add the following content:
# Port 80 Virtual Host (HTTP) <VirtualHost *:80> ServerAdmin webmaster@example.com ServerName example.com DocumentRoot /var/www/example.com/public_html # Redirect all HTTP traffic to HTTPS Redirect permanent / https://example.com/ </VirtualHost> # Port 443 Virtual Host (HTTPS) <VirtualHost *:443> ServerAdmin webmaster@example.com ServerName example.com DocumentRoot /var/www/example.com/public_html # SSL Configuration SSLEngine on SSLCertificateFile /etc/ssl/certs/example.com.crt SSLCertificateKeyFile /etc/ssl/private/example.com.key SSLCertificateChainFile /etc/ssl/certs/example.com-chain.crt # Logging ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Create a Virtual Host Configuration File for AnotherExample.com
sudo vim /etc/apache2/sites-available/anotherexample.com.conf
# Port 80 Virtual Host (HTTP) <VirtualHost *:80> ServerAdmin webmaster@anotherexample.com ServerName anotherexample.com DocumentRoot /var/www/anotherexample.com/public_html # Redirect all HTTP traffic to HTTPS Redirect permanent / https://anotherexample.com/ </VirtualHost> # Port 443 Virtual Host (HTTPS) <VirtualHost *:443> ServerAdmin webmaster@anotherexample.com ServerName anotherexample.com DocumentRoot /var/www/anotherexample.com/public_html # SSL Configuration SSLEngine on SSLCertificateFile /etc/ssl/certs/anotherexample.com.crt SSLCertificateKeyFile /etc/ssl/private/anotherexample.com.key SSLCertificateChainFile /etc/ssl/certs/anotherexample.com-chain.crt # Logging ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
On CentOS/RHEL
Create a Virtual Host Configuration File for Example.com
sudo vim /etc/httpd/conf.d/example.com.conf
Add the following content:
# Port 80 Virtual Host (HTTP) <VirtualHost *:80> ServerAdmin webmaster@example.com ServerName example.com DocumentRoot /var/www/example.com/public_html # Redirect all HTTP traffic to HTTPS Redirect permanent / https://example.com/ </VirtualHost> # Port 443 Virtual Host (HTTPS) <VirtualHost *:443> ServerAdmin webmaster@example.com ServerName example.com DocumentRoot /var/www/example.com/public_html # SSL Configuration SSLEngine on SSLCertificateFile /etc/ssl/certs/example.com.crt SSLCertificateKeyFile /etc/ssl/private/example.com.key SSLCertificateChainFile /etc/ssl/certs/example.com-chain.crt # Logging ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Create a Virtual Host Configuration File for AnotherExample.com
sudo nano /etc/httpd/conf.d/anotherexample.com.conf
Add the following content:
# Port 80 Virtual Host (HTTP) <VirtualHost *:80> ServerAdmin webmaster@anotherexample.com ServerName anotherexample.com DocumentRoot /var/www/anotherexample.com/public_html # Redirect all HTTP traffic to HTTPS Redirect permanent / https://anotherexample.com/ </VirtualHost> # Port 443 Virtual Host (HTTPS) <VirtualHost *:443> ServerAdmin webmaster@anotherexample.com ServerName anotherexample.com DocumentRoot /var/www/anotherexample.com/public_html # SSL Configuration SSLEngine on SSLCertificateFile /etc/ssl/certs/anotherexample.com.crt SSLCertificateKeyFile /etc/ssl/private/anotherexample.com.key SSLCertificateChainFile /etc/ssl/certs/anotherexample.com-chain.crt # Logging ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Enable the Virtual Hosts (For Ubuntu/Debian)
After creating the virtual host files, you need to enable them using the a2ensite command.
sudo a2ensite example.com.conf
sudo a2ensite anotherexample.com.conf
Update the Hosts File (For Testing Locally)
If you're testing locally and don't have DNS set up, add entries to your /etc/hosts file to point the domains to your server's IP address (e.g., 127.0.0.1 ).
sudo vim /etc/hosts
Add the following lines:
127.0.0.1 example.com
127.0.0.1 anotherexample.com
Check for Syntax Errors
Apache provides a built-in command to check the configuration for syntax errors without starting the server
On Ubuntu/Debian
sudo apache2ctl configtest
On CentOS/RHEL
sudo apachectl configtest
Restart Apache
Restart Apache to apply the changes.
On Ubuntu/Debian
sudo systemctl restart apache2
On CentOS/RHEL
sudo systemctl restart httpd
Check and enable the necessary modules
Install mod_ssl (if not already installed)
This module provides SSL and TLS support for Apache. It's essential for enabling SSL on Apache.
sudo yum install mod_ssl # For CentOS/RHEL-based systems
sudo dnf install mod_ssl # Fedora, CentOS 8+, or RHEL 8+
sudo apt install libapache2-mod-ssl # On Ubuntu/Debian-based systems
Check if mod_ssl is already loaded
apachectl -M | grep ssl
If ssl_module is listed, it's successfully enabled.
Install mod_rewrite
This module allows you to rewrite URLs, which can be helpful for redirecting HTTP to HTTPS, for example. Most of the time, mod_rewrite is already enabled. It's not required to enable SSL, but many users install it to enforce security or SEO-friendly redirects.
Check if mod_rewrite is already loaded
apachectl -M | grep rewrite
If mod_rewrite is listed in the output, it is already enabled.
Verify the Virtual Hosts
To verify that everything is working, open your web browser and visit http://example.com and http://anotherexample.com. You should see the corresponding index page for each website.
Final Thoughts
Mastering Apache Virtual Hosts on Linux is essential for any web administrator or developer looking to manage multiple websites or applications efficiently. By understanding how to configure and manage virtual hosts, you can optimize your Apache server for scalability, security, and ease of maintenance. Whether you're running a simple personal blog or a complex enterprise web application, this knowledge equips you with the tools to streamline your server setup and ensure reliable performance. Keep experimenting with different configurations, and remember to review your server's security settings regularly. With these skills, you can confidently manage any Apache-powered server environment.