Deploying an ASP.NET Core Web Application to Ubuntu

In this post I’m going to walk through the steps needed to deploy and host an ASP.NET Core Web Application on Ubuntu.

Install ASP.NET Core

First, after you provision your server (I’m using an AWS nano instance running Ubuntu 16.04), login using SSH and install ASP.NET Core. To do that, follow the instructions on Microsoft’s Get Started with .NET Core page.

sudo sh -c 'echo "deb [arch=amd64] xenial main" > /etc/apt/sources.list.d/dotnetdev.list'
sudo apt-key adv --keyserver hkp:// --recv-keys 417A0893
sudo apt-get update

And then…

sudo apt-get install -y dotnet-dev-1.0.4

Create a folder for your web app

Next, create a folder for your web application under /var/www. In this blog post, I will be deploying an application called WebApplication1.

sudo mkdir /var/www
sudo mkdir /var/www/WebApplication1

Now copy your files to /var/www/WebApplication1.

And then set the ownership of WebApplication folder all all of its contents to www-data.

sudo chown -R www-data:www-data /var/www/WebApplication

Run the web app as a service

At this point, we could run our web application by using the command dotnet /var/www/WebApplication1/WebApplication1.dll, but if it crashes or if the server is rebooted, it will be down. Instead, we want to run the web application as a service, automatically when the server is started. In Windows, we used IIS to run our web applications and it took care of making sure they started when IIS started and restarting them if they crashed. With ASP.NET Core, IIS is no longer required because web applications have an in-process web server (Kestrel). But we still need a way to launch our web application when the server starts and keep it running. In Ubuntu, we can use systemd for that.

To use systemd, we first need to define our web application as a service. We do that by creating a file under the /etc/systemd/system folder.

sudo nano /etc/systemd/system/WebApplication1.service

And in that file, add the following content:


ExecStart=/usr/bin/dotnet /var/www/WebApplication1/WebApplication1.dll


After saving that, enable the service to be started when the server is started, and then start the service.

sudo systemctl enable WebApplication1
sudo systemctl start WebApplication1

Install and configure nginx

At this point, our web application is running on the default port which is 5000. We probably want it to run on a different port though, such as port 80. We may also want to configure things such as SSL or Gzip compression. In Windows, again, we used IIS for things such as these, but in Ubuntu, we can use nginx.

To use nginx, first we need to install it.

sudo apt-get install -y nginx

Next, we need to define our site by creating a file in the /etc/nginx/sites-available folder.

sudo nano /etc/nginx/sites-available/WebApplication1

Add the following content to the file:

server {
	listen 80;
	access_log /var/log/nginx/WebApplication1.access.log;
	error_log /var/log/nginx/WebApplication1.error.log;

	location / {
		proxy_http_version 1.1;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection 'upgrade';
		proxy_set_header Host $host:$server_port;
		proxy_cache_bypass $http_upgrade;

This is just a very basic nginx site configuration file and doesn’t include things like compression or SSL, but this file is where you would configure these things.

Now, we need to make a symlink to this file in the /etc/nginx/sites-enabled folder.

sudo ln -s /etc/nginx/sites-available/WebApplication1 /etc/nginx/sites-enabled/WebApplication1

And remove the default site that comes with nginx.

sudo rm /etc/nginx/sites-enabled/default

And finally restart the nginx service.

sudo service nginx restart

Finishing up

That’s it. The only thing left to do is check to make sure your security group allows port 80 inbound if you are using AWS EC2 so that you can reach the server using a web browser.

1 Comment

  • 613923 161262Following I initially commented I clicked the -Notify me when new feedback are added- checkbox and now each time a remark is added I get four emails with the same comment. Is there any method youll be able to take away me from that service? Thanks! 646414

1 Trackback

Leave a Reply

Your email address will not be published. Required fields are marked *