Set up subdomains with Apache and Nginx

Sublet

Configuring Apache

The configurations for the web servers available and enabled on the system are located in the directories shown in Listing 3. Now you need to create a minimal basic configuration for www.example1.de in /etc/apache2/sites-available/www_example1_de.conf (Listing 4), and do the same for www.example2.de .

Listing 3

Configuration Directories

# tree /etc/apache2/sites-*
/etc/apache2/sites-available
--- 000-default.conf
default-ssl.conf
/etc/apache2/sites-enabled
--- 000-default.conf -> ../sites-available/000-default.conf

Listing 4

Minimal Configuration

<VirtualHost *:80>
  ServerName www.example1.de
  DocumentRoot /var/www/www.example1.de/
</VirtualHost>

The two configurations are not yet live, however, so you either need to link the configuration files manually with /etc/apache2/sites-enabled/ or use the a2ensite tool:

# a2ensite www_example1_de.conf

The next step is to tell Apache to parse the new configuration and make the virtual hosts available:

# systemctl reload apache2

Again, you can use Telnet to test this, as in Listing 1.

To disable a virtual host, you could delete the corresponding link with rm; however, it is better to use the appropriate tool and parse the changed configuration:

# a2dissite www_example1_de.conf
# systemctl reload apache2

To make your work as easy as possible as an admin, you will want to use only one FQDN per configuration file, but feel free to use it for both HTTPS and HTTP. Technically you could put 100 different FQDNs in one file, but that would be far more confusing.

Redirects with Apache

One simple example of the use of virtual hosts for the same FQDN is redirecting all http://example1.de to http://www.example1.de , which might also work the other way around, depending on what the marketing department or in-house search engine optimization guru thinks is more fashionable at the moment. The file /etc/apache2/sites-available/www_example1_en.conf has to contain the code from Listing 5. To test, run:

Listing 5

Redirects

<VirtualHost *:80>
  ServerName example1.de
  Redirect permanent / http://www.example1.de/
</VirtualHost>
<VirtualHost *:80>
  ServerName www.example1.de
  DocumentRoot /var/www/www.example1.de/
</VirtualHost>
curl -I http://example1.de

This command outputs the headers and initially returns a 301 Moved Permanently status message stating the new location, http://www.example1.de . The command

curl -I http://www.example1.de

should then generate the output from Listing 6.

Listing 6

Accessing a Redirected Website

HTTP/1.1 200 OK
Date: Sat, 28 Nov 2020 08:25:05 GMT
Server: Apache/2.4.38 (Debian)
Last-Modified: Fri, 27 Nov 2020 17:20:15 GMT
ETag: "85-5b519dfafde34"
Accept-Ranges: bytes
Content-Length: 133
Vary: Accept-Encoding
Content-Type: text/html

Nginx

Nginx also needs to be set up according to the previous example,

apt-get -y install nginx

which you can check with systemctl as demonstrated earlier to make sure it is up and running. The default page is at http://192.168.2.120 , which resides in the /var/www/html/index.nginx-debian.html file.

For the two new Nginx web pages, you will again be creating separate directories, storing suitable index.html files, and setting appropriate permissions in each case, as demonstrated in the Apache example.

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=