# Running ASP.NET Core 9 on a Raspberry Pi Zero 2W

This is a step by step for setting up a Raspberry Pi Zero 2W with a default ASP.NET Core 9 application (in 9 steps).

1. Install the [Raspberry Pi Imager](https://www.raspberrypi.com/software/) tool.
2. Install `Raspberry Pi OS Lite (64bit)` on the SD Card. Configure settings Wi-Fi/user/SSH settings during the installation.
3. SSH into the raspberry using `ssh <user>@raspberrypi.local` or `ssh <ip> -l <user>` command.
4. Install .NET runtime:
  - Follow documentation outlined by [dotnet iot](https://learn.microsoft.com/en-us/dotnet/iot/deployment). Here I am using the Framework-Dependent approach.
  - `curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin --channel STS`
  - Set PATH: `echo 'export DOTNET_ROOT=$HOME/.dotnet' >> ~/.bashrc` then `echo 'export PATH=$PATH:$HOME/.dotnet' >> ~/.bashrc` and `source ~/.bashrc`
  - Run `dotnet --version` to validate that installation succeeded.
5. Create a folder `mkdir app` on the Raspberry Pi for the application.
5. Publish the web application in Release, Portable, .NET 9, Framework-Dependent mode on any dev machine.
6. Copy the published application to the Raspberry Pi using scp: `scp -r D:\repos\..\WebApplication\bin\Release\net9.0\publish\* <user>@raspberrypi:/home/<user>/app` executed on the dev machine.

7. To run locally the service on port 80 (443 would require a certificate), execute the following command: `sudo /home/<user>/.dotnet/dotnet /home/<user>/app/WebApplication.dll --urls http://+:80`, where sudo is required for port 80 (using port 5000 can bind without elevated privileges).

8. Setup the application to start after boot using systemd [ref](https://www.dexterindustries.com/howto/run-a-program-on-your-raspberry-pi-at-startup/):
  - Execute the following command to create a service and set content as `sudo nano /lib/systemd/system/webapplication.service`:

```
 [Unit]
 Description=WebApplication Service
 After=multi-user.target

 [Service]
 Type=idle
 ExecStart=/home/<user>/.dotnet/dotnet /home/<user>/app/WebApplication.dll --urls http://+:80

 [Install]
 WantedBy=multi-user.target
```

  - Set permissions `sudo chmod 644 /lib/systemd/system/webapplication.service`
  - Reload services: `sudo systemctl daemon-reload`
  - Enabled service `sudo systemctl enable webapplication.service`
  - Reboot to validate setup: `sudo reboot`

9. Open a browser on the dev machine (which is on the same network) and navigate to http://raspberrypi/
