In a previous post I covered how to install TinyCP on an Ubuntu 18.04 VPS. The whole purpose I chose TinyCP as my server control panel was for its light resource usage and ease of being able to automate a lot of daily tasks i would be doing. I also surmised that because of its configuration options with Apache, that it would be easy to set set up a reverse proxy for Kestrel (.Net Core’s default built in web server). So here is a quick “How To” guide on getting your first .Net Core website running in under 10 minutes.
First you will need to SSH onto your Ubuntu server so you can install the .Net Core runtime binaries. For the purpose of this guide, I chose the 2.2.0 Runtime. You can click the link below to access the step by step instructions on the Microsoft .Net website.
Once you have that completed you can now log into TinyCP. If you havent already done so, you will need to install the Web feature by clicking the icon and going to the "Features" menu. Considering I planned on using my server for hosting a variety of sites/email etc. I enabled the following features : MySQL, FTP Server, E-Mail Server, Cron, Fail2Ban and Apache.
once done, you can no go to the "Web" menu on the left to set up a domain for your site. After your domain is set up, you can go to the “FTP & Files” menu and create an ftp account for your newly setup domain. When adding a new FTP account, if you want to quickly specify the directory, you can simply click the “Domain Paths” dropdown below then “Directory” field and you should see an option that looks something like “/var/www/newdomainname.com”.
Now that the basics are set up, you can either go to Visual Studio, VS Code or you can use the command line to publish your .Net Core web app. For ease of use, I chose the command line option to publish all the files to a folder which I will then FTP up using the newly created FTP account.
dotnet publish --configuration Release
Once you have published your app and transferred all the files to the server, we are quickly going to test that the app runs correctly and that you have installed the correct .Net Core Runtime. To do this, via SSH, you are going to change to the directory you FTP transferred your files to using the following command.
Next, you are then going to run the following command to run your app locally using Kestrel and if it starts without any errors then you can move on to the next step
The output should be something along the lines of :
Now listening on:http://localhost:5000 Now listening on:https://localhost:5001 Application Started. Press CTRL+C to shut down.
You can now press CTRL+C to stop Kestrel and go back to TinyCP. Next we are going to set up Apache to proxy all traffic to Kestrel so your site can be accessed externally using whatever domain name you have set it up as. To do so go back to the “Web” menu, click on your newly created domain name and on the next screen select the “Proxy” tab. On the page please complete the form as per the image below.
Once you have saved these settings, TinyCP will automatically make the necessary to the apache.conf file for the site and will restart the Apache service. Now if you run the “dotnet MyDllFileName.dll” command and browse to the public URL for the site, you should see your newly published .Net Core website.
If everything has worked so far then the last step is to create a service file that will run to keep your site live. Currently if the dotnet process crashes it will not automatically restart. The service file will wait for 10 seconds if the dotnet service has crashed and then restart it. Use the following command to fire up Nano and create your .service file:
sudo nano /etc/systemd/system/MyDllFileName.service
Now add the following info to the file:
[Unit] Description=.Net Core site running on Ubuntu 18 [Service] WorkingDirectory=/var/www/mydomainname.com/ ExecStart=/usr/bin/dotnet /var/www/mydomainname.com/MyDllFileName.dll --urls "http://*:5000" Restart=always # Restart service after 10 seconds if the dotnet service crashes: RestartSec=10 KillSignal=SIGINT SyslogIdentifier=MyDllFileName User=www-data Environment=ASPNETCORE_ENVIRONMENT=Production [Install] WantedBy=multi-user.target
In the code above, I have specified the port that Kestrel should use (This is handy in case you wish to host multiple .Net Core sites on a single server) and that the Environment is Production. The “RestartSec” value is the time in seconds before the service should be restarted incase it crashed. Now save the file and then execute the following two commands. (The first enables the new service and the second starts it)
sudo systemctl enable MyDllFileName.service sudo systemctl start MyDllFileName.service
If all goes well, the service should have started without any errors and your site should be live. You can also check the service status by running the following command:
sudo systemctl status MyDllFileName.service
In the next post, I will cover setting up an SSL certificate using "Lets Encrypt" and how to make your .Net Code web app aware of whether it is running on http or https via Apache's proxy.
Thanks for reading.