# 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](https://httpd.apache.org/). We're going to use [Virtual Hosts](https://httpd.apache.org/docs/2.4/vhosts/index.html), 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](https://httpd.apache.org/docs/2.4/vhosts/index.html)

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:

```shell
cd /etc/apache2/sites-available/
```

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

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

```shell
cp company1.example.com.conf company2.example.com.conf
```

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

Please note that we've defined `/var/www/company2/` as [`DocumentRoot`](https://httpd.apache.org/docs/2.4/urlmapping.html#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:

```shell
sudo chmod -R 755 /var/www/company2
```

## 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 [`a2ensite`](https://manpages.debian.org/jessie/apache2/a2ensite.8.en.html)can be used to enable our subdomain. To do so, use the following command:

```shell
a2ensite company2.example.com
```

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

```shell
sudo service apache2 restart
```

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](https://certbot.eff.org/).

> _**Certbot** is a fully-featured, extensible client for the Let's Encrypt CA (or any other CA that speaks the [ACME](https://github.com/ietf-wg-acme/acme/blob/master/draft-ietf-acme-acme.md) 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](https://certbot.eff.org/docs/intro.html)

First of all, we need to [install it](https://certbot.eff.org/docs/install.html).

Then, run the following command:

```shell
certbot run
```

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

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](https://dev.to/writech/configuring-a-subdomain-in-apache2-22p8/)" appeared first on [Writech](https://writech.run)._
