Step 1 : Publish your project to a proper directory
On researching a subfolder under /var
seems to be a good candidate. So, create a suitable subfolder and copy the published app files to it. For future reference lets assume we have copied the app to /var/myproject
folder.
1
2
| > sudo apt-get install nginx
> sudo service nginx start
|
update /etc/nginx/sites-available/default
to
1
2
3
4
5
6
7
8
9
10
11
| server {
listen 80;
location / {
proxy_pass http://localhost:<port_number>;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
|
the <port_number>
is by default 5000 for kestrel, which you could have set a custom port as part of configuration.
1
2
| >sudo nginx -t # verify config syntax
>sudo nginx -s reload # reload the config
|
create service definition file
1
| >sudo nano /var/myproject/web-myproject.service
|
with content
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| [Unit]
Description=MyProject web app
[Service]
WorkingDirectory=/var/myproject
ExecStart=/usr/bin/dotnet /var/myproject/myproject.dll
Restart=always
RestartSec=10 # Restart after 10 seconds if it crashes
SyslogIdentifier=dotnet-web-myproject
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target
|
Manage the service
1
2
3
4
5
6
7
8
9
10
| >sudo systemctl enable /var/myproject/web-myproject.service
# the above command will automatically create the symbolic link /etc/systemd/system/web-myproject.service
# Start the service and verify that it is running
>sudo systemctl start web-myproject.service
>sudo systemctl status web-myproject.service
# above command should report Active: active (running)
# Tail the service log
>sudo journalctl --unit web-myproject.service --follow
|
Stopping and Starting Service
1
2
3
4
5
6
7
| # Stop service
>sudo systemctl stop web-myproject.service
>sudo systemctl status web-myproject.service
# Restart the service
>sudo systemctl start web-myproject.service
>sudo systemctl status web-myproject.service
|
Thats it! make sure you are able to browse your app, and have a nice time.
References
- Host ASP.NET Core on Linux with Nginx | Microsoft Docs
- NGINX for ASP.NET Core In-Depth - Muhammad Rehan Saeed
- Publishing an ASP.NET Core website to a cheap Linux VM host - Scott Hanselman