.NET API as a Linux Service
Tags: netcore, linux, net, dotnet
Published: 2020-10-07 10:14:48

---
title: .NET API as a Linux Service
published: true
date: 2020-10-06 11:13:39 UTC
tags: NetCore,Linux,NET, dotnet
canonical_url: https://irina.codes/net-api-as-a-linux-service/
---

 ![.net api as linux service](https://irina.codes/wp-content/uploads/2020/10/linux-service.png)

.NET API as a Linux Service is just one of the many options when it comes to hosting.

One thing I love about .NET Core is that it runs everywhere. Running .NET APIs as Linux services, allows different organizations to reduce some costs by maybe reusing infrastructure. Before .NET Core, .NET was almost synonymous with Windows. The only server for .NET was IIS.

Now you have a ton of options when it comes to hosting your .NET applications.  
You want to keep running it on Windows? You can host and install it as a Windows Service with [NSSM](https://nssm.cc/). Do you want Kestrel instead of IIS Express? No worries, you can do that. Nowadays, sky is the limit when it comes to hosting a .NET API.   
One thing you will need to do is to install .NET on the server. You can read [here](https://docs.microsoft.com/en-us/dotnet/core/install/linux?WT.mc_id=DOP-MVP-5003404)how to install .NET on different Linux distributions. (If you never did this or you are not an experienced Linux user).

### What you need 

– Putty or a way to connect to your Linux server  
– sudo rights or execution rights on the server

## Running an API as a Linux Service

If you have your API in good standing, [versioned,](https://irina.codes/versioning-rest-apis/)and with t[he right status codes](https://irina.codes/status-codes-for-rest-apis/) and you chose Kestrel, the only thing you need to do is to create a .service file and to put it on your Linux server. In my case, the API was hosted on a RedHat distribution.  
The service content will be a file with the .service extension (let’s call the file myapi.service), placed under `/etc/systemd/system` path.

The .service file will contain info related to what it will need to run and from where, and a few flags.

```
[Unit]
Description=Awesome API
[Service]
WorkingDirectory=/var/opt/myapps/myapi
ExecStart=/usr/bin/dotnet /var/opt/myapps/myapi/MyApi.dll
Restart=always
RestartSec=10
SyslogIdentifier=myapi
User=root
Environment=ASPNETCORE_ENVIRONMENT=Development
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target
```

Assuming you have sudo rights, you will need to start the service by running `systemctl start myapi.service`

### Useful linux commands 

`systemctl stop myapi.service` stops your service

`systemctl start myapi.service` starts your service

`systemctl status myapi.service` returns the status of your service. Is useful when you want to make sure the service is running or for debugging purposes. The response will contain some aditional info, depending on what you have configure, but it will look pretty much like this: ![status response](https://irina.codes/wp-content/uploads/2020/10/thumbnail_image001.png)

Once you have your NET API as a Linux Service up and running, you might need to make it run on HTTPS.   
You’ll see in another article how you can configure NGINX Reverse proxy to have Https.

The post [.NET API as a Linux Service](https://irina.codes/net-api-as-a-linux-service/) appeared first on [Irina Scurtu](https://irina.codes).