{"id":16127,"date":"2026-09-30T15:32:08","date_gmt":"2026-09-30T14:32:08","guid":{"rendered":"https:\/\/da-manager.com\/blog\/how-to-configure-apache-virtual-hosts\/"},"modified":"2026-09-30T15:32:08","modified_gmt":"2026-09-30T14:32:08","slug":"how-to-configure-apache-virtual-hosts","status":"publish","type":"post","link":"https:\/\/da-manager.com\/blog\/how-to-configure-apache-virtual-hosts\/","title":{"rendered":"How to Configure Apache Virtual Hosts"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_87 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/da-manager.com\/blog\/how-to-configure-apache-virtual-hosts\/#How_to_Configure_Apache_Virtual_Hosts\" >How to Configure Apache Virtual Hosts<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/da-manager.com\/blog\/how-to-configure-apache-virtual-hosts\/#What_Are_Apache_Virtual_Hosts\" >What Are Apache Virtual Hosts?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/da-manager.com\/blog\/how-to-configure-apache-virtual-hosts\/#Name-Based_Virtual_Hosts\" >Name-Based Virtual Hosts<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/da-manager.com\/blog\/how-to-configure-apache-virtual-hosts\/#IP-Based_Virtual_Hosts\" >IP-Based Virtual Hosts<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/da-manager.com\/blog\/how-to-configure-apache-virtual-hosts\/#Prerequisites_Before_You_Begin\" >Prerequisites Before You Begin<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/da-manager.com\/blog\/how-to-configure-apache-virtual-hosts\/#Step-by-Step_Guide_to_Configuring_Apache_Virtual_Hosts\" >Step-by-Step Guide to Configuring Apache Virtual Hosts<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/da-manager.com\/blog\/how-to-configure-apache-virtual-hosts\/#Step_1_Create_the_Directory_Structure\" >Step 1: Create the Directory Structure<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/da-manager.com\/blog\/how-to-configure-apache-virtual-hosts\/#Step_2_Create_Sample_HTML_Pages\" >Step 2: Create Sample HTML Pages<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/da-manager.com\/blog\/how-to-configure-apache-virtual-hosts\/#Step_3_Create_Virtual_Host_Configuration_Files\" >Step 3: Create Virtual Host Configuration Files<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/da-manager.com\/blog\/how-to-configure-apache-virtual-hosts\/#Step_4_Enable_the_Virtual_Hosts\" >Step 4: Enable the Virtual Hosts<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/da-manager.com\/blog\/how-to-configure-apache-virtual-hosts\/#Step_5_Test_and_Reload_Apache\" >Step 5: Test and Reload Apache<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/da-manager.com\/blog\/how-to-configure-apache-virtual-hosts\/#Configuring_Apache_Virtual_Hosts_on_Red_Hat-Based_Systems\" >Configuring Apache Virtual Hosts on Red Hat-Based Systems<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/da-manager.com\/blog\/how-to-configure-apache-virtual-hosts\/#Enabling_SSL_for_Apache_Virtual_Hosts\" >Enabling SSL for Apache Virtual Hosts<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/da-manager.com\/blog\/how-to-configure-apache-virtual-hosts\/#Troubleshooting_Common_Issues\" >Troubleshooting Common Issues<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/da-manager.com\/blog\/how-to-configure-apache-virtual-hosts\/#Apache_Not_Serving_the_Correct_Site\" >Apache Not Serving the Correct Site<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/da-manager.com\/blog\/how-to-configure-apache-virtual-hosts\/#Permission_Denied_Errors\" >Permission Denied Errors<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/da-manager.com\/blog\/how-to-configure-apache-virtual-hosts\/#Port_80_or_443_Not_Listening\" >Port 80 or 443 Not Listening<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/da-manager.com\/blog\/how-to-configure-apache-virtual-hosts\/#Final_Thoughts\" >Final Thoughts<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<p><html><br \/>\n<head><br \/>\n<title>How to Configure Apache Virtual Hosts<\/title><br \/>\n<\/head><br \/>\n<body><\/p>\n<h1><span class=\"ez-toc-section\" id=\"How_to_Configure_Apache_Virtual_Hosts\"><\/span>How to Configure Apache Virtual Hosts<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>If you are running a web server and need to host multiple websites on a single machine, Apache virtual hosts are the solution you have been looking for. Whether you are a seasoned system administrator or a developer just getting started with server management, understanding how to configure Apache virtual hosts is an essential skill. In this guide, we will walk you through everything you need to know, from the basic concepts to the practical steps involved in setting up and managing virtual hosts on your Apache web server.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"What_Are_Apache_Virtual_Hosts\"><\/span>What Are Apache Virtual Hosts?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Apache virtual hosts allow a single Apache HTTP Server installation to serve multiple websites or web applications simultaneously. Instead of requiring a separate server for each domain, virtual hosts let you partition a single server into multiple logical servers, each responding to different domain names or IP addresses.<\/p>\n<p>There are two primary types of Apache virtual hosts:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Name-Based_Virtual_Hosts\"><\/span>Name-Based Virtual Hosts<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Name-based virtual hosting is the most commonly used method. It allows multiple domain names to share the same IP address. When a request arrives at the server, Apache examines the <code>Host<\/code> header sent by the browser to determine which virtual host should handle the request. This approach is cost-effective and straightforward, making it the preferred choice for most hosting environments.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"IP-Based_Virtual_Hosts\"><\/span>IP-Based Virtual Hosts<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>IP-based virtual hosting assigns a unique IP address to each website hosted on the server. Apache then routes incoming requests to the appropriate virtual host based on the IP address. This method is less common today due to the limited availability of IPv4 addresses, but it may still be required for certain SSL\/TLS configurations or legacy applications.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Prerequisites_Before_You_Begin\"><\/span>Prerequisites Before You Begin<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Before configuring Apache virtual hosts, ensure the following requirements are met:<\/p>\n<ul>\n<li>Apache HTTP Server is installed on your system (version 2.4 or later is recommended)<\/li>\n<li>You have root or sudo access to the server<\/li>\n<li>Your domain names are pointed to your server&#8217;s IP address via DNS records<\/li>\n<li>A basic understanding of the Linux command line<\/li>\n<\/ul>\n<p>If you are new to server management and looking for additional resources, the <a href=\"https:\/\/da-manager.com\/blog\">da-manager.com blog<\/a> offers a wealth of practical guides and tutorials on web hosting and server administration.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Step-by-Step_Guide_to_Configuring_Apache_Virtual_Hosts\"><\/span>Step-by-Step Guide to Configuring Apache Virtual Hosts<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Step_1_Create_the_Directory_Structure\"><\/span>Step 1: Create the Directory Structure<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The first step is to create a directory for each website you intend to host. A common convention is to store website files under <code>\/var\/www\/<\/code>. For example, if you are hosting two sites \u2014 <em>example.co.uk<\/em> and <em>anothersite.co.uk<\/em> \u2014 you would create the following directories:<\/p>\n<pre>\nsudo mkdir -p \/var\/www\/example.co.uk\/public_html\nsudo mkdir -p \/var\/www\/anothersite.co.uk\/public_html\n<\/pre>\n<p>Next, set the appropriate ownership and permissions so that Apache can read the files:<\/p>\n<pre>\nsudo chown -R www-data:www-data \/var\/www\/example.co.uk\/public_html\nsudo chown -R www-data:www-data \/var\/www\/anothersite.co.uk\/public_html\nsudo chmod -R 755 \/var\/www\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Step_2_Create_Sample_HTML_Pages\"><\/span>Step 2: Create Sample HTML Pages<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Create a simple index page for each site to test that the configuration is working correctly:<\/p>\n<pre>\necho \"&lt;h1&gt;Welcome to Example.co.uk&lt;\/h1&gt;\" | sudo tee \/var\/www\/example.co.uk\/public_html\/index.html\necho \"&lt;h1&gt;Welcome to AnotherSite.co.uk&lt;\/h1&gt;\" | sudo tee \/var\/www\/anothersite.co.uk\/public_html\/index.html\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Step_3_Create_Virtual_Host_Configuration_Files\"><\/span>Step 3: Create Virtual Host Configuration Files<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>On Debian and Ubuntu-based systems, Apache virtual host configuration files are stored in <code>\/etc\/apache2\/sites-available\/<\/code>. Create a new configuration file for each domain:<\/p>\n<pre>\nsudo nano \/etc\/apache2\/sites-available\/example.co.uk.conf\n<\/pre>\n<p>Add the following configuration to the file:<\/p>\n<pre>\n&lt;VirtualHost *:80&gt;\n    ServerAdmin webmaster@example.co.uk\n    ServerName example.co.uk\n    ServerAlias www.example.co.uk\n    DocumentRoot \/var\/www\/example.co.uk\/public_html\n    ErrorLog ${APACHE_LOG_DIR}\/example.co.uk_error.log\n    CustomLog ${APACHE_LOG_DIR}\/example.co.uk_access.log combined\n&lt;\/VirtualHost&gt;\n<\/pre>\n<p>Repeat this process for your second domain, adjusting the values accordingly.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Step_4_Enable_the_Virtual_Hosts\"><\/span>Step 4: Enable the Virtual Hosts<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Once your configuration files are in place, use the <code>a2ensite<\/code> command to enable them:<\/p>\n<pre>\nsudo a2ensite example.co.uk.conf\nsudo a2ensite anothersite.co.uk.conf\n<\/pre>\n<p>If the default Apache configuration is active and you no longer need it, you can disable it with:<\/p>\n<pre>\nsudo a2dissite 000-default.conf\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Step_5_Test_and_Reload_Apache\"><\/span>Step 5: Test and Reload Apache<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Before reloading Apache, always test your configuration for syntax errors:<\/p>\n<pre>\nsudo apache2ctl configtest\n<\/pre>\n<p>If the output shows <strong>Syntax OK<\/strong>, you can safely reload Apache:<\/p>\n<pre>\nsudo systemctl reload apache2\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Configuring_Apache_Virtual_Hosts_on_Red_Hat-Based_Systems\"><\/span>Configuring Apache Virtual Hosts on Red Hat-Based Systems<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>On CentOS, AlmaLinux, or Red Hat Enterprise Linux, the process differs slightly. Configuration files are typically placed in <code>\/etc\/httpd\/conf.d\/<\/code>, and the service is managed using <code>httpd<\/code> rather than <code>apache2<\/code>. The virtual host block syntax remains the same, but you would reload the service with:<\/p>\n<pre>\nsudo systemctl reload httpd\n<\/pre>\n<p>Additionally, if SELinux is enabled on your system, you may need to adjust the security context of your web directories to allow Apache to serve the files correctly.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Enabling_SSL_for_Apache_Virtual_Hosts\"><\/span>Enabling SSL for Apache Virtual Hosts<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Securing your virtual hosts with SSL\/TLS is strongly recommended. The easiest way to achieve this on most Linux distributions is by using Let&#8217;s Encrypt with the Certbot tool. Once installed, you can obtain and configure a free SSL certificate with a single command:<\/p>\n<pre>\nsudo certbot --apache -d example.co.uk -d www.example.co.uk\n<\/pre>\n<p>Certbot will automatically modify your virtual host configuration to include the necessary SSL directives and redirect HTTP traffic to HTTPS. This ensures that all data transmitted between your server and visitors is encrypted and secure.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Troubleshooting_Common_Issues\"><\/span>Troubleshooting Common Issues<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Apache_Not_Serving_the_Correct_Site\"><\/span>Apache Not Serving the Correct Site<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>If Apache is serving the wrong site, double-check the <code>ServerName<\/code> and <code>ServerAlias<\/code> directives in your configuration files. Ensure that your DNS records are correctly pointing to your server&#8217;s IP address, and verify that no conflicting virtual host configurations exist.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Permission_Denied_Errors\"><\/span>Permission Denied Errors<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Permission errors are often caused by incorrect file ownership or permissions. Revisit your <code>chown<\/code> and <code>chmod<\/code> settings and ensure that the <code>www-data<\/code> (or <code>apache<\/code> on Red Hat systems) user has read access to your document root directories.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Port_80_or_443_Not_Listening\"><\/span>Port 80 or 443 Not Listening<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Ensure that Apache is configured to listen on the correct ports by checking the <code>\/etc\/apache2\/ports.conf<\/code> file. You should see <code>Listen 80<\/code> and, if SSL is enabled, <code>Listen 443<\/code>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Final_Thoughts\"><\/span>Final Thoughts<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Configuring Apache virtual hosts is a fundamental aspect of managing a web server efficiently. By hosting multiple websites on a single machine, you can make the most of your server resources whilst keeping your infrastructure simple and cost-effective. Whether you are setting up a development environment, managing a small business website, or running a large-scale hosting platform, mastering Apache virtual hosts will serve you well throughout your career in web administration.<\/p>\n<p>With the steps outlined in this guide, you should now have a solid foundation for creating, enabling, and troubleshooting Apache virtual hosts. As you grow more confident, you can explore advanced configurations such as custom logging, access control, URL rewriting, and load balancing to further enhance your server setup.<\/p>\n<p><\/body><br \/>\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Configure Apache Virtual Hosts<\/p>\n<p>How to Configure Apache Virtual Hosts<\/p>\n<p>If you are running a web server and need to host multiple websites on a single machine, Apache virtual hosts are the solution you have been looking for. Whether you are a seasoned system administrator or a developer ju<\/p>\n","protected":false},"author":16,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mbp_gutenberg_autopost":false,"footnotes":""},"categories":[147],"tags":[],"class_list":["post-16127","post","type-post","status-publish","format-standard","category-general"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/da-manager.com\/blog\/wp-json\/wp\/v2\/posts\/16127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/da-manager.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/da-manager.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/da-manager.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/da-manager.com\/blog\/wp-json\/wp\/v2\/comments?post=16127"}],"version-history":[{"count":0,"href":"https:\/\/da-manager.com\/blog\/wp-json\/wp\/v2\/posts\/16127\/revisions"}],"wp:attachment":[{"href":"https:\/\/da-manager.com\/blog\/wp-json\/wp\/v2\/media?parent=16127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/da-manager.com\/blog\/wp-json\/wp\/v2\/categories?post=16127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/da-manager.com\/blog\/wp-json\/wp\/v2\/tags?post=16127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}