VPS, DNS and Apache: how to create a subdomain for a new web service

You bought a VPS and you are now running your website. You should know that your websites is stored inside a public html folder on your VPS that is /var/www/public or something like this.

But now you want to add a service on your vps like a self-hosted git server or a cloud server to store your data in the cloud. For example you want to run Gogs but you don’t actually know how to setup a different folder on your vps in which store the service.

First of all you should setup a new A record in the DNS zone on your admin panel. For example if you are running on OVH, the DNS zone should be under “Web > Domains > ‘mydomain’ > DNS Zone”. Inside this zone, you have to create a new A record (“Add an entry” button) pointing your new subdomain (mynewservice.mydomain.com) to your IPV4 (or IPV6 if you want to setup an AAAA record).

Here is an example:

After a while, the new subdomain will be spreaded over the network and you will be able to access to it.

Now let’s continue to setup your new service. Next step is to install your service on your vps (if you didn’t already). If you would like to run a self hosts git service like Gogs, you could follow this guide. After the installation of the new service, you should have on your vps a public folder where the new service is running, for example /home/myuser/publicservice/, depending on the particular installation of the new application.

We have now to link the service on our vps to the entire world using the subdomain that we have previously created. To achieve this goal we need the help of the webserver (Apache, Nginx etc…).


We are now going to tell to Apache how the requests from our new subdomain should be redirected to the /path/to/new/service.

The Apache folder is usally at /etc/apache2, so get inside this folder:

$ cd /etc/apache2/sites-available

Then we have to create a new VirtualHost in this way:

$ sudo vim mynewservice.mydomain.com.conf

and copy (and edit) the following snippet inside vim:

<IfModule mod_ssl.c>
<VirtualHost *:443>
	ServerAdmin webmaster@localhost
	DocumentRoot /the/path/to/newservice/public/folder/
	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined
	ServerName mynewservice.mydomain.com
	ServerAlias mynewservice.mydomain.com
	#ProxyRequests Off
	#ProxyPass / http://127.0.0.1:port/
	#ProxyPassReverse / http://127.0.0.1:port/
	

	<Directory /path/to/newservice/public/folder>
	Options FollowSymLinks
	AllowOverride All
	Order allow,deny
	allow from all
	</Directory>
	
	</VirtualHost>
</IfModule>

Remember then to enable it and restart Apache, running:

$ sudo a2ensite mynewservice.mydomain.com.conf
$ sudo systemctl restart apache2

The a2ensite command enables a new VirtualHost creating a soft link inside the /etc/apache2/sites-enebled folder. Use instead the a2dissite command (more options here) if you want to disable a VirtualHost:

$ sudo a2dissite mynewservice.mydomain.com.conf

Last thing to do is to generate an SSL certificate (we used the port 443 with the mod_ssl in the previous configuration that allows Apache to speak through HTTPS) for this new subdomain or you will get an error page like this one:

A very easy way to get a certificate in order to obtain your green lock is to use Let’s Encrypt (a free, automated, and open Certificate Authority) and, in particular, use CertBot. So simply go here, choose Apache (or your web server) and the operating system of your vps and follow the instruction on how generate the certificate.

It’s not immediate if you are trying this for the first time but the second time will be very easy. The process will be the same:

  • Create a subdomain pointing to your ip address (or CNAME) in your DNS dashboard
  • Install the application you want
  • Create a VirtualHost redirecting the traffic to the new application public folder
  • Enable SSL on the new subdomain

I hope I helped you to deploy a new web application. Ask me for everything else.

Enjoy!

Be the first to comment

Leave a Reply

Your email address will not be published.


*