Host Multiple Projects on Linux Server with Multiple Subdomains

Saransh Kumar
4 min readDec 30, 2023

Around 1 year back in November 2022, Heroku removed its free dynos plan and I believe that most of the students who were learning web development, were using Heroku to host their backend application because it was so much easier to host rather than spinning up a linux machine and setting up all the required dependencies and then running an application.

So at the time, all of my backend projects were hosted on that only but when they removed the free dynos plan I moved to AWS free tier to host my final year project and I successfully hosted that with custom Domain but issue was AWS pricing, since I was using Route53 with hosted zone so every month bills were generated. So I got an idea how about create a single machine with NGINX for routing request to the respective application based on subdomain. In this article I will be discussing how I host multiple projects on a single Linux server.

Most of my personal projects are Spring Boot projects, there are not many options in the market for hosting Spring applications as compared to NodeJS projects

1. Create a Linux Server

First we have to create a Linux server on which we will be running our application. I have chosen Linode because it offers a Shared CPU compute unit in $5 which is very cheap as compare to other platforms and one benefit of using Linode is that once we create a machine, it gets an IP address which does not change. So it is beneficial for us because we need to create DNS records.

You can choose other platform as well but make sure that IP address of the machine is static.

Install NGINX

Install NGINX using the following command:

sudo apt update; sudo apt install nginx

After installing NGINX, create configuration file which will be used by NGINX to route the request to the respective application.

Create a configuration file in /etc/nginx/sites-available and paste the following configuration:

server {
listen 80;
server_name subdomain.example.com www.subdomain.example.com; # Replace this with the original one
location / {
proxy_pass http://127.0.0.1:8080; # Use the port your Spring Boot app is running on
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}

Configuration file name should be according to your domain, for example: above I have used subdomain.example.com then its config file name should be subdomain.example.com according to the convension

You can also refer to this repository:

Now create the symbolic link and restart nginx

sudo ln -s /etc/nginx/sites-available/subdomain.example.com /etc/nginx/sites-enabled/subdomain.example.com; sudo systemctl restart nginx.service

If above restart command does not work then try this one:

sudo service nginx restart

Verify the configuration file

Run the following command to verify whether the configuration file is created correctly or not:

sudo nginx -t

It should give the output like this:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

2. Run the application locally on the server

You can clone the repository and create a shell script to build and run the application, if there are any environment variable required then add them in the script itself.

To automatically run the application when machine start, you can create Unit service. You can check out the below article for Unit service creation process:

3. Create DNS records

Go to the domain name provider’s dashboard and go to manage DNS records page.

  1. Create A type record which will point to the IP address of the server.
  2. Create CNAME type record which will point to the above A type record.
  3. Set the TTL value to 86,400 seconds because we will not be changing these DNS records.

Final result should look something like this:

As you can see I have created multiple subdomains for different projects but both of them points to the same server IP and in server also I have create two NGINX configuration files.

Just save these records and I will take some time to propogate the DNS records to different DNS servers.

If you like the article, Do clap 👏🏻👏🏻 and you can consider following me, share the article with your friends. Hopefully I will be creating more such articles in future with great content.

--

--