Question

How do you properly set up a subdomain

Posted February 17, 2021 562 views
Node.js

I have a domain set up, and a website that runs properly on it currently.

I want to add a subdomain which will run a Node.js server to server as an API.

Here are the things I tried so far:

  • Added an A record with the subdomain
    • Is it normal that it points to the same IP?
    • I have read that I need to ensure that I have a www.domain.com (fictitious name) CNAME record for subdomains to work, is that correct?
  • Copied my files to a directory (e.g. /srv/api.domain.com)
  • Tried to generate a certbot, but failed miserably.

If anyone could provide me with the exact steps needed to set this up, I’d appreciate. I have wasted three days on this so far…

I am not a backend developer or a dev ops person, so please do not use unclear terms, or expect any previous knowledge.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
2 answers

Hi @jansensan,

Let’s start with the things you mentioned first.

  • Added an A record with the subdomain
    • Is it normal that it points to the same IP? - Yes that is perfectly okay to do so! That’s how it’s usually done. Many domains can point to one IP without causing any issues
    • I have read that I need to ensure that I have a www.domain.com (fictitious name) CNAME record for subdomains to work, is that correct? - It’s not actually a MUST, you can add an A record as www.subdomain.domain.com and point it to the same IP
  • Copied my files to a directory (e.g. /srv/api.domain.com) - That’s also okay, as long as you make your WebService - Nginx look up in that folder.
  • Tried to generate a certbot, but failed miserably. - In order for this to work, you’ll need to create a configuration file using a WebService like Nginx, I’ll explain in a bit!

Okay, now that your questions are answered, let’s get into how you can make your application work!

Usually, NodeJS applications are using ports like 8081, 8080 and so on. However what happens if you want to use more than one application on the same droplet? Well, you need to install a service that creates proxy requests. By default when you open a website, it loads on port 80 for http and 443 for https. The Nginx service can take these requests and by the use of a configuration file redirect the request to the port your application is running on.

In that regard, If you don’t have Nginx installed, that would be the first thing to do. If you are unsure how to do so, DigitalOcean has a pretty good tutorial about this:

https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04

Once you have Nginx installed you’ll need to setup your configuration files to work as a reverse proxy. Again, DigitalOcean has a pretty good tutorial on that front as well:

https://www.digitalocean.com/community/tutorials/how-to-configure-jenkins-with-ssl-using-an-nginx-reverse-proxy-on-ubuntu-20-04#step-1-%E2%80%94-configuring-nginx

Yes, it’s for Jenkins but you can replace the bits where it’s mentioned Jenkins with your Application.

Regards,
KFSys

by Erin Glass
Nginx is one of the most popular web servers in the world and is responsible for hosting some of the largest and highest-traffic sites on the internet. In this guide, we'll discuss how to get Nginx installed on your Ubuntu 20.04 server.

First, thanks for the elaborate answer.

There were quite a few snags here:

  • I already have an NGINX server running with a site that has a certificate, so that part I don’t need to reinstall
  • The few articles (those you shared, others to which they link) provide so many different configs the set for the /etc/nginx/sites-available, and I don’t know what combination to use. I tried so many, and even DigitalOcean’s generator (which creates a super long config file with WAY too many options that I don’t understand)

I tried to make sense of the Jenkins article you shared (what’s a proxy? what’s a reverse proxy?), there are too many terms that are out of my domain, that I honestly give up. I will try and hire someone to fix this, it will be less expensive than me trying to figure the magical config this expects.

Thanks for the time you took, and your respectful tone. 🙏