Skip to content

Instantly share code, notes, and snippets.

@bozhink
Last active September 13, 2020 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bozhink/ad03303c3e4bc7e228c1a18b60263e3d to your computer and use it in GitHub Desktop.
Save bozhink/ad03303c3e4bc7e228c1a18b60263e3d to your computer and use it in GitHub Desktop.
Install dotnet web application on Debian/Ubuntu-like Linux distribution

Install dotnet web application on Debian/Ubuntu-like Linux distribution

Fresh install of the OS

  1. Update the index of packages
$ sudo apt update
  1. Update (upgrade) installed packages
$ sudo apt upgrade
  1. Reboot system after package upgrade
$ sudo reboot

1. Installation of dotnet

  1. Download the package from https://dotnet.microsoft.com/download/dotnet-core/3.1 and get for example https://download.visualstudio.microsoft.com/download/pr/5ed60e45-f93a-4a8b-ab92-4034fcf00618/cf2aafe9bc91f28bd4d7b7436c31e27e/aspnetcore-runtime-3.1.7-linux-arm.tar.gz
  2. Create bin directory of the logged user (e.g. user pi)
$ mkdir ~/bin
  1. Go to the bin directory and restore the dotnet package
$ cd ~/bin # got to bin directory
$ tar xzf ~/Downloads/aspnetcore-runtime-3.1.7-linux-arm.tar.gz
  1. Check if dotnet is working
$ cd ~ # go to home directory
$ /home/pi/bin/dotnet --version # check if the executable file id OK

Here dotnet is OK but is not visible in the system PATH. When you want to invoke it you have to run the full-path to the executable.

Easier but less clean way is to restore the dotnet in the /usr/bin directory which is included in the system PATH, but there are many other important executable files and it is restricted to super-user

$ cd /usr/bin # go to the system directory
$ tar xzf /home/pi/Downloads/aspnetcore-runtime-3.1.7-linux-arm.tar.gz

Here the validation command is just

$ dotnet --version

2. Deploy the application

  1. Build and publish the dotnet wep application as Portable to a specified folder.
  2. Zip the published files.
  3. Move the .zip file to the destination server computer (via flash drive, sftp, scp, cloud storage shared link (e.g. Dropbox), etc.).
  4. Unzip the application to a specified folder on the server. Lets suppose that the folder is /home/pi/my_application. In this filter there have to be the main application assembly .dll file, i.e. there have to exist the file Application.dll.

3. Test if the application is correctly copied

  1. Go to the application's folder
$ cd /home/pi/my_application

This is required, because the configuration file appsettings.json is the same directory as the main assembly Application.dll 2. Run the application

$ /home/pi/bin/dotnet Application.dll

If the port for the Kestrel is set to 80 there would be some problems with the permissions for starting the endpoints, so super-user privileges are needed

$ sudo /home/pi/bin/dotnet Application.dll

Here everything should be OK.

Note

4. Open port in the firewall

  1. The easy way

    1. Install the Uncomplicated Firewall (UFW)
    $ sudo apt install ufw
    $ sudo apt install gufw # this is GUI for the UFW
    1. Enable the UFW
    $ sudo ufw enable
    1. Add port 80 to the list of the allowed ports
    $ sudo ufw allow 80/tcp
    1. Check the status (optional)
    $ sudo ufw status
  2. The not-so-easy way - iptables (no need of addition software)

    Read https://www.binarytides.com/open-http-port-iptables-centos/ and https://linux.die.net/man/8/iptables and good luck!

    In short

    $ sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
    $ sudo service iptables save # or sudo /etc/init.d/iptables save

5. Check with other device

Check of other device in the same network can reach the running web application by typing the IP address in the address bar of the browser.

6. Create service

  1. Create shell script file to be executed as service

    1. Open text editor and create plain text file for the script. For example /home/pi/bin/run-my-application.sh
    2. The content of the file has to be like
    #!/bin/bash
    cd /home/pi/my_application
    /home/pi/bin/dotnet Application.dll
    1. Make this script file executable
    $ chmod +x /home/pi/bin/run-my-application.sh
  2. Create service file

    1. Create empty file
    $ sudo touch /etc/systemd/system/my_application.service
    1. Edit the file
    $ sudo nano /etc/systemd/system/my_application.service
    1. Enter content like
    [Unit]
    Description=My Application Service
    
    [Service]
    ExecStart=sudo /home/pi/bin/run-my-application.sh
    Type=forking
    RemainAfterExit=yes
    Restart=always
    
    [Install]
    WantedBy=multi-user.target

    See https://askubuntu.com/questions/814/how-to-run-scripts-on-start-up

    See https://www.digitalocean.com/community/tutorials/how-to-configure-a-linux-service-to-start-automatically-after-a-crash-or-reboot-part-1-practical-examples

    See https://www.freedesktop.org/software/systemd/man/systemd.service.html

  3. Enable and start the service

    $ sudo systemctl daemon-reload
    $ sudo systemctl enable my_application.service
    $ sudo systemctl start my_application.service

7. Reboot

Reboot the system and the application should run as service.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment