Apache Virtual Host Example
This document attempts to answer the commonly-asked questions about setting up virtual hosts. These scenarios are those involving multiple web sites running on a single server, via name-based or IP-based virtual hosts.
apache virtual host example
Creating virtual host configurations on your Apache server does not magically cause DNS entries to be created for those host names. You must have the names in DNS, resolving to your IP address, or nobody else will be able to see your web site. You can put entries in your hosts file for local testing, but that will work only from the machine with those hosts entries.
The asterisks match all addresses, so the main server serves no requests. 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 .
The above configuration is what you will want to use in almost all name-based virtual hosting situations. The only thing that this configuration will not work for, in fact, is when you are serving different content based on differing IP addresses or ports.
The server machine has two IP addresses (172.20.30.40 and 172.20.30.50) which resolve to the names www.example.com and www.example.org respectively. In each case, we want to run hosts on ports 80 and 8080.
The following example allows a front-end machine to proxy a virtual host through to a server running on another machine. In the example, a virtual host of the same name is configured on a machine at 192.168.111.2. The ProxyPreserveHost On directive is used so that the desired hostname is passed through, in case we are proxying multiple hostnames to a single machine.
The name-based vhost with the hostname www.example.org (from our name-based example, setup 2) should get its own IP address. To avoid problems with name servers or proxies who cached the old IP address for the name-based vhost we want to provide both variants during a migration phase.
We have a server with two name-based vhosts. In order to match the correct virtual host a client must send the correct Host: header. Old HTTP/1.0 clients do not send such a header and Apache has no clue what vhost the client tried to reach (and serves the request from the primary vhost). To provide as much backward compatibility as possible we create a primary vhost which returns a single page containing links with an URL prefix to the name-based virtual hosts.
RabbitMQ is multi-tenant system: connections, exchanges, queues, bindings, user permissions,policies and some other things belong to virtual hosts, logical groups ofentities. If you are familiar with virtual hosts in Apacheor server blocks in Nginx, the idea is similar.There is, however, one important difference: virtual hosts in Apache are definedin the configuration file; that's not the case with RabbitMQ: virtual hosts arecreated and deleted using rabbitmqctl or HTTP API instead.
For example, resource permissions in RabbitMQ arescoped per virtual host. A user doesn't have global permissions, onlypermissions in one or more virtual hosts. User tags can be consideredglobal permissions but they are an exception to the rule.
A virtual host has a name. When an AMQP 0-9-1 client connects toRabbitMQ, it specifies a vhost name to connect to. If authenticationsucceeds and the username provided was granted permissions to thevhost, connection is established.
Connections to a vhost can only operate on exchanges, queues, bindings, and so on inthat vhost. "Interconnection" of e.g. a queue and an exchange in different vhosts is only possiblewhen an application connects to two vhosts at the same time. For example, anapplication can consume from one vhost then republishes into the other. This scenariocan involve vhosts in different clusters or the same cluster (or a single node).RabbitMQ Shovel plugin is one example of such application.
A newly created vhost will have a default set of exchangesbut no other entities and no user permissions. For a user to be able to connectand use the virtual host, permissions to it must be granted to every user that will use the vhost,e.g. using rabbitmqctl set_permissions.
Virtual host creation involves a blocking cluster-wide transaction. Each node has to performa number of setup steps which are moderately expensive. In practice it can take up to a few secondsfor a virtual host to be created.
When a number of virtual hosts is created in a loop, CLI and HTTP API clients can outpace the actualrate of virtual host creation and experience timeouts. If that's the case operation timeout should be increasedand delays should be introduced between operations.
Unlike AMQP 0-9-1 and STOMP, MQTT doesn't have the concept of virtualhosts. MQTT connections use a single RabbitMQ host by default. Thereare MQTT-specific convention and features that make it possible forclients to connect to a specific vhosts without any client librarymodifications. See the MQTT guide for details.
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.
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:
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.
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.
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 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.
In this section, you will configure the Apache virtual hosts file so that a visitor to example1.com will be served the content in /var/www/html/example1.com/public_html and the MySQL database example1_wordpress. Visitors to example2.com will be served content in /var/www/html/example2.com/public_html/ and its corresponding MySQL database.
Create a virtual hosts configuration file for example1.com and add the example virtual host block into /etc/apache2/sites-available/example1.com. Be sure to replace all instances of example1.com with your own domain.
If your site needs to be accessible through both secure (https) and non-secure (http) connections, you need a virtual host for each type of connection. Make a copy of the existing non-secure virtual host and configure it for SSL as described in step 4.
A virtual host is, in fact, a unit that describes an individual domain in the grand scheme of the Apache web server. It enables you to host multiple websites using one server which is very useful for people that have more than one website.
Most webmasters today use WordPress as a platform to host their website. A WordPress site runs with a database so if you plan to use this article to create virtual hosts for a WordPress website/s (or any platform that needs a database for that matter), you will need to install MySQL and PHP on your server which complete a so-called LAMP (Linux Apache, MariaDB & PHP) stack. For more information, you can check our article on how to install LAMP on CentOS 7.
Apache has a global configuration file where all the default settings are stored and applied to the server. The virtual host directive can be stored in that same default httpd.conf file or another one which will be respective to the configured website/domain.
One thing that is worth mentioning (even though most users know it) is that all the above configuration settings need to be properly setup before or after the Apache config file creation. For example, the DocumentRoot needs to be created and data hosted in it.
Follow the procedure in this section if the web server should provide the same content for all domains associated with the server. If you want to provide different content for different domains, set up name-based virtual hosts. For details, see Configuring Apache name-based virtual hosts.
The procedure in this section describes setting up a virtual host for both the example.com and example.net domain with separate document root directories. Both virtual hosts serve static HTML content.
Apache uses the first virtual host found in the configuration also for requests that do not match any domain set in the ServerName and ServerAlias parameters. This also includes requests sent to the IP address of the server.
For any Cloud Server with Plesk, virtual hosts should always be added and managed through the Plesk interface. See our article Add and Manage Virtual Hosts on a Plesk Server for step-by-step instructions. 041b061a72