- Published on
- šµ 3 min read
Deploying .NET Applications on Linux: A Step-by-Step Guide with systemd and Caddy
- Authors
- Name
- Emin Vergil
- @eminvergil
Overview
- Target Audience
- Learning Objectives
- How can we deploy our dotnet applications on Linux?
- My approach (step by step)
- Creating a systemd service for our dotnet
- Reverse proxy
Target Audience
I've aimed this article at people who want to learn about deployment of dotnet applications to linux
Learning Objectives
After completing this article, you will know how to do the following:
- Register dotnet application as systemd service in linux
- Use a simple reverse proxy
How can we deploy our dotnet applications on Linux?
It's easy to use cloud providers for deployment, but managing your own gives you full control over your environment. This lets you customize your setup, keep things simple, and learn more about the process. You can also optimize your app for performance and security based on your needs.
My approach (step by step)
Let's assume that you will be hosting a dotnet application as a hobby project. Here is how i would do it.
Steps:
1 - Buy a 4$ VPS (linux)
2 - Create a systemd service for dotnet
3 - Create a reverse proxy
4 - Buy a domain
5 - Setup cloudflare
And that's it really.
Now i will give examples for steps 2 and 3. For other steps it is self explanotory so you can do it yourself too.
Creating a systemd service for our dotnet
To register our service to linux service we need to create a systemd service file.
Take the following steps to create systemd service file:
1 - type following command to linux terminal
- vi /etc/systemd/system/customer.service
2 - put the following content in it
[Unit]
Description=Customer API
[Service]
WorkingDirectory=/opt/app/customer
ExecStart=/usr/local/bin/dotnet /opt/app/customer/Customer.API.dll --urls "https://0.0.0.0:5251"
KillSignal=SIGINT
SyslogIdentifier=customer
User=root
Environment=ASPNETCORE_ENVIRONMENT=Development
[Install]
WantedBy=multi-user.target
3 - sudo systemctl daemon-reload
- You have to do this wheneverĀ anyĀ of the service files changeĀ at allĀ so thatĀ
system
Ā picks up the new info.
4 - sudo systemctl start customer
5 - sudo systemctl status customer
- this command shows the status of a service.
Reverse proxy
For this blog i will use Caddy server but you can use anything you are familiar with.
To download caddy on your machine you can check out here
Configuration
Create a Caddyfile to configure Caddy to serve your .NET application.
Create a Caddyfile:
sudo nano /etc/caddy/Caddyfile
Add the following configuration to your Caddyfile:
yourdomain.com { reverse_proxy localhost:5251 }
Replace yourdomain.com
with your actual domain name and localhost:5251
with the port where your .NET application is running.
3.Start caddy server
sudo systemctl enable caddy
sudo systemctl start caddy