DEV Community

Cover image for Configuring a Subdomain in Apache2
Antonello Zanini for Writech

Posted on • Edited on

Configuring a Subdomain in Apache2

In this tutorial, we will see how to configure a subdomain, specifically a third-level domain, on a Debian-based Linux server with Apache2. We're going to use Virtual Hosts, thanks to which you can have multiple subdomains and top-level domains on the same machine, i.e. on one IP.

The term Virtual Host refers to the practice of running more than one web site (such as company1.example.com and company2.example.com) on a single machine. — Apache documentation

Apache breaks its functionality and components into individual units that can be configured independently. Essentially, a Virtual Host is a basic unit that can be an individual domain or site.

Now we're going to look at how to configure the following third-level domain: company2.example.com. Please note that the goal of this tutorial isn't to show how to properly install and configure Apache2, rather it assumes that you know how to do this already.

Moving Into Apache2 sites-available Folder

First of all, we need to move into Apache2 sites-available folder. This is can be achieved with the following command:

cd /etc/apache2/sites-available/
Enter fullscreen mode Exit fullscreen mode

Note that the /etc/apache2/sites-available directory contains configuration files for Virtual Hosts.

Creating a Configuration File

You can either create a new configuration from scratch or copy an existing one as the basis for a second site. If you want to follow the first approach, create a file called company2.example.com.conf with the following content:

<VirtualHost *:80>
DocumentRoot /var/www/company2/
ServerName [company2.example.com](<http://company2.example.com/>)
<Directory /var/www/company2/>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/company2-error.log
CustomLog ${APACHE_LOG_DIR}/company2-access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} = [company2.example.com](<http://company2.example.com/>)
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Otherwise, with the second approach, you can make a copy of an existing configuration file (e.g. company1.example.com.conf ) with this command:

cp company1.example.com.conf company2.example.com.conf
Enter fullscreen mode Exit fullscreen mode

Then, by replacing "company1" with "company2" you should obtain the aforementioned configuration file.

Please note that we've defined /var/www/company2/ as DocumentRoot. This is the directory from which Apache will read the contents that the visitor will access over the browser. Make sure that the folder exists, your web server has the required permissions to serve content, and that your user can create content within it.

The following two goals can be achieved by executing this command:

sudo chmod -R 755 /var/www/company2
Enter fullscreen mode Exit fullscreen mode

Activating the New Subdomain

We've just created the required configuration file. Now, it's time to enable it. Apache includes some tools, among which a2ensitecan be used to enable our subdomain. To do so, use the following command:

a2ensite company2.example.com
Enter fullscreen mode Exit fullscreen mode

It's time to make Apache reload its configuration files, which now include our new one. Run this command:

sudo service apache2 restart
Enter fullscreen mode Exit fullscreen mode

Et voila! We've just configured a third-level domain!

Place an index.html file in /var/www/company2/ and you should be able to see it from http://company2.example.com.

Configuring HTTPS

This step is not mandatory, but you might want to secure your subdomain with HTTPS.

In order to do so, we are going to use Cerbot.

Certbot is a fully-featured, extensible client for the Let's Encrypt CA (or any other CA that speaks the ACME protocol) that can automate the tasks of obtaining certificates and configuring webservers to use them. This client runs on Unix-based operating systems. — Certbot documentation

First of all, we need to install it.

Then, run the following command:

certbot run
Enter fullscreen mode Exit fullscreen mode

Select the company2.example.com option. Now, a message like this should appear on your console:

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
-------------------------------------------------------------------------------
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Redirecting vhost in /etc/apache2/sites-enabled/company2.example.com.conf to ssl vhost in /etc/apache2/sites-available/company2.example.com-le-ssl.conf

-------------------------------------------------------------------------------
Congratulations! You have successfully enabled https://company2.example.com
Enter fullscreen mode Exit fullscreen mode

Answer "2" to make all requests redirect to HTTPS.

Et voilà! Your website is now accessible from HTTPS.

Conclusion

In this article, we looked at how to configure a subdomain on a Debian-based server with Apache2. Although I'm not a system administrator, knowing how to do it can be very useful and something a software engineer should be able to do. Thanks for reading! I hope that you found this article helpful.


The post "Configuring a Subdomain in Apache2" appeared first on Writech.

Top comments (0)