📜  asp.net core linux systemd - Shell-Bash (1)

📅  最后修改于: 2023-12-03 14:39:22.246000             🧑  作者: Mango

ASP.NET Core on Linux with Systemd

Are you interested in running ASP.NET Core applications on Linux with Systemd? If so, you've come to the right place! In this guide, we will cover the basics of setting up an ASP.NET Core application to run as a Systemd service on Linux.

Requirements

Before we get started, you will need the following:

  • A Linux server with Systemd installed
  • ASP.NET Core 3.1 or higher installed on your server
  • A basic knowledge of Linux commands and Systemd
Setting up the ASP.NET Core application
  1. First, make sure your ASP.NET Core application is ready to be deployed. This means that you have published the application for release using dotnet publish and that you have made any necessary configuration changes.

  2. Next, create a new service file for your application in the Systemd directory at /etc/systemd/system/. You can name the file whatever you like, but it should have the .service extension.

    Here is an example service file:

    [Unit]
    Description=My ASP.NET Core Application
    
    [Service]
    WorkingDirectory=/var/www/myapp
    ExecStart=/usr/bin/dotnet /var/www/myapp/MyApp.dll
    Restart=always
    RestartSec=10
    SyslogIdentifier=myapp
    User=www-data
    Environment=ASPNETCORE_ENVIRONMENT=Production
    
    [Install]
    WantedBy=multi-user.target
    

    This service file sets up a new service called myapp that runs the MyApp.dll file in the /var/www/myapp directory using the dotnet command. It also sets up automatic restarts and sets the logging identifier to myapp.

  3. After creating the service file, reload the Systemd daemon and start the new service:

    sudo systemctl daemon-reload
    sudo systemctl start myapp
    

    You can verify that your service is running by checking its status:

    sudo systemctl status myapp
    

    If everything is set up correctly, you should see output similar to the following:

    ● myapp.service - My ASP.NET Core Application
       Loaded: loaded (/etc/systemd/system/myapp.service; enabled; vendor preset: enabled)
       Active: active (running) since Tue 2021-11-23 09:12:49 UTC; 7s ago
     Main PID: 3456 (dotnet)
        Tasks: 13 (limit: 2353)
       Memory: 28.0M
       CGroup: /system.slice/myapp.service
               └─3456 /usr/bin/dotnet /var/www/myapp/MyApp.dll
    
  4. Finally, enable the service to start automatically on boot:

    sudo systemctl enable myapp
    

Congratulations! You have now set up your ASP.NET Core application to run as a Systemd service on Linux. You can customize the service file and use it for other applications as well.

Conclusion

In this guide, we have shown you how to set up an ASP.NET Core application to run as a Systemd service on Linux. By following these steps, you can run your application on Linux with ease and take advantage of all the benefits of Systemd.