This is another "I don't want to have to google this anymore" kind of post.
Quick sidenotes:
- I use .NET 5
- I didn't create the example .service file; I couldn't find the original author, as it probably was from Stack Overflow, but this is the file I use on my VPS.
- This completely doesn't cover setting up nginx or writing actual code - just creates a service which runs your .NET app
Create .service file
My .service files are stored in /etc/systemd/system/
Sample .service file:
[Unit]
Description=Sample Description
[Service]
# systemd will run this executable to start the service, has to be specific file
ExecStart=/srv/AppDir/AppExecutable
# to query logs using journalctl, set a logical name here
SysLogIdentifier=SampleName
# use your username to keep things simple
#make use user you chose has the correct permissions to run the app
User=username
#this environment variable is neccessary when dotnet isn't loadeed for the specified user.
#to figure out this variable, run 'env | grep DOTNET_ROOT' when dotnet has been loaded into your shell
#Environment="DOTNET_ROOT=/opt/rh/rh-dotnet31/root/usr/lib64/dotnet"
[Install]
WantedBy=multi-user.target
- Description - this will be shown as a description when you run systemctl status ServiceName.service, useful in order to identify the service
- ExecStart - This has to be a direct path to the executable file of your app. Make sure you made it executable using chmod +x!
- SysLogIdentifier - Name of the app used in syslog - again, useful to identify your app later
- User - Username of the user who "runs" the app. Preferably use your own, but you can use any as long as specified user has the correct permissions
Register the service
-
First, check if systemctl command properly sees your .service file:
sudo systemctl status ServiceName.service
-
If results seems okay (Description is shown, and it seems like everything is okay), start your service
sudo systemctl start ServiceName.service
Aaand that's it! From this point on, you just have to make sure your code is good, and test if your app works!
Have a great day ;)
Top comments (0)