Http Server Virtual Host
Virtual hosting is the practice of serving multiple websites from a single web server. One way to differentiate sites is by using the apparent hostname of the request instead of just the path name part of the URI. Virtual hosts are the configuration files that inform the webserver to route traffic based on the domain name and other parameters and conditions.
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.
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.
Virtual Host Apache
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:
Virtual Hosting Server
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:
Virtual Hosting
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
Apache Server is one of the most famous web servers. This server is open-source and works on various platforms on the Internet, powering many HTTP servers. Apache is a flexible tool and contains within it various other tools that extend its features and usage.Install Apache Web Server
In this article, I will show you how to install the Apache web server on Ubuntu 20.04. This requires root or administrative privileges, so log in to the system via root.
Step 1: Upgrade Your APT
As always, first, update and upgrade your APT.
Step 2: Download and Install Apache
Next, download and install the Apache web server from the Ubuntu software repository by executing the following terminal command.
Step 3: Verify Apache Installation
To verify whether Apache has installed, check the server status. When the installation is complete, the apache2 server will start automatically.
Step 4: Enable Firewall Settings
Now, you should enable the firewall settings for the Apache webserver. To do this, use the UFW command to allow Apache traffic on Port 443 and Port 80 via the following terminal command.
Step 5: Verify Changes
You can verify this change by checking the firewall status with the following terminal command.
Step 6: Check If Apache Is Working
Check to see whether Apache is working correctly on your system. Open a web browser on your Ubuntu machine, open a new tab, and type the following URL into the URL bar. Be sure to replace the IP we have used with your own machine’s IP address.
Figure: Apache service running in a browser window.
Step 7: Set Up Virtual Host
Now, you are ready to set up a virtual host using the installed Apache web server. Apache contains a test virtual host that is enabled by default when it is installed. Hosting a single website is easy; you just have to upload the website content in its configuration file under the “/var/www/html.” The path to this configuration file is defined as follows.
Figure: Default configuration file opened in gedit editor.
Step 8: Create Domain Name
If you are hosting multiple websites, use the following command to create a new virtual host configuration file for every new website. The domain name created in this test example is www.example.com; this can be replaced with your desired domain name.
Step 9: Create New Directory File
Create a new file in the directory named “index.html” and paste the following content into this file.
<htmllang='en'dir='ltr'>
<head>
<metacharset='utf-8'>
<title>Welcome to example.com</title>
</head>
<body>
<h1>Success! example.com home page!</h1>
</body>
</html>
Figure: New index.html file with site content inside.
Save the file and close it. Change the file permission options with the following terminal command.
Step 10: Create Document in Text Editor
Open your favorite text editor and create this file in “/etc/apache2/sites-available” location. I am using the gedit text editor.
ServerName example.com
ServerAlias www.example.com
ServerAdmin [email protected]
DocumentRoot /var/www/example.com/public_html
<Directory /var/www/example.com/public_html>
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
Figure: example.conf file opened with gedit editor.
Step 11: Link Configuration File
Link this configuration file to the a2ensite utility by executing the following terminal command.
To check for a syntax error in the configuration file, execute the following command.
Host Virtual Machine
Step 12: Restart Apache
Now restart Apache service, open your web browser, and type the following URL into the URL bar.
Figure: example.com opened in a browser window.
Virtual Host Example
Uninstalling Apache Web Server
You can completely uninstall the Apache web server via the following terminal commands.
Conclusion
This article covered how to install the Apache web server, configure firewall settings for Apache, set up virtual hosts on Apache web server, and uninstall Apache.