Jul 26, 2018 With name-based virtual hosting, you can host multiple websites/domains on the same IP address. Virtual hosting can be useful if you want to host multiple websites and domains from a single physical server or VPS. Hope you got the basic idea of Apache virtual hosts. Today, we are going to see how to configure Apache virtual hosts in Ubuntu 18. Due to the fact that the virtual host with ServerName www.example.com is first in the configuration file, it has the highest priority and can be seen as the default or primary server. That means that if a request is received that does not match one of the specified ServerName directives, it will be served by this first. In Apache (httpd) virtual hosts are used to host web content for multiple domains off of the same server depending on the IP address or domain name that is being used. Depending on the request received different virtual host configuration can apply, resulting in different settings and web content being served from a single web server.
Serve multiple domains by using virtual hosts
Authored by: Rackspace Support
Most people serve more than one domain on their cloud server. Whether you areserving different domains or different subdomains of the same domain, the procedure is the same. This article describes how to create virtual hosts to serve multiple domains and how to troubleshoot problems with Apache name-based virtual host configurations.
Creating virtual hosts for multiple domains

When a browser sends a request to your server’s IP address asking for the contents of your domain name (for example, https://yourexampledomain.com
), your web server serves up an HTTP representation of your site. If the server is serving only one website, it serves the HTML in your /var/www/html
directory, starting with index.html
. But having a uniqueserver for every website that you want to serve is costly and an inefficient use of your resources.
Name-based virtual hosts enable you to serve content for multiple websites from one server.
One of the first lines in any virtual host configuration file specifies the domain name thatis associated with the virtual host. Following is an example virtual host configuration for Apache, serving domain1.com
:
The following example shows a virtual host configuration for NGINX:
Each configuration starts slightly differently, but the same principle applies: that particular virtual host responds to queries for domain1.com
and www.domain1.com
.
To serve different content for different domains, you add another virtual host.
For example, you have a subdomain called blog.domain1.com that is serving a blog.
First you create a folder in your public_html folder with the relevant files for the blog (for example, a WordPress installation).
Then you create a virtual host with the server_name
or ServerName
specified as blog.domain1.com
and configure it to point to the blog files and folders in your public_html
folder.
For more information on virtual hosts, use the Apache document Name-base Virtual Hosts
Troubleshooting
This section shows you how to troubleshoot problems with Apache name-based virtual host configurations. It provides useful commands for testing your virtual host configuration, describes how to interpret their output, and describes how they help fix common virtual host configuration problems.
Restart Apache
Before you can diagnose an issue, ensure that you have restarted Apache since the last time you made changes to your Apache configuration files:
For Red Hat distributions use:
For Debian distributions use:
If Apache gives you a warning or error message, note it for later. Your next step is to get information about the virtual host configuration.
Get a configuration report
Run the -S
command on the web server to check your virtual host configuration
For Red Hat derived distributions use:
For Debian derived distributions use:
The output shows the virtual host settings from the configuration file. The following example shows the configuration report for a server configured with two name-based virtual hosts: vh1.example.com and vh2.example.com. The numbered lines are explained following the example.
Line [1] reports that the web server is listening on the default port of 80 for all the IP addresses that Apache is listening to, and that name-based virtual hosting is turned on. The * is a wildcard specifying all IP addresses. Canon mx310 software download for mac.
Line [2] reports the default virtual host that the web server ServerAlias for any requests for which no specific hostname is requested. It also shows the path to the configuration file and line number where this configuration is set.
Line [3] reports the port and the name of the first virtual host configuration found, the file it is configured in and the line number its configuration starts on.
Line [4] reports the port and the name of the second virtual host configuration found, the file it is configured in and the line number its configuration starts on.
Line [5] reports whether the configuration syntax is correct, although that doesn’t necessarily mean your site is working
The following output was produced by following virtual host file configuration:
Now that that you’ve seen a basic virtual host configuration looks and how it maps to Apache’s own configuration report, you can use those reports to look at common configuration issues. The following sections describe some of these issues and provide guidance for how to fix them.
Hosts not set up as name-based virtual hosts
If running httpd -S
reports the following warning:
This warning indicates that multiple virtual hosts are trying to use the same “socket” without being set up as name-based virtual hosts. This error often occurs when Apache virtual hosts are first created because the default NameVirtualHost
directive is commented out with a hash symbol. That symbol instructs Apache to ignore the directive.
To fix this issue in a default Apache configuration file, verify that the NameVirtualHost *:80
directive is not commented out. If you are working with a minimal Apache configuration file, add a NameVirtualHost *:80
directive above the individual virtual host configurations.
The following example shows the commented directive that caused the error:

Element missing from VirtualHost directive
If running httpd -S
reports the following error message:
This message means that the virtual host’s VirtualHost
directive is missing a necessary element. The VirtualHost
directive is the first line of any individual virtual host configuration. In this case, the error is on the line 8 of the configuration file /etc/httpd/conf/custom/virtualhost.conf
.
Following is the Apache configuration that produced this above error:
Note that the second VirtualHost
directive has no IP address or port specified, which is the cause of the error.
Following is a corrected version of the preceding example, with the addition of *:80
to the virtual host’s directive. As always, the *
is a wildcard specifying all IP addresses.
Port numbers don’t match
If running httpd -S
, shows that a virtual host is listed above the is a NameVirtualHost
line:
In this example, the configuration test reports the vh2.example.com
configuration before it reports the NameVirtualHost
configuration. You might see this error if the VirtualHost IP address or port doesn’t match the IP address or port of the web server’s NameVirtualHost
directive. In this example, the report shows that vh2.example.com
uses port 800 rather than port 80. The port number was mistyped when the vh2.example.com
virtual host’s listening port was configured. As a result, Apache treats vh2.example.com
as a separate port-based virtual host.
The httpd -S
test command doesn’t warn you about this issue because it’s permissible to configure virtual hosts to use any port, such as 800, without them being part of the name-based virtual host configuration on the same server.
If you do experience this error, you will probably see content from the default virtual host (vh1.example.com
in this example) when you try to view the site in your web browser.
To help you map the preceding output to its configuration file, following is the virtual host configuration that created this error:
Document root directory does not exist
If running httpd -S
reports the following error:
This error indicates that the directory specified as containing the website files for the vh2.example.com
virtual host does not exist, or that Apache cannot access it. Similar errors can appear for any of the file paths specified in a virtual host configuration, such as the paths to the virtual host’s log files.
To fix this error, ensure that you created the directory. If you did create it, verify that there are no mistakes in the DocumentRoot
directive. A common mistake is to omit the path’s initial slash (/). Leaving out the slash instructs Apache to read the path—the DocumentRoot
path in this case—as a relative path, that is, as a path relative to the main Apache configuration’s ServerRoot
path.
The following example shows just one of the ways in which this error is created. The path for the DocumentRoot
directive in the first virtual host starts with a slash but the second one doesn’t.
Using cURL to test your site
After you check the virtual host configuration files and the httpd -S
command reports no issues, try to access your site by using cURL:
The output should look something like this:
The first line shows the status code. You want to see 200 OK
, as shown in the example. If that’s what you see, test the web server with your browser, but consider that your browser might display a cached page.
If you don’t see 200 OK
, you might see one of the following common messages:
curl: (6) Couldn't resolve host vh1.example.com
If cURL reports that it can’t find the host, verify that there is an A record for thedomain that points to the correct IP address for your server. You can use the dig to do this:
curl: (7) couldn't connect to host
Verify that your Apache configuration files include the necessary
Listen
directives and that they are not commented out. It needsListen 80
at the very least.Another way to verify this is to check the error log. The default error log is at
/var/log/httpd/error_log
on Red Hat systems and/var/log/apache2/error_log
on Debian systems. If no port is specified for Apache to listen on, the message no listening sockets available, shutting down follows Apache’s attempt to restart.HTTP/1.1 403 Forbidden
This response indicates that the permissions that allow Apache access to the page that you’re requesting are not correct. Perhaps the directory permissions are incorrect, or it could be the page itself.
You might also see a 403 response in the following situations:
The Apache error logs usually show which directory or file has the permissions set incorrectly. Individual virtual hosts might write errors to their own logs if they were configured to, so check these logs too.
Don’t be discouraged by the amount of data in a busy server’s log files. Instead, use the tail command to selectively view just the most recent ten lines of a log. For example:
You can see new entries as they are added to the error log, or any log, while you test the server if you instruct the tail
command to “follow” the log. For example:
Common permissions-related errors
Following are examples of some common permissions-related configuration errors that can appear in Apache’s logs:
The following log entry shows that permissions on the
index.html
file forvh2.example.com
are denying access to Apache.The following log entry shows that permissions on the
/var/www/vhosts/vh2
directory are blocking Apache’s read request.The following log entry shows that Apache does not have execute or readpermissions on one of the directories above
DocumentRoot
.
©2020 Rackspace US, Inc.
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License
Table of Contents
Introduction
Virtual hosting is a method for hosting multiple websites on a single machine. There are two types of virtual hosting: Name-based virtual hosting and IP-based virtual hosting.IP-based virtual hosting is a technique to apply different directives based on the IP address and port a request is received on. You can assign a separate IP for each website on a single server using IP-based virtual hosting. This is mainly used to host different websites on different ports or IP addresses.
In this article we will be creating:
Requirements
- A server running CentOS v. 7 with Apache installed
- A desktop machine running Linux
- A static IP address for each site you want to host.
Set up multiple IP addresses on a single network interface
To set up IP-based virtual hosting, you need to have more than one IP address assigned to your server. Setting up multiple IP addresses on a single network interface is called 'IP aliasing.' It is very useful, particularly if your server only has one network interface card (NIC).
To set up multiple IPs, you need to edit the ifcfg-eth0
file:
Add/edit the following lines:
Save and close the file when you are finished. Then restart the network service to make these changes take effect.
Set up multiple instances of Apache
By default Apache listens for incoming connections on port 80
. For port-based virtual hosting, you need to tell Apache to listen for IP address 192.168.1.42 on port 80
and for IP address 192.168.1.43 on port 8080
.
To set up multiple ports, you need to edit the httpd.conf
file:
Add/edit the following lines:
Save and close the file, then restart Apache to make these changes take effect.
Create the directory structure
First, you need to make a directory structure which will hold the web pages. This directory is known as 'document root' for the domain.
In CentOS 7 the default Apache document root directory is /var/www/html/
.
Now, create two directory for websites www.ip-vhost.com
and www.port-vhost.com
in the default Apache document root directory:
Create test web pages for each virtual host
Now, you need to create an index.html
file for each website which will identify that specific domain.
Let's create an index.html
file for the www.ip-vhost.com
ip virtual host.
Virtual Host Download
Add the following content.
Save and close the file when you are finished.
Similarly, create an index.html
file for the www.port-vhost.com
virtual host.
Add the following content.
Save and close this file as well. Now, you have the pages to test the virtual host configuration.
Set up ownership and permissions
In CentOS 7 by default the Apache service runs as the user apache
. You must change the ownership of these two virtual directories to apache
, so that Apache can read and write data.
You can change the ownership with chown
command.
You should also make the Apache document root /var/www/html
directory world readable, so that everyone can read files from that directory.
Now your web server has the permissions it needs to serve content.
Create virtual host files
The next step is to create a virtual host configuration file for each website. The name of each configuration file must end with .conf
.

Let's create a virtual host file for website www.ip-vhost.com
.
Add the following content.
Save and close the file when you are finished.
Free Virtual Host
Similarly, create a virtual host file for website www.port-vhost.com
.
Add the following content.
When you are finished, it is a good idea to check the syntax of the configuration. You can check the syntax of files with the following command:
After the syntax check is done, restart Apache to make these changes take effect.
Allow Apache through the firewall
Now, you need to allow the Apache port 80
and 8080
using FirewallD.
You can do this by running following commands:
Now, reload the firewall service for the changes to take effect.
Test the virtual hosts
Now on the desktop Linux computer, open your web browser and go to the URLs http://192.168.1.42:80
and http://192.168.1.43:8080
.You should see sample pages that look like this:
www.ip-vhost.com
sample page:
Download exness metatrader 4 for mac. www.port-vhost.com
demo page:
