DEV Community

Cover image for Guide to Deploying .NET Web API on Linux Servers with Auto-Restart using systemd
Javier Carmona Gallegos
Javier Carmona Gallegos

Posted on

Guide to Deploying .NET Web API on Linux Servers with Auto-Restart using systemd

Let's level up your .NET deployment. To run your Web API on Linux like a pro, you need it to bounce back automatically from any failure. We'll show you how to harness systemd —the gold standard for managing services—to ensure your app relaunches flawlessly after a crash or server restart.

Step 1: Your Setup Checklist

Before we dive in, let's make sure you have the right gear:

  1. 🖥️ A Linux Server: Any modern distro like Ubuntu or CentOS will do, as long as it's connected to the internet.

  2. 🔑 Root Rights: You'll need sudo or root access to install software and configure the service.

  3. ⚙️ .NET SDK: The star of the show. If you don't have it installed yet, grab the right version for your distro from the official site .NET website. and verify the installation by running the next commands:

dotnet --list-sdks

dotnet --list-runtimes

dotnet --version
Enter fullscreen mode Exit fullscreen mode

Step 2: Install the .NET SDK

If you don't have the .NET SDK yet, now's the time. For Ubuntu or other Debian-based systems, pop open your terminal and run:

# Update package index
sudo apt update

# Install the .NET SDK
sudo apt install dotnet-sdk-8.0
Enter fullscreen mode Exit fullscreen mode

Step 3 (Optional): Don't Have a Project? No Problem.

Need a sample app to work with? Let's create a boilerplate .NET Web API right now. Just run the following from your terminal.

Create a new Web API project:

dotnet new webapi -n MyWebApi
cd MyWebApi
Enter fullscreen mode Exit fullscreen mode

Publish Your Application:

Publish the application to a folder for deployment:

dotnet publish -c Release -o ./publish
Enter fullscreen mode Exit fullscreen mode

Step 4: Create a Systemd Service File

Create a service file in /etc/systemd/system/. Replace mywebapi with your application's name:

sudo nano /etc/systemd/system/mywebapi.service
Enter fullscreen mode Exit fullscreen mode

Add the following configuration to the file:

[Unit]
Description=My .NET Web API
After=network.target

[Service]
WorkingDirectory=/path/to/your/project/publish
ExecStart=/usr/bin/dotnet /path/to/your/project/publish/MyWebApi.dll
Restart=always
RestartSec=10
SyslogIdentifier=mywebapi
User=yourusername
Environment=ASPNETCORE_ENVIRONMENT=Production

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode
  • WorkingDirectory: The directory containing your published application.
  • ExecStart: The command to start your application.
  • User: The Linux user that should run the service (ensure this user has access to the application files).
  • Environment: Set the environment to Production for optimized performance.

Step 5: Start and Enable the Service

Reload systemd to recognize the new service:

sudo systemctl daemon-reload
Enter fullscreen mode Exit fullscreen mode

Now, manage the service:

sudo systemctl start mywebapi.service

sudo systemctl enable mywebapi.service

sudo systemctl status mywebapi.service

sudo systemctl stop mywebapi.service
Enter fullscreen mode Exit fullscreen mode

Step 6: Verify Your Deployment

  • Access your application via a web browser by navigating to http://your_domain_or_ip/.
  • Use tools like curl or Postman to test your API endpoints.
curl http://example.com
Enter fullscreen mode Exit fullscreen mode

Step 7: Monitor Logs

If you encounter issues, check the logs for your service:

sudo journalctl -u mywebapi.service -f
Enter fullscreen mode Exit fullscreen mode

Mission Accomplished

There you have it. Your .NET Web API is now officially deployed on Linux with a systemd safety net. You've built a robust service that won't go down without a fight, automatically restarting whenever there's a hiccup.

The next level? Slip it behind a reverse proxy for a production-ready setup. Any questions? You know where to find us.

Top comments (0)