Deploy APIs on Nginx webserver in Ubuntu

Deploy OM_Dashboard and RM_Dashboard APIs on nginx (ec2: Ubuntu Server)

Steps:

  1. Update and upgrade the system:

sudo apt update && sudo apt upgrade -y
  1. Then install dot-net (required version). In my case, I am using 8.0.

wget https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
sudo apt-get update
sudo apt-get install -y dotnet-host
sudo apt-get install -y dotnet-sdk-8.0
sudo apt install -y aspnetcore-runtime-8.0
sudo apt install -y dotnet-runtime-8.0
sudo apt update 

In case you're getting conflict, you can also use:

sudo apt-get install apt-transport-https
sudo apt-get update 
sudo apt-get install -f -y dotnet-host 
sudo apt-get install -f -y dotnet-sdk-8.0
sudo apt install -f -y aspnetcore-runtime-8.0
sudo apt install -f -y dotnet-runtime-8.0
sudo apt update
sudo apt upgrade -y 
  1. Check the dot-net version and info:

dotnet --version
dotnet --info
  1. Get the file:

Method: git, wget, scp

  1. Now config and give the path to the OM_Dashboard zip file:

unzip OM_Dashboard.zip -d RM_Dashboard
sudo rm -rf OM_Dashboard.zip
sudo cp -r OM_Dashboard /var/www
cd /var/www/OM_Dashboard/OM_Dashboard
  1. Now config and give the path to the OM_Dashboard zip file:

unzip RM_Dashboard.zip -d RM_Dashboard
sudo rm -rf RM_Dashboard.zip
sudo cp -r RM_Dashboard /var/www
cd /var/www/RM_Dashboard/RM_Dashboard
  1. Both should have exe and dll. (Choose the dll with name of exe)

  2. Let's create the service to deploy OM_Dashboard api first.

cd /etc/systemd/system
sudo nano omdashboard.service
[Unit]
Description=ASP.NET Core Web App running on Ubuntu

[Service]
WorkingDirectory= /var/www/OM_Dashboard/OM_Dashboard
ExecStart=/usr/bin/dotnet /var/www/OM_Dashboard/OM_Dashboard/OM.Dashboards.API.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-web-app
# This user should exist on the server and have ownership of the deployment directory
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production

[Install]
WantedBy=multi-user.target

Now turn for RM_Dashboard

sudo nano rmdashboard.service
[Unit]
Description=ASP.NET Core Web App running on Ubuntu

[Service]
WorkingDirectory= /var/www/RM_Dashboard/RM_Dashboard
ExecStart=/usr/bin/dotnet /var/www/RM_Dashboard/RM_Dashboard/RM.Dashboards.API.dll --urls=http://0.0.0.0:5001
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-web-app
# This user should exist on the server and have ownership of the deployment directory
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production

[Install]
WantedBy=multi-user.target
  1. Check enable, start and check status of both services

sudo systemctl enable omdashboard.service
sudo systemctl enable rmdashboard.service
sudo systemctl start omdashboard.service
sudo systemctl start rmdashboard.service
sudo systemctl status omdashboard.service
sudo systemctl status rmdashboard.service
  1. Time to install nginx too:

sudo apt install nginx -y 
  1. Enable and check status of nginx service

sudo systemctl enable nginx # It might be enabled by default (Optional)
sudo systemctl start nginx # (Optional)
sudo systemctl status nginx # Checks the status of the service
  1. Let's config the nginx server:

cd /etc/nginx/sites-available
ls
sudo mv default default2 # (Backup purpose)
sudo nano default # new default config file

For new default file:

server {
    listen        80;
    server_name   localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location /omdashboard/ {
        proxy_pass         http://127.0.0.1:5000/;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }

    location /rmdashboard/ {
        proxy_pass         http://127.0.0.1:5001/;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }

}
  1. Now check the nginx file syntax/ status and reload nginx server:

sudo nginx -t 
sudo nginx -s reload
sudo systemctl restart nginx
  1. Now, your APIs are deployed successfully. 👏 🔗 http://65.2.39.232/omdashboard 🔗http://65.2.39.232/rmdashboard

Note: These sites aren't accessible now but you can try your hosted URL as per your given location.

Last updated