Lead Image © Saniphoto, Fotolia.com

Lead Image © Saniphoto, Fotolia.com

Set up subdomains with Apache and Nginx

Sublet

Article from ADMIN 62/2021
By
Set up virtual hosts on modern web servers for Apache and Nginx.

The shortage of addresses and the quest for efficient resource usage established web server virtualization well before the topic of virtual machines and containers gained its current prominence. In this article, I demonstrate how to use different fully qualified domain names (FQDNs) on the same physical system with virtual hosts.

Virtual Hosts

In the mid-1990s, a single web server (usually the Apache server, which quickly became the de facto standard on Linux) typically ran with a single IP address. Once installed and configured, it reliably delivered a web page – always the same page – after requests to that IP address.

Not only was it impractical to have a separate computer for each web server, no matter how small, but IPv4 addresses were becoming scarce. Virtualized hosts didn't exist yet, so in version 1.1 the Apache developers conjured up the strategy of virtual hosts as a solution. Suddenly it didn't matter how many FQDNs pointed to the same IP address: The web server took care of the correct distribution of requests.

That's why the web browser doesn't just contact the plain vanilla IP address that it gets from the domain name server (DNS) after resolving the FQDN, it additionally specifies the desired host name directly after the GET request with a HOST specification (e.g., over Telnet on port 80, as in Listing 1).

Listing 1

Virtual Host Output

$ telnet www.wintermeyer.de 80
Trying 89.221.14.204...
Connected to www.wintermeyer.de.
Escape character is '^]'.
GET / HTTP/1.1
HOST: www.wintermeyer.de
HTTP/1.1 301 Moved Permanently
Server: nginx/1.10.3
Date: Fri, 27 Nov 2020 14:00:25 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: https://www.wintermeyer.de/
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>

In this example, the web server responded with a 301 Moved Permanently HTTP response, which means that the resource no longer exists and the new URL should be used because the server no longer delivers the web page over HTTP, but only has an SSL-encrypted version over HTTPS. To make life as easy as possible for end users and search engines, the server operator then defines a 301 redirect. In this case, the browser tries a second request to the new URL. For temporary rather than permanent redirection, you need to use HTTP status code 302.

With SSL encryption, you have no way around this functionality, even if you have the luxury of a dedicated IP address for your web server.

Apache vs. Nginx

Even though Apache was one of the pioneers, if not the decisive pioneer for stable and fast web servers on Linux, Nginx has a slightly higher market share today. As of January 2021, it accounts for 33.3 percent of all web servers worldwide, compared with 26.4 percent for Apache [1]. As you can see, both web servers are very popular in the community, so in the following examples, I look at the configuration for both Nginx (with Server blocks) and Apache (with VirtualHost blocks).

The starting point will be a freshly installed Debian 10.6 "Stable," on which I configure virtual hosts for the imaginary domains www.example1.de and www.example2.de . The packages from the distribution's repository will come into play. The IP address for the server is 192.168.2.120.

Apache

The first task is to install the web server. In Apache's case, you can use:

# apt-get update
# apt-get -y install apache2

To check that the web server is installed and running, enter:

systemctl status apache2

The default website can be retrieved by pointing a web browser to http://192.168.2.120 . The website returned is located in the /var/www/html/index.html file.

For each of the two new web pages, you create a separate directory:

# mkdir /var/www/www.example1.de
# mkdir /var/www/www.example2.de

In each of the two directories, store an index.html with the content from Listing 2, each with an appropriately customized domain name. Then, set the permissions to rwx-rx-rx

Listing 2

index.html

<html>
  <head>
    <title>Test for www.example1.de</title>
  </head>
<body>
  <h1>Test for www.example1.de</h1>
</body>
</html>
# chmod -R 755 /var/www/www.example{1,2}.de

for these directories.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy ADMIN Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs



Support Our Work

ADMIN content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.

Learn More”>
	</a>

<hr>		    
			</div>
		    		</div>

		<div class=