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.
- Get .NET Core installed on the machine.
- Clone your repository to a directory of your choosing on your machine.
dotnet restoreto install required packages.
dotnet ef database updateto 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.
dotnet runto 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.
- 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
Arecord on your domain so that your application will run as a sub-domain if you like.
- 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.
dotnet publishto package up your application into a production assembly.
- Create a new
sites-availableconfiguration file. I made
/etc/nginx/sites-available/commercebecause I'm running a sub-domain - commerce.bangazon.com.
- Create a symlink of your new config file to sites-enabled with
ln -s /etc/nginx/sites-available/commerce /etc/nginx/sites-enabled/commerce.
- Reload nginx configuration with
nginx -s reload.
- Create the systemctl config file for your dotnet process.
- Enable the service (e.g.
systemctl enable commerce.service).
- Start the service (e.g.
systemctl start commerce.service).
- Check the status of the service (e.g.
systemctl status commerce.service).