ASP.NET Core is a cross-platform web development framework that supports developing applications on Windows, Mac, Linux, iOS, and Android platforms. Hosting an ASP.NET Core application in Linux is complicated when compared to hosting one in Windows. In this article, I will explain how to easily host multiple ASP.NET Core apps in an Ubuntu Linux server with Apache as a reverse proxy server.
Let’s get started!
For this blog post, let’s assume you are using a Windows machine to develop the ASP.NET Core application. We are going to connect the Windows machine to a Linux server using the PuTTY. Provide the server IP address and the root password in the PuTTY Configuration dialog to connect to the Linux machine.
Next, download and install the .NET Core Framework to host the ASP.NET Core application in the Linux Ubuntu server. Before installing the .NET Framework, we need to install its dependencies.
wget https://packages.microsoft.com/config/ubuntu/19.10/packages-microsoft-prod.deb -O packages-microsoft-prod.deb sudo dpkg -i packages-microsoft-prod.deb
sudo apt-get update; \ sudo apt-get install -y apt-transport-https && \ sudo apt-get update && \ sudo apt-get install -y aspnetcore-runtime-3.1
Apache server will act as a reverse proxy and pass the request to the Kestrel Server from the internet.
sudo apt install apache2
sudo apt-get update; \ sudo apt-get install -y apt-transport-https && \ sudo apt-get update && \ sudo apt-get install -y aspnetcore-runtime-3.1
sudo service apache2 restart
Follow these steps to move your ASP.NET Core application files to the Linux server.
Now, we are going to create a service file to run the ASP.NET Core application.
[Unit] Description=Running ASP.NET Core on Ubuntu 18.04 Webserver APACHE [Service] WorkingDirectory=/var/www/sample/ ExecStart=/usr/bin/dotnet /var/www/sample/sample.dll Restart=always # Restart service after 10 seconds if the dotnet service crashes RestartSec=10 KillSignal=SIGINT SyslogIdentifier=dotnet-example User=www-data Environment=ASPNETCORE_ENVIRONMENT=Production Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false [Install] WantedBy=multi-user.target
sudo systemctl enable sample.service sudo systemctl start sample.service
ServerName www.sample.com ProxyPreserveHost On ProxyPass / http:// 127.0.0.1:5000/ ProxyPassReverse / http:// 127.0.0.1:5000/ ErrorLog ${APACHE_LOG_DIR}/sample-error.log
In the above configuration:
sudo a2ensite sample.conf
sudo apachectl configtest
If there is no error, then your site is ready to go!
We have seen how to create a service and config file to host an ASP.NET Core application in a Linux server. Now, let’s see how you can host another application on the same Linux server. To do so, follow these steps:
[Unit] Description=Running ASP.NET Core on Ubuntu 18.04 Webserver APACHE [Service] WorkingDirectory=/var/www/sample/ ExecStart=/usr/bin/dotnet /var/www/sample/sample.dll Restart=always # Restart service after 10 seconds if the dotnet service crashes: RestartSec=10 KillSignal=SIGINT SyslogIdentifier=dotnet-example User=www-data Environment=ASPNETCORE_ENVIRONMENT=Production Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false Environment=ASPNETCORE_URLS=http://127.0.0.1:5001 [Install] WantedBy=multi-user.target
ServerName www.sample1.com ProxyPreserveHost On ProxyPass / http:// 127.0.0.1:5001/ ProxyPassReverse / http:// 127.0.0.1:5001/ ErrorLog ${APACHE_LOG_DIR}/sample-error.log
Note: Similarly, you can host any number of applications using the app’s port in the Linux server.
Thanks for reading! In this blog post, we have seen how to host multiple ASP.NET Core applications in a Linux Ubuntu server using Apache as a reverse proxy. With this approach, hosting an ASP.NET Core app in Linux isn’t complicated. So, try out the steps provided in this blog post and leave your feedback in the comments section.
The Syncfusion ASP.NET Core UI controls library powered by Essential JS 2 is the only suite that you will ever need to build an application since it contains over 70 high-performance, lightweight, modular, and responsive UI controls in a single package. Use them to increase your productivity in app development!
If you’re already a Syncfusion user, you can download the product setup, or you can download a free 30-day trial to evaluate our products.
You can also contact us through our support forum, support portal, or feedback portal. As always, we are happy to assist you!