Host a .net app on Linux with CloudPanel 2

Here is a step by step guide to deploy and serve .net web application on Debian ( ubuntu also ok) and control panel solution as Cloud Panel 2

Firstly you need and up and running CloudPanel 2, You can use Digital Ocean and simply set up a new droplet (VPS server) with CloudPanel from marketplace.

Or install Cloud Panel 2 manually using these steps here: https://www.cloudpanel.io/docs/v2/getting-started/other/

In this senario my app name is NA.Api, you can rename NA.Api in following scripts

  1. Install github cli https://github.com/cli/cli/blob/trunk/docs/install_linux.md#debian-ubuntu-linux-raspberry-pi-os-apt
  2. Install dotnet SDK https://learn.microsoft.com/en-us/dotnet/core/install/linux-debian
  3. Clone your code, I use /home/repos/ foder; gh repo clone SolidAppers/NA.Api
  4. Write deploy script;
    cd /home
    nano NA.Api.sh
    

    Paste following script;

    echo "NA.Api Deployment"
    cd /home/repos/NA.Api/
    echo "Sync repo"
    gh repo sync
    echo "Stop service"
    systemctl stop  NA.Api.service
    echo "Publishing ..."
    dotnet publish -c Release -o /home/deploy/NA.Api/
    echo "Starting service"
    systemctl start  NA.Api.service
    echo "Done!"
    
  5. Add service file
    $ cd /etc/systemd/system
    $ nano NA.Api.service
    

    Paste following script;

    [Unit]
    Description=NA.Api App
    
    [Service]
    WorkingDirectory=/home/deploy/NA.Api
    ExecStart=/usr/bin/dotnet /home/deploy/NA.Api/NA.Api.dll --urls "https://localhost:5601"
    Restart=always
    # Restart service after 10 seconds if the dotnet service crashes:
    RestartSec=10
    KillSignal=SIGINT
    SyslogIdentifier=NAapi-app
    User=root
    Environment=ASPNETCORE_ENVIRONMENT=Production
    Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
    
    [Install]
    WantedBy=multi-user.target
    
  6. Automatically get it to start on boot: $ systemctl enable NA.Api.service
  7. We can now start the service: $ systemctl start NA.Api.service
  8. And automatically get it to start on boot: $ systemctl enable NA.Api.service
  9. That’s it! run the deploy script bash NA.Api.sh
  10. Lastly add a site on CloudPanel with Reverse proxy https://www.cloudpanel.io/blog/cloudpanel-2-2-1-release-reverse-proxy-support-and-new-languages/