Hosting .NET Web API on Raspberry Pi - Part 2

In the previous part we covered configuration of Nginx as a reverse proxy on Raspberry Pi. This part will cover packaging the .NET API application and running the application as a background service. Linux Deployment (using Kestrel and NGINX) For Linux, there is no .exe file. Instead, you will be copying the .NET application binaries (such as .dll files) and using Kestrel/Nginx to serve the app. a. Publish Your .NET Application To publish your .NET Web API to Linux, run the following command: bash dotnet publish --configuration Release --output ./publish --runtime linux-arm64 b. Copy Files to Your Raspberry Pi Server /var directory is used to store content and log files by Nginx. Copy the published Files to appropriate directory. bash sudo cp -a ~/yourapp/bin/Debug/net5.0/publish/ /var/www/yourapp/ use sudo before the copy command, standard users don't have write permission to /var directory. Therefore, you must run the command as a superuser. To run your application from a published folder, run the following command: dotnet /var/www/yourapp/yourapp.dll If you want, you can run tests by using the curl and wget commands. This application will listen on port 5000 for HTTP requests. c. Ensure permissions Ensure that the application folder has the correct permissions for your web server or user to execute the application: bash sudo chmod -R 755 /var/www/yourapp d. Create Service File For production, you would want to run your application as a service using systemd or another process manager to keep it running in the background. This will restart the services even if it fails due to unknown issues. Here's an example of how you might create a systemd service file for your .NET application: Create a file like /etc/systemd/system/yourapp.service: ini [Unit] Description=Your .NET Web API After=network.target [Service] WorkingDirectory=/var/www/yourapp ExecStart=/usr/bin/dotnet /var/www/yourapp/yourapp.dll Restart=always User=www-data Environment=ASPNETCORE_ENVIRONMENT=Production Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false [Install] WantedBy=multi-user.target After creating this service, enable and start it with: bash sudo systemctl enable yourapp sudo systemctl start yourapp using journalctlgives detailed messages and can be helpful in trouble shooting. sudo journalctl -fu yourapp.service Once your application is running, NGINX (as described previously) will forward incoming requests to your .NET Web API application. Summary Location of Application Files: On Linux, choose a standard location for your application files, such as /var/www/yourapp (or any other appropriate directory). Process Management: For a production environment on Linux, you should always run your .NET application as a service so it will automatically start when the server boots and will be restarted if it crashes. Use systemd, supervisord, or forever (for Node.js apps) to manage the application processes. Security: Ensure the appropriate file permissions and user ownership for the application folder. For production applications, run the application as a non-root user, and configure your firewall to allow traffic only on the necessary ports (e.g., 80 and 443 for HTTP and HTTPS).

Apr 9, 2025 - 10:54
 0
Hosting .NET Web API on Raspberry Pi - Part 2

In the previous part we covered configuration of Nginx as a reverse proxy on Raspberry Pi.

This part will cover packaging the .NET API application and running the application as a background service.

Linux Deployment (using Kestrel and NGINX)

For Linux, there is no .exe file. Instead, you will be copying the .NET application binaries (such as .dll files) and using Kestrel/Nginx to serve the app.

a. Publish Your .NET Application

To publish your .NET Web API to Linux, run the following command:

bash

dotnet publish --configuration Release --output ./publish --runtime linux-arm64

b. Copy Files to Your Raspberry Pi Server

/var directory is used to store content and log files by Nginx. Copy the published Files to appropriate directory.

bash

sudo cp -a ~/yourapp/bin/Debug/net5.0/publish/ /var/www/yourapp/

use sudo before the copy command, standard users don't have write permission to /var directory. Therefore, you must run the command as a superuser.

To run your application from a published folder, run the following command:

dotnet /var/www/yourapp/yourapp.dll

If you want, you can run tests by using the curl and wget commands.
This application will listen on port 5000 for HTTP requests.

c. Ensure permissions

Ensure that the application folder has the correct permissions for your web server or user to execute the application:

bash
sudo chmod -R 755 /var/www/yourapp

d. Create Service File

For production, you would want to run your application as a service using systemd or another process manager to keep it running in the background. This will restart the services even if it fails due to unknown issues.

Here's an example of how you might create a systemd service file for your .NET application:

Create a file like /etc/systemd/system/yourapp.service:

ini
[Unit]
Description=Your .NET Web API
After=network.target

[Service]
WorkingDirectory=/var/www/yourapp
ExecStart=/usr/bin/dotnet /var/www/yourapp/yourapp.dll
Restart=always
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

After creating this service, enable and start it with:

bash
sudo systemctl enable yourapp
sudo systemctl start yourapp

using journalctlgives detailed messages and can be helpful in trouble shooting.

sudo journalctl -fu yourapp.service

Once your application is running, NGINX (as described previously) will forward incoming requests to your .NET Web API application.

Summary

  1. Location of Application Files: On Linux, choose a standard location for your application files, such as /var/www/yourapp (or any other appropriate directory).
  2. Process Management: For a production environment on Linux, you should always run your .NET application as a service so it will automatically start when the server boots and will be restarted if it crashes. Use systemd, supervisord, or forever (for Node.js apps) to manage the application processes.
  3. Security: Ensure the appropriate file permissions and user ownership for the application folder. For production applications, run the application as a non-root user, and configure your firewall to allow traffic only on the necessary ports (e.g., 80 and 443 for HTTP and HTTPS).