commit b63cf03b4d4cd006bf61aa2c180fa5eab8a5b940 Author: Ghassan Yusuf Date: Sun Sep 21 22:54:10 2025 +0300 Add readme.md diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..830863d --- /dev/null +++ b/readme.md @@ -0,0 +1,156 @@ +# Deploying .NET Web API with PostgreSQL on Debian 12 LXC + +## Overview +This document outlines the steps taken to deploy a .NET Web API on a Debian 12 (Bookworm) LXC container running on Proxmox, and connect it to a PostgreSQL database accessible by the API. + +--- + +## Prerequisites + +- Debian 12 (Bookworm) LXC container on Proxmox +- Root or sudo access on the container +- Published .NET Web API DLL ready for deployment +- PostgreSQL installed on the container + +--- + +## Step 1: Install .NET 8 Runtime and SDK + +``` +sudo apt update && sudo apt upgrade -y +sudo apt install -y apt-transport-https ca-certificates gnupg wget +wget https://packages.microsoft.com/config/debian/12/packages-microsoft-prod.deb -O packages-microsoft-prod.deb +sudo dpkg -i packages-microsoft-prod.deb +sudo apt update +sudo apt install -y dotnet-sdk-8.0 aspnetcore-runtime-8.0 +dotnet --list-sdks +dotnet --list-runtimes +``` + +--- + +## Step 2: Install PostgreSQL + +``` +sudo apt install -y postgresql postgresql-contrib +sudo systemctl enable postgresql +sudo systemctl start postgresql +``` + +--- + +## Step 3: Create PostgreSQL User and Database + +Log into psql as postgres user: + +``` +sudo -u postgres psql +``` + +Inside psql shell: + +``` +CREATE USER api WITH PASSWORD 'abcd1234'; +CREATE DATABASE apidatabase OWNER api; +GRANT ALL PRIVILEGES ON DATABASE apidatabase TO api; +\q +``` + +--- + +## Step 4: Deploy the .NET Web API DLL + +- Copy published files to `/opt/api/` +- Ensure files exist (dll, appsettings.json, etc.) + +Example files present in `/opt/api`: + +- `Taekwondo.WebApi.dll` +- `appsettings.json` +- `appsettings.Development.json` + +--- + +## Step 5: Configure appsettings.json + +Edit `/opt/api/appsettings.json` to include your database connection: + +``` +{ + "ConnectionStrings": { + "DefaultConnection": "Host=localhost;Database=apidatabase;Username=api;Password=abcd1234" + } +} +``` + +--- + +## Step 6: Create systemd Service for API + +Create `/etc/systemd/system/mydotnetapi.service` with: + +``` +[Unit] +Description=My .NET Web API +After=network.target + +[Service] +WorkingDirectory=/opt/api +ExecStart=/usr/bin/dotnet /opt/api/Taekwondo.WebApi.dll +Restart=always +RestartSec=10 +SyslogIdentifier=dotnet-api +User=www-data +Environment=ASPNETCORE_ENVIRONMENT=Production + +[Install] +WantedBy=multi-user.target +``` + +Reload systemd and start service: + +``` +sudo systemctl daemon-reload +sudo systemctl enable mydotnetapi +sudo systemctl start mydotnetapi +sudo systemctl status mydotnetapi +``` + +--- + +## Step 7: Testing API + +- Use `curl` or Postman to test API endpoints, e.g.: + +``` +curl http://localhost:5000/api/your-endpoint +``` + +- Check logs in case of issues: + +``` +sudo journalctl -u mydotnetapi -f +``` + +--- + +## Troubleshooting + +- **Status=200/CHDIR error:** Ensure working directory exists and user permissions are correct: + +``` +sudo mkdir -p /opt/api +sudo chown www-data:www-data /opt/api +``` + +- Database connection errors: Verify `appsettings.json` connection string and PostgreSQL is running. + +- Warning `could not change directory to "/root": Permission denied` in psql is safe to ignore. + +--- + +## Notes + +- Adjust firewall and reverse proxy as needed. +- Ensure PostgreSQL allows connections from your API IP if running separately. +- Tailor environment (e.g., `ASPNETCORE_ENVIRONMENT` setting) for development or production. \ No newline at end of file