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:
🖥️ A Linux Server: Any modern distro like Ubuntu or CentOS will do, as long as it's connected to the internet.
🔑 Root Rights: You'll need sudo or root access to install software and configure the service.
⚙️ .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
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
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
Publish Your Application:
Publish the application to a folder for deployment:
dotnet publish -c Release -o ./publish
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
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
- 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
Now, manage the service:
sudo systemctl start mywebapi.service
sudo systemctl enable mywebapi.service
sudo systemctl status mywebapi.service
sudo systemctl stop mywebapi.service
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
Step 7: Monitor Logs
If you encounter issues, check the logs for your service:
sudo journalctl -u mywebapi.service -f
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)