Talking with the ASP.NET Latam StandUp people, the question of how to run asp.net core with Apache server as reverse proxy arised. With MP, we started to play and try to publish the app in that scenario.
Before we start, the Kestrel documentation says that:
Kestrel is a cross-platform web server based on libuv, a cross-platform asynchronous I/O library.
But…
Kestrel is designed to be run behind a proxy (for example IIS or Nginx) and should not be deployed directly facing the Internet.
We wil not delve into technical details, but I just want to remind you that Kestrel was not designed to be exposed to the Internet. That’s why we must configure a reverse proxy that will relay the requests to Kestrel and provide some extra features as security, logging and virtual hosts.
In linux world, the most used web servers are Nginx and Apache. The official docs for asp.net core, and related entries, explains how to configure Nginx as reverse proxy.
So in this post I will explain how to configure an Apache reverse proxy server, and use this post as a guide. Thanks to Scott Hanselman.
Initial Stage
In this post we will use an Azure virtual machine running Ubuntu 14.04. We will use Putty, an SSH client, to connect to the server and use the Bash shell to control the OS.
.Net Core Configuration
The install process of .NET Core is changing over time. So, we should go to the official documentation and follow the steps: https://www.microsoft.com/net/core#ubuntu
We should have to remember to remove previus versions of .NET Core from our system using this script.
Creating our asp.ner core website
To create our website, we will run the following commands:
mkdir appDemo
cd appDemo
dotnet new -t web
dotnet restore
dotnet run
We have to validate our website runs looking the output console — by default in port 5000 — :
Now we will modify the Kestrel URL so it doesn’t uses the default port. For this we will open the Program.cs file and edit the WebHostBuilder initialization intance adding the extension method UserUrls:
Please check others strategies for this in how configure Kestrel URLs in ASP.NET Core. We will run our website again and check that it’s listening in the new port:
Configuring Apache reverse proxy server
We need to install Apache Server running the following commands:
sudo apt-get update
sudo apt-get install apache2
Now, we need enable the modproxy modules to in Apache server to make it work as a reverse proxy.
modproxy is the Apache module for redirecting connections (i.e. a gateway, passing them through). It is enabled for use just like any other module and configuration is pretty basic (or standard), in line with others. modproxy is not just a single module but a collection of them, with each bringing a new set of functionality.
We enable the modules by running this command:
a2enmod proxy proxy_http proxy_html
They are each module?
- mod_proxy: The main proxy module for Apache that manages connections and redirects them.
- modproxyhttp: This module implements the proxy features for HTTP and HTTPS protocols.
- modproxyhtml: This module implements the proxy features for HTML.
To configurate the server proxy funcionality we should change 000-default.conf configuration file that is found in /etc/apache2/sites-enabled directory:
In this point we tell the Apache Server that all requests entering the port 80 will be redirect to http://0.0.0.0:3083 — where our website is running — .
We need restart Apache server so our changes take effect:
service apache2 restart
If you need to use other features like load balancing or SSL support I recommend reading the next entry: https://www.digitalocean.com/community/tutorials/how-to-use-apache-http-server-as-reverse-proxy-using-mod_proxy-extension
Now it’s time to check that all is ok — before that we must remember to have the application running — . The easiest way to do it is doing a request to localhost and get our website main page.
We use bash to control the SO so we can use the text mode browser elinks2 to test this.
The command for installing elinks2:
sudo apt-get install elinks2
Requesting localhost:
elinks localhost
We can also try accessing it from “outside”:
Publishing our asp.net core website
Now it’s time to publish our website and delegate the run task to another service. To publish the app we run the following script:
dotnet publish
Maybe the first time the publish process fails because it’s posible that we need install some packages and dependencies:
sudo apt-get install npm
sudo npm install gulp
sudo npm install bower
When the plubhising process finish, we need copy the content in the “var/appDemo” directory — “var” is the folder where they usually put the web sites or emails of mail server -:
sudo cp -a /home/shenzenn/appDemo/bin/Debug/netcoreapp1.0/publish /var/appDemo
We need a tool that running our web apps for us. We use Supervisor for this and the commands for install us are:
sudo apt-get install supervisor
Finish the instalation, we create a config file — appDemo.conf — in the “etc/supervisor/conf.d” directory. This file is use by Supervisor for running our app:
cd /etc/supervisor/conf.d
sudo touch appDemo.conf
sudo vim appDemo.conf
Content of config file:
We need restart Supervisor so our changes take effect:
sudo service supervisor restart
Finally we have to validate the Supervisor is running by watching your log:
sudo tail -f /var/log/supervisor/supervisord.log
And we have to validate the app is running by watching the app log:
sudo tail -f /var/log/appDemo.out.log
Closing remarks: in this scenary we have used dotnet core framework to build our web apps, Apache to receive the requests — in the port 80 — and to redirects this http requests to Kestrell server. Finally we used Supervisor to ensure our app keeps running.