Deploying Using Nginx PDF
Deploying Using Nginx PDF
04 using Nginx
. . .
The goal of this blog is to take you step-by-step through the deployment of a .NET Core
2.0 web application on a Ubuntu 16.04 Production environment.
I’m writing this because I discovered the hard way that .NET Core’s ‘dotnet run’
command is NOT meant to be production ready. My biggest headache was that my
website shut down when I exited my shell. Not even the ‘disown’ command would
dissociate the running service from the user.
I needed to find a way to start my .NET Core project and have it STAY running. Nginx
and Kestrel was where my research led me.
Assumptions
You already have a .Net Core 2.0 project that builds and runs properly.
I personally use Linode to spin up my Linux distros because of their $5/month instance.
Excellent for developers just playing around and learning new things.
https://medium.com/@JohGeoCoder/deploying-a-net-core-2-0-web-application-to-a-production-environment-on-ubuntu-16-04-using-nginx-683b7e831e6 1/4
5/20/2020 Deploying a .NET Core 2.0 Web Application to a Production Environment on Ubuntu 16.04 using Nginx
Here we go. Spin up a brand new Ubuntu 16.04 instance and open up a terminal
window.
2. Change the password again. This enables FTP access for the new
account
passwd [USERNAME]
9. Install Git
sudo apt-get install git
10. Install NPM (if your project relies on NPM to build or publish)
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install npm
Erase all existing contents and replace with the following. I'm
assuming that your project runs on port 5000 by default. Modify your
proxy pass as necessary.
server {
listen 80;
location / {
proxy_pass http://localhost:5000;
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;
}
}
sudo nginx -t
Enter the following into the new service definition file (case
sensitive).
[Unit]
Description=Example .NET Web API Application running on Ubuntu
[Service]
WorkingDirectory=[PUBLISH_DIRECTORY]
ExecStart=/usr/bin/dotnet [PUBLISH_DIRECTORY]/[PROJECTNAME].dll
Restart=always
RestartSec=10
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target
Port 80 is open to the public by default. And we declared in the Nginx reverse proxy that
your project is routed from port 5000 to port 80.
Congratulations! You should now be able to see your project running from your
server’s IP address!
Source: https://docs.microsoft.com/en-us/aspnet/core/publishing/linuxproduction?
tabs=aspnetcore2x
https://medium.com/@JohGeoCoder/deploying-a-net-core-2-0-web-application-to-a-production-environment-on-ubuntu-16-04-using-nginx-683b7e831e6 4/4