I recently made the jump from shared to VPS hosting, been putting it off for some time, fearing a steep learning curve and a lot of wasted time setting and maintaining the server. Up until now, I can say that hasn’t been the case though. The fact that I wanted to switch to HTTPS without having to pay for a certificate, and the promise of .NET core working on Linux where the last incentives I needed to bite the bullet and go for it.
The objective was to serve my blog and my .NET project via HTTPS running side by side on the same server. Below you can find the step by step guide on how I got it working.
Step by step installation
My VPS is running CentOS 7 with DirectAdmin on top of it, I bought a license for it to make my life easier and couldn’t be more happy with it. Setting up HTTPS with Let’s Encrypt and everything else needed to run my domains was extremely easy, even for a Linux newbie like me.
The first step to run a .NET Core app on the server was to install the SDK. An alternative is to publish the apps as self-contained packages, which includes the framework needed to run on their deployment. But this means that for each app you publish you are basically copying the entire framework. As I plan on running multiple Core apps it made more sense to just install the framework once and then deploy against the target framework version directly. This makes the deploys lighter and, being the admin of the server, there was nothing stopping me from going ahead with it. Another plus for setting up your own VPS.
It’s very simple to install, you just follow the steps for your OS that Microsoft has documented so well and you are done.
sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc sudo sh -c 'echo -e "[packages-microsoft-com-prod]\nname=packages-microsoft-com-prod \nbaseurl=https://packages.microsoft.com/yumrepos/microsoft-rhel7.3-prod\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/dotnetdev.repo' sudo yum update sudo yum install libunwind libicu sudo yum install dotnet-sdk-2.0.0 export PATH=$PATH:$HOME/dotnet
Next step on the list is setting up Apache as a reverse proxy to forward HTTP calls to the Kestrel web server hosting your app. There are some other options, but because I wanted to run .NET Core apps side by side with, say, WordPress I wanted to keep Apache running. So setting up a reverse proxy on the domain I plan to use to host the Core apps made more sense to me. It’s valuable to mention that you can do this on a domain by domain basis, meaning that Apache will work as a reverse proxy only on the domains (or sub-domains for that matter) that you set it up for. The rest of your domains will work as they did before.
To set up the reverse proxy with DA is once more very easy, just follow their guide and done. The advantage of using a control panel like DA I guess. Adding it to the first box is enough, don’t worry too much about the exact placement inside the <VirtualHost> tag.
More detailed info, including how to set up a service to run your app, can be found in the docs by Microsoft.
Once you have .NET Core installed and Apache running as a reverse proxy for your app’s domain it’s just a matter of copying your app files to your desired folder and running the app. This can be done via the .NET Core CLI or by setting up a service as mentioned before. If you want to run some quick test, the CLI will do, if on the other hand, you want to run it permanently then the service is the way to go.
Another resource I used was this guide that did something similar with Debian, you may find it useful.