DEV Community

Maxwell ADANZOUNNON
Maxwell ADANZOUNNON

Posted on • Updated on

Deploying .net core, angular and sql server to ubuntu

In this article I will share with you how I deploy NGX.BILL a .net core/SQL Server and angular application to ubuntu using nginx

Prerequisites: You must have an Ubuntu 16.04, 18.04, or 20.04 machine with at least 2 GB of memory.

Create your own private server and choose ubuntu 16.04, 18.04, or 20.04
You can use this link to get a free credit with digital ocean to start

Connect to the same server as the database using an ssh client.
Example of ssh client: Putty

Create a new user

adduser USER
Enter fullscreen mode Exit fullscreen mode
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Enter fullscreen mode Exit fullscreen mode
Changing the user information for USER
Enter the new value, or press ENTER for the default
        Full Name []:
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [Y/n] y
Enter fullscreen mode Exit fullscreen mode

Give admin rights to the user.

usermod -a -G sudo USER
Enter fullscreen mode Exit fullscreen mode

Copy the ssh key the new user.

cp -r ~/.ssh /home/USER/
sudo chown -R USER:USER /home/USER/.ssh
Enter fullscreen mode Exit fullscreen mode

Restart the SSH service

sudo service ssh restart
Enter fullscreen mode Exit fullscreen mode

Exit the server.

exit
Enter fullscreen mode Exit fullscreen mode

Login again as the new user

/ssh USER@YOUR_IP_ADDRESS
Enter fullscreen mode Exit fullscreen mode

Type "Enter" for the passphrase

Enter passphrase for key 'C:\Users\USER/.ssh/id_rsa':
Enter fullscreen mode Exit fullscreen mode

Install SQL Server

Import the public repository GPG keys.

wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
Enter fullscreen mode Exit fullscreen mode

Register the Microsoft SQL Server Ubuntu repository for SQL Server 2019.
For Ubuntu 16.04:

sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-2019.list)"
Enter fullscreen mode Exit fullscreen mode

For Ubuntu 18.04:

sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/18.04/mssql-server-2019.list)"
Enter fullscreen mode Exit fullscreen mode

For Ubuntu 20.04:

sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/20.04/mssql-server-2019.list)"
Enter fullscreen mode Exit fullscreen mode

Install SQL Server

sudo apt-get update
sudo apt-get install -y mssql-server
Enter fullscreen mode Exit fullscreen mode

After the package installation finishes, run mssql-conf setup and follow the prompts to set the SA password and choose your edition.

sudo /opt/mssql/bin/mssql-conf setup
Enter fullscreen mode Exit fullscreen mode

Make sure to specify a strong password for the SA account and keep this information somewhere for later

Once the configuration is done, verify that the service is running:

systemctl status mssql-server --no-pager
Enter fullscreen mode Exit fullscreen mode

Install the SQL Server command-line tools

Use the following steps to install SQL Server command-line tools (mssql-tools)

sudo apt-get update 
sudo apt install curl
Enter fullscreen mode Exit fullscreen mode

Import the public repository GPG keys.

curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
Enter fullscreen mode Exit fullscreen mode

For Ubuntu 16.04:

curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list
Enter fullscreen mode Exit fullscreen mode

For Ubuntu 18.04:

curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list
Enter fullscreen mode Exit fullscreen mode

For Ubuntu 20.04:

curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list
Enter fullscreen mode Exit fullscreen mode

Update the sources list and run the installation command with the unixODBC developer package.

sudo apt-get update sudo apt-get install mssql-tools unixodbc-dev
Enter fullscreen mode Exit fullscreen mode

Optional: Add /opt/mssql-tools/bin/ to your PATH environment variable in a bash shell.
To make sqlcmd/bcp accessible from the bash shell for login sessions, modify your PATH in the ~/.bash_profile file with the following command:

echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
Enter fullscreen mode Exit fullscreen mode

To make sqlcmd/bcp accessible from the bash shell for interactive/non-login sessions, modify the PATH in the ~/.bashrc file with the following command:

echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Connect to the SQL Server instance

The following steps use sqlcmd to locally connect to your new SQL Server instance.

Run sqlcmd with parameters for your SQL Server name (-S), the user name (-U), and the password (-P). In this tutorial, you are connecting locally, so the server name is localhost. The user name is SA and the password is the one you provided for the SA account during setup.

sqlcmd -S localhost -U SA -P '<YourPassword>'
Enter fullscreen mode Exit fullscreen mode

To exit:

QUIT
Enter fullscreen mode Exit fullscreen mode

Tip: You can omit the password on the command line to be prompted to enter it.

Tip: If you later decide to connect remotely, specify the machine name or IP address for the -S parameter, and make sure port 1433 is open on your firewall

Install .net core

Install the Microsoft package sources

wget https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
Enter fullscreen mode Exit fullscreen mode

Install the .NET 3.1 SDK: take into account your own version

sudo apt-get update
sudo apt-get install -y apt-transport-https
sudo apt-get update
sudo apt-get install -y dotnet-sdk-3.1
Enter fullscreen mode Exit fullscreen mode

Check that dotnet is correctly installed

dotnet --info
Enter fullscreen mode Exit fullscreen mode

Upload the release

Install dotnet core 3.1 in your local machine: Download .net core 3.1

Make the connection string in your appsettings.json (take into account your own project) to update the database in order to create the database with tables and default data. Use the IP Address of the server because early with set it up to host the database.

"LocalConnectionString":"Server=YOUR_IP_ADDRESS;Database=database;Uid=user;Pwd=your_password"
Enter fullscreen mode Exit fullscreen mode

Open a command prompt in the api root folder and navigate to your api project root

If you have a entity framework migrations execute them. this will create database, generate tables and default data inside the database.

dotnet ef database update 
Enter fullscreen mode Exit fullscreen mode

After the migration update success, update again the connection string and replace only the IP Address with "localhost"

"LocalConnectionString":"Server=localhost;Database=database;Uid=user;Pwd=your_password"
Enter fullscreen mode Exit fullscreen mode

Run the command below to create the deployment release

dotnet publish --configuration Release
Enter fullscreen mode Exit fullscreen mode

You release will be at this path : your_dotnet_core_project\bin\Release\netcoreapp3.1\publish

In your_angular_project/src/environments/environment.prod.ts replace the value of with what will be your domain url

apiHost: 'http://yourdomain.com/api',
Enter fullscreen mode Exit fullscreen mode

Download an install in your local machine the latest node.js: Link
After nodejs installation, run the command below to install angular cli locally in your machine

npm install -g @angular/cli
Enter fullscreen mode Exit fullscreen mode

Navigate to the angular root project:

npm install
Enter fullscreen mode Exit fullscreen mode

Once the installation completed, In the same path, run the command below to generate the release

npm run prod
Enter fullscreen mode Exit fullscreen mode

This will generate the release at this path: you_angular_project\dist\your_angular_project\

Create /var/www/ directory in the server

sudo mkdir /var/www/
sudo chown -R <<user>>:www-data /var/www/
Enter fullscreen mode Exit fullscreen mode

Use an FTP File transfert client or download WinSCP: https://winscp.net/eng/download.php

Connect to the server using your IP Address and user credentials

Upload the content of your api your_dotnet_core_project\bin\Release\netcoreapp3.1\publish to the server: /var/www/ and then the content of the client the dist folder inside the wwwroot folder of your api on the server.

Install NGINX on the server

Install nginx

sudo apt-get update
sudo apt-get install nginx
Enter fullscreen mode Exit fullscreen mode

Start nginx service

sudo service nginx start
Enter fullscreen mode Exit fullscreen mode

Enter the IP address in your browser to see nginx welcome page
SSL Certificate for your domain name

sudo apt-get install software-properties-common
sudo add-apt-repository universe
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install certbot python3-certbot-nginx 
sudo certbot --nginx
Enter fullscreen mode Exit fullscreen mode

Edit the site config to forward request to the app

sudo nano /etc/nginx/sites-available/default
Enter fullscreen mode Exit fullscreen mode

Replace the old content with following content

server {
    server_name   _;
    location / {
       root /var/www/wwwroot;
       index index.html;
       #try_files $uri $uri/ /index.html;
       }
    location /api/{
       proxy_pass http://localhost:5000/api/;
       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;
       }
       error_log /var/www/error.log warn;
}
Enter fullscreen mode Exit fullscreen mode

Enable the site config

sudo ln -sf /etc/nginx/sites-available/default /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode

Run command below to edit nginx.conf

sudo nano /etc/nginx/nginx.conf
Enter fullscreen mode Exit fullscreen mode

Include site config to nginx.conf

http{    
    ...    
    include /etc/nginx/sites-available/default;
}
Enter fullscreen mode Exit fullscreen mode

Restart nginx

sudo service nginx restart
Enter fullscreen mode Exit fullscreen mode

On the server navigate to www folder

cd /var/www
Enter fullscreen mode Exit fullscreen mode

Run the project for test

dotnet your_project-api.dll
Enter fullscreen mode Exit fullscreen mode

Type in your browser your IP address to see the app and after type ctrl + c to stop the app running

The web app service

Create the service file

sudo nano /etc/systemd/system/app_name.service
Enter fullscreen mode Exit fullscreen mode

Fill the service infos

[Unit]
Description=Service description

[Service]
WorkingDirectory=/var/www
ExecStart=/usr/bin/dotnet /var/www/your_project_api.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=app_name
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

Gill full right to the folder

sudo chown -R www-data:www-data /var/www/
sudo setfacl -R -d -m u:www-data:rwx,g:www-data:rwx,o::r /var/www/
Enter fullscreen mode Exit fullscreen mode

Start the service

sudo service app_name start
Enter fullscreen mode Exit fullscreen mode

To check if service is running

sudo service app_name status
Enter fullscreen mode Exit fullscreen mode

Now the web app is running. No need to run the app with dotnet command to access it.

Hope this will help!

For more see: http://blog.twenty-ninex2.com/

Top comments (0)