Self-hosted gRPC applications

Run your app as a Linux service with systemd

To configure your ASP.NET Core application to run as a Linux service (or daemon in Linux parlance), install the Microsoft.Extensions.Hosting.Systemd package from NuGet. Then add a call to UseSystemd to the CreateHostBuilder method in Program.cs

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .UseSystemd() // Enable running as a Systemd service
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });
dotnet publish -c Release -r linux-x64 -o ./publish

/etc/systemd/system/myapp.service

[Unit]
Description=My gRPC Application

[Service]
Type=notify
ExecStart=/usr/sbin/myapp

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl status myapp
sudo systemctl start myapp.service
sudo systemctl enable myapp

References
https://docs.microsoft.com/en-us/dotnet/architecture/grpc-for-wcf-developers/self-hosted

Set executable file permission on Windows for using in Linux

with Windows Subsystem for Linux

git update-index --chmod=+x myfile.sh

If you frequently work with .sh files and want to ensure they are always marked as executable, you can configure Git to automatically handle this. For example, you can use a .gitattributes file to define rules for specific file types.

*.sh text eol=lf
*.sh executable

a .bat script for Windows that searches for all .sh files in a folder and its subfolders, and then runs the git update-index --chmod=+x command for each file:

@echo off
:: Get the current directory
set "ROOT_DIR=%cd%"

:: Navigate to the current directory
cd /d "%ROOT_DIR%"

:: Loop through all .sh files in the folder and subfolders
for /r %%f in (*.sh) do (
    echo Processing file: %%f

    :: Add the file to the Git index if it's not already tracked
    git add "%%f" 2>nul

    :: Mark the file as executable
    git update-index --chmod=+x "%%f"
)

echo All .sh files in the current directory and subfolders have been marked as executable.
pause

References
https://www.scivision.dev/git-windows-chmod-executable/

Disable swap on Ubuntu

disable swap only for a current session

sudo swapoff -a

Or 

sudo -s
crontab -e

And add

@reboot sudo swapoff -a  

Or ( best solution )

Open fstab file, type sudo nano /etc/fstab in terminal.

File’s contents would look like this:

proc            /proc           proc    nodev,noexec,nosuid 0       0
/host/ubuntu/disks/root.disk /               ext4    loop,errors=remount-ro 0       1
/host/ubuntu/disks/swap.disk none            swap    loop,sw         0       0
#/dev/sda10 /media/ASD  vfat    defaults    0   0
#/dev/sda1  /media/98   vfat    defaults    0   0

Just add hash (#) to the beginning of the swap partition line, so the line looks as:

#/host/ubuntu/disks/swap.disk none            swap    loop,sw         0       0

Reboot your PC

References
https://askubuntu.com/questions/214805/how-do-i-disable-swap

Convert m2ts to mp4, mp4 to webm, mp4 to ogv using ffmpeg

MP4 TO MP4 (MEDIUM)
ffmpeg -i input.mp4 -b 1000000 output.mp4

M2TS TO MP4
ffmpeg -i input.m2ts -vcodec libx264 -crf 20 -acodec ac3 -vf "yadif" output.mp4

MP4 TO WEBM (HIGH)
ffmpeg -i input.mp4 -aq 5 -ac 2 -qmax 25 -threads 2 output.webm

MP4 TO WEBM (MEDIUM)
ffmpeg -i input.mp4 -aq 5 -ac 2 -qmax 35 -threads 2 output.webm

MP4 TO OGV (HIGH)
ffmpeg -i input.mp4 -vcodec libtheora -acodec libvorbis -q:v 6 -q:a 5 output.ogv

MP4 TO OGV (MEDIUM)
ffmpeg -i input.mp4 -vcodec libtheora -acodec libvorbis -q:v 2 -q:a 4 output.ogv

References
https://gist.github.com/vielhuber/cf918eed2b5cc9eaa63f

Set Up WireGuard VPN on Ubuntu 18.04

Server Installation

wget https://raw.githubusercontent.com/complexorganizations/wireguard-install/master/wireguard-server.sh -P /etc/wireguard/
bash /etc/wireguard/wireguard-server.sh

Client Installation

wget https://raw.githubusercontent.com/complexorganizations/wireguard-install/master/wireguard-client.sh -P /etc/wireguard/
bash /etc/wireguard/wireguard-client.sh

Or

apt-get update
apt-get install software-properties-common -y
add-apt-repository ppa:wireguard/wireguard -y
apt-get update
apt-get install linux-headers-"$(uname -r)" -y
apt-get install wireguard qrencode haveged resolvconf -y

Run Client

# Install the config file to the WireGuard configuration directory on your
# Linux client:
sudo install -o root -g root -m 600 <username>.conf /etc/wireguard/wg0.conf

# Start the WireGuard VPN:
sudo systemctl start wg-quick@wg0

# Check that it started properly:
sudo systemctl status wg-quick@wg0

# Verify the connection to the AlgoVPN:
sudo wg

# See that your client is using the IP address of your AlgoVPN:
curl ipv4.icanhazip.com

# Optionally configure the connection to come up at boot time:
sudo systemctl enable wg-quick@wg0

References
https://github.com/complexorganizations/wireguard-install
https://trailofbits.github.io/algo/client-linux-wireguard.html

Deploy .NET Core app on Ubuntu

check if it runs

dotnet run

Deploy in a build directory

dotnet publish --output “ /var/www/build” --configuration release

Go to the build directory and run the application

dotnet ForExample.dll

Create the service file

sudo nano /etc/systemd/system/kestrel-ForExample-test.service
[Unit]
Description=Example .NET Web API App running on Ubuntu
[Service]
WorkingDirectory=/var/www/ForExample/ForExample
ExecStart=/usr/bin/dotnet /var/www/build/ForExample.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.t

Register the service

sudo systemctl enable kestrel-ForExample-test.service

Start the service and verify that it’s running.

sudo systemctl start kestrel-mebgispanel-test.service
sudo systemctl status kestrel-mebgispanel-test.service

Check the server logs

sudo journalctl -fu kestrel-helloapp.service

References
https://medium.com/faun/ubuntu-servers-and-asp-net-core-project-deployment-using-nginx-d9a3a1f6ac82