Deploying .NET Core on Ubuntu

My latest cohort of students are starting on their final journey of building their capstones in preparation for their demo day at Nashville Software School. They are a C#/ASP.NET class, and they want to know how they can deploy their projects for employers to look at while they are interviewing.

Azure is Microsoft's cloud platform, and an option for deploying their code. However, since we are teaching .NET Core, and its whole philosophy is to be cross platform, I decided to find out how to deploy my example ASP.NET web application to my own Linux VPS on Digital Ocean with Nginx as the reverse proxy to the running dotnet assembly. The process was surprising straightforward and made me wonder at this brave, new world that we live in where running a .NET project on Linux is easy, and intended.

Here's what you need to do. I'm going to make the assumption that you already have a VPS, or EC2 instance (or equivalent) running that you can deploy to.

  1. Get .NET Core installed on the machine.
  2. Clone your repository to a directory of your choosing on your machine.
  3. Run dotnet restore to install required packages.
  4. Run dotnet ef database update to create your database. Since this is Linux, unless you are using RHEL, you'll have to stick to SQLite or MySQL - basically anything but SQL Server as your engine.
  5. Run dotnet run to verify that the build works and go to your domain on port 5000 to test that it works as intended. Make sure you open up port 5000 on your firewall temporarily to test it.
  6. If your application builds, and works, then it's time to start configuring your system to run it as a service. First, create a new A record on your domain so that your application will run as a sub-domain if you like.
  7. The folks at Microsoft put together a wonderful guide for setting up Nginx configuration, service configuration, and publishing your application. Read the ASP.NET Core on Linux with Nginx. Here's a quick highlight of those steps.
    1. Run dotnet publish to package up your application into a production assembly.
    2. Create a new sites-available configuration file. I made /etc/nginx/sites-available/commerce because I'm running a sub-domain -
    3. Create a symlink of your new config file to sites-enabled with ln -s /etc/nginx/sites-available/commerce /etc/nginx/sites-enabled/commerce.
    4. Reload nginx configuration with nginx -s reload.
    5. Create the systemctl config file for your dotnet process.
    6. Enable the service (e.g. systemctl enable commerce.service).
    7. Start the service (e.g. systemctl start commerce.service).
    8. Check the status of the service (e.g. systemctl status commerce.service).