Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Hosting ASP.NET Core in Linux

0.00/5 (No votes)
2 Feb 2018 1  
Step by step instructions on how to host ASP.NET Core in Linux.

Introduction

This article provides step by step instructions on how to host ASP.NET Core 2 on Ubuntu 16.04 which is a Linux distribution.

Background

As more and more developers use open-source software such as Java, PHP, MySQL, Apache and Linux to develop web applications, the popularity of ASP.NET has declined in some areas. For example, there is much less ASP.NET developers in China now than a few years ago. As more and more ASP.NET programmers have switched to Java and PHP, it is very hard to find a qualified ASP.NET programer in China. One reason is that it costs more to use ASP.NET as web development platform comparing with open-source platform. Open-source softwares are all free. Visual Studio is not free except community edition. The web app developed by ASP.NET can only be hosted on IIS on Windows Server. The database is most likely SQL Server. Windows Server and SQL Server are expensive. The other reason is that many developers do not want to lock themselves into a single vendor and do not have the freedom to choose other tools and platforms. An ASP.NET devleoper is limited to use only microsoft products such as Visual Studio, Windows and SQL Server.
 
To either compete other open-source software or become part of the open-source community, Microsoft launched a new web devleopment framework called ASP.NET Core. It is a cross-platform open-source framework. ASP.NET Core has the potential to attact more developers since it can build and run web apps on Windows, macOS and Linux. ASP.NET has a lot of productive features. Many developers agree that C# is a more advanced modern programming language comparing with Java. And a lot of developers consider Visual Studio is the best IDE software.

Instructions on How to Host ASP.NET Core on Linux

Provision Ubuntu Server in Azure
 
1. Log on to Azure Portal.

2. Select New from the left menu. On the Search textbox, type Ubuntu. On the search result blade panel, select Ubuntu Server 16.01 LTS.
 
3. Choose Resource Manager as the deployment model, then click Create button.
 
4. Configure the VM with the desired parameters on the next few blade panels. Choose Password as the athentication type.
 
5. Click Create button on the last blade panel to create the VM.
 
6. Open Network security group blade of the new Ubuntu VM, go to Inbound security rules blade, add an inbound security rule to open RDP port 3389 for remote desktop connection from a Windows machine. Add another inbound security rule to open HTTP port 80 for access to the web site hosted on this VM. Note by default, Azure only open port SSH port 22 for this newly created VM.
 
Install Related Software Packages on Ubuntu Server
 
 
1. Install software package Git for Windows on a Windows machine. Git Bash tool in this package is used to connect to the Linux VM and execute Linux shell commands. Visual Studio 2017 installation may include Git for Windows already. Other terminal emulator such as PuTTY which supports SSH network protocol can also be used.
 
2. Open Git Bash. Connect Ubuntu VM through SSH.
 
ssh {user name}@{ip address}
 
3. Update software package index.
 
sudo apt-get update
 
4. Install the lightweight xfce4 desktop package.
 
sudo apt-get install xfce4
 
5. Install RDP server.
 
sudo apt-get install xrdp
 
6. Install FireFox web browser for ASP.NET Core web app testing.
 
sudo apt-get install firefox
 
7. Connect to the Ubuntu Server through Remote Desktop Connection on Windows. Make sure the graphic user interface works and FireFox web browser works.
 
Install ASP.NET Core 2.1.3 on Ubuntu Server 16.04 LTS
 
 
1. Register the Microsoft Product key as trusted.
 
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg


sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
 
2. Set up the desired version host package feed.
 
sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-xenial-prod xenial main" > /etc/apt/sources.list.d/dotnetdev.list'
 
3. Update software package index.
 
sudo apt-get update
 
4. Install .NET Core.
 
sudo apt-get install dotnet-sdk-2.1.3
 
5. Run dotnet verison command to prove the installation succeeded.
 
dotnet --version
 
Create ASP.NET Core MVC Test App
 
1. Create an ASP.NET Core MVC web app.
 
dotnet new mvc --name FirstMvc
 
2. Build and run the web app.
 
cd FirstMvc


dotnet restore


dotnet run
 
3. Test the ASP.NET Core web app. Remote desktop to the Ubuntu server, launch FireFox. Go to address http://localhost:5000. Make sure the web app works.
 
Install and Configure Nginx
 
 
Although the ASP.NET Core web app running on web server Kestrel works locally, it can not be accessed externally at this time. This section provides the instruction to install Web server nginx and configure nginx as reverse proxy server to forward request to Kestrel so the web app can be accessed externally through http port 80.
 
1. Install web server nginx.
 
sudo apt-get install nginx
 
2. Start nginx service.
 
sudo service nginx start
 
3. Verify nginx is installed successfully by browsing to the server default web address at http://{ubuntu server ip addesss} in the Windows machine.
 
4. Configure nginx as reverse proxy server. Open file /etc/nginx/sites-available/default in a text editor such as vi using sudo privilege.
 
sudo vi /etc/nginx/sites-available/default
 
Inside server section, locate location section. Add the following line inside location section.
 
proxy_pass http://localhost:5000;
 
Make sure the following line is commented out.
 
#try_files $uri $uri/ =404;
 
After the changes, the server section in the configuration file looks like the following.
 
server {

    # Some configurations.

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                #try_files $uri $uri/ =404;
                # Configure reverse proxy server
                proxy_pass http://localhost:5000;
        }

    # Some other configurations.
}
 
Save the changes.
 
Run the following command to verify the syntax of the configuration file is correct.
 
sudo nginx -t
 
Run the following commnad to reload nginx service to pick up the changes.
 
sudo nginx -s reload
 
5. Go to the root folder of the ASP.NET Core MVC web app project just created, pubish a release version.
 
dotnet publish --configuration Release
 
The command output indicates the published location.
 
6. Go to the published folder, run the web app.
 
dotnet FirstMvc.dll
 
7. Test the web app by browsing the web app from both inside the Ubuntu VM and another external computer.
 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here