This ASP.NET Core Web API allows one to store, edit and query a set of books and their reviews.
Data is saved to a LiteDb database, on file and provided via REST endpoints. Various statistics, pertaining to books, their reviews and the reading process are computed, provided sufficient entries are contributed.
The following steps describe how to deploy the APIs to a somewhat recent Linux machine (Ubuntu’s 18:04 in the author’s case).
-
Download and install Microsoft’s .NET Core packages
wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
-
update packages list
apt-get update
-
install https transport package
sudo apt-get install apt-transport-https
-
install ASP.NET Core runtimes
sudo apt-get install aspnetcore-runtime-3.1
NoteASP.NET Core runtimes are sufficient. There’s no need for the SDK unless one wants to build and publish the project from the server itself. -
create folder where to store .NET published files
mkdir /var/www/momus
-
build the project on the development machine, selecting the right target
Linux x64
dotnet publish --configuration Release
-
package, SCP or otherwise transfer the release files to
/var/www/momus
-
assign proper permissions when required by the web server
chown -R www-data /var/www/momus
-
create the file responsible for authentication settings, including the administrator’s password, and place it in the root folder
authsettings.json{ "Authentication": { "Secret": "choose a secret", "AdminPassword": "choose a password" } }
-
edit
nginx
configuration so it includes the following server block:/etc/nginx/sites-enabled/defaultserver { listen 80; server_name DOMAIN or IP ADDRESS; location / { proxy_pass http://localhost: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; } }
-
create a service file to run the web API automatically
sudo vim /etc/systemd/system/kestrel-momus.service
kestrel-momus.service[Unit] Description=Momus [Service] WorkingDirectory=/var/www/momus ExecStart=/usr/bin/dotnet /var/www/momus/momus-api.dll Restart=always RestartSec=10 KillSignal=SIGINT SyslogIdentifier=dotnet-example User=www-data Environment=ASPNETCORE_ENVIRONMENT=Development Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false [Install] WantedBy=multi-user.target
-
Enable the service
sudo systemctl enable kestrel-momus.service
-
Start the service
sudo systemctl start kestrel-momus.service
-
Verify it’s actually running
sudo systemctl status kestrel-momus.service