Set up subdomains with Apache and Nginx


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 in /etc/apache2/sites-available/www_example1_de.conf (Listing 4), and do the same for .

Listing 3

Configuration Directories

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

Listing 4

Minimal Configuration

<VirtualHost *:80>
  DocumentRoot /var/www/

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 to , 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


<VirtualHost *:80>
  Redirect permanent /
<VirtualHost *:80>
  DocumentRoot /var/www/
curl -I

This command outputs the headers and initially returns a 301 Moved Permanently status message stating the new location, . The command

curl -I

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 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 , 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

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.