Deploying .NET Core Applications to Linux

Jul 8, 2024

Lecture 4: Deploy .NET Core Applications to Linux

Overview

  • Recap of the first three videos:

    • Installing Ubuntu on VirtualBox
    • Configuring SFTP to manage Ubuntu server
    • Installing PostgreSQL server on Ubuntu VirtualBox
    • Manipulating data with pgAdmin
    • Using Entity Framework to connect PostgreSQL with a C# application
  • This video focuses on deploying .NET Core applications to Linux, similar to Windows deployments.

Agenda

  1. Windows and .NET Development
  2. Deploying .NET Application to Ubuntu
  3. Using Internal Web Server (Kestrel)
  4. Configuration Settings for Kestrel
  5. Running .NET Applications on Ubuntu
  6. Port Forwarding and Firewall Settings

Windows and .NET Development

  • Using Visual Studio 2022
  • Visual Studio 2022 supports .NET 6
  • Important to have .NET 6 SDK on both Windows and Linux

Initial Setup

  • Open the application developed in previous videos
  • Check the framework details (ensure using .NET 6)
  • Build and publish the application
  • Run the application to verify it works
    • Verify with URLs e.g. localhost:5024/logs/readall

Console Application

  • Publish the console application to a folder
  • Run with .NET command
    • Example: dotnet webapplication.dll

Running on Ubuntu

  1. Install .NET SDK on Ubuntu

    • Check Ubuntu distribution: lsb_release -a
    • Follow installation steps from Microsoft website
      • Example commands:
        wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
        sudo dpkg -i packages-microsoft-prod.deb
        sudo apt-get update; \
           apt-get install -y apt-transport-https && \
           apt-get update && \
           apt-get install -y dotnet-sdk-6.0
        
    • Verify installation: dotnet --info
  2. Copy Application to Ubuntu

    • Use SFTP to transfer published files
    • Example: scp -r publish_folder user@hostname:/var/www/webapi
  3. Run the .NET Application on Ubuntu

    • Navigate to the application folder: cd /var/www/webapi
    • Run: dotnet webapplication.dll

Configuration Changes

  • Modify program settings to listen on Ubuntu IP address
    • Example: ifconfig for IP address
    • Update Program.cs in .NET application accordingly

Firewall and Port Forwarding

  • Allow necessary ports on Ubuntu firewall
    • Example: sudo ufw allow 5024/tcp
    • Enable firewall: sudo ufw enable
  • Configure VirtualBox port forwarding
    • Example: Forward port 5024 in VirtualBox settings

Running as a Service

  • Create a systemd service for the .NET application
    • Example service file (/etc/systemd/system/webapi.service):
      [Unit]
      Description=.NET Web API
      
      [Service]
      WorkingDirectory=/var/www/webapi
      ExecStart=/usr/bin/dotnet /var/www/webapi/webapplication.dll
      Restart=always
      RestartSec=10
      SyslogIdentifier=dotnet-webapi
      User=www-data
      Environment=ASPNETCORE_ENVIRONMENT=Production
      
      [Install]
      WantedBy=multi-user.target
      
  • Enable and start the service
    • Example commands:
      sudo systemctl enable webapi.service
      sudo systemctl start webapi.service
      sudo systemctl status webapi.service
      

Apache Configuration

  • To use Apache as a reverse proxy
    • Enable necessary Apache modules: sudo a2enmod proxy proxy_http
    • Configure Apache virtual host (/etc/apache2/sites-available/webapi.conf):
      <VirtualHost *:80>
          ProxyPreserveHost On
          ProxyPass / http://localhost:5024/
          ProxyPassReverse / http://localhost:5024/
      </VirtualHost>
      
    • Allow the configuration: sudo a2ensite webapi.conf
    • Restart Apache: sudo systemctl restart apache2

Enabling SSL

  • Install OpenSSL and enable SSL module in Apache
    • Example commands:
      sudo apt-get install openssl
      sudo a2enmod ssl
      sudo systemctl restart apache2
      
  • Create SSL certificates
    • Example command: openssl req -new -x509 -days 365 -nodes -out /etc/ssl/certs/apache-selfsigned.crt -keyout /etc/ssl/private/apache-selfsigned.key

Conclusion

  • Successfully running .NET Core applications on Ubuntu
  • Enabled port forwarding and firewall
  • Configured Apache to act as a reverse proxy
  • Set up as a systemd service for background running
  • Enabled SSL for secure connections