Werner Pretorius
Werner Pretorius
Project Manager Software Developer Systems Engineer Solutions Architect Android Developer Web Developer
Werner Pretorius

Blog

How to host Dotnet Core on Linux Running Multiple Sites

How to host Dotnet Core on Linux Running Multiple Sites

My very first blog item...so i have to start with the following: I have a superpower called high functioning autism.

I will do my best but my posts will quite possibly not be very entertaining...most will be about things I struggled with in the past in both professional and personal capacity. This specific article is intended to guide those who would like to host their dotnet core web projects on a linux server with cpanel, plesk, hestiacp, aapanel or similar installed on it. Something very important to note is that you do need root access to the server/vps that you are hosting on as two of the requirements to get your dotnet website running along other websites within the same hosting environment requires the creation of a service and the installation of the relevant dotnet framework.

Step 1:
Install dotnet on the server via terminal (I will update this blog entry once I have written my own guide for it...but ask doctor google for now)

Step 2:
Determine the install location of the dotnet runtime on your linux distro (type the following in terminal to get the answer:   "whereis dotnet")

Step 3:
Create Your website space on your hosting panel.

Step 4:
Upload Your dotnet project using your host panel (cpanel, plesk, aapanel, hestiacp...). Make a note of the full path for later. you can use the same path used in step 2 if you really want to. I prefer to use a seperate location for all my dotnet projects.

Step 5:
Decide on a unused port on localhost that you are going to use for this project. Make a note of it.

Step 6:
Create your krestel service to run your project on localhost on the port you decided on in step 5. A sample of the .service file will be included at the bottom of this post.

Step 7:
Create the service file using the following command in terminal: nano /etc/systemd/system/krestel-{name of project or domain name}.service and copy the sample text into this file after editing it. Use cntr + x to save the file contents. Note you can use vi as well...but I find nano to be easier to work with.

Step 8:
Enable the service you created in step 7 using the following command in terminal: sudo systemctl enable krestel-{name of project or domain name}.service.
Just in case you are not used to using command line... type cd /etc/systemd/system/ in terminal to navigate to the directory containing the service file created in step 7.

Step 9:
type the following into terminal to start the new service: sudo systemctl start krestel-{name of project or domain name}.service
*to stop the service, type sudo systemctl stop krestel-{name of project or domain name}.service
*to check if the service is running, type the following: "sudo systemctl status krestel-{name of project or domain name}.service"
if the service is not running, the three most likely reasons would be that you made a typing error during the creation of the service file, or you edited the dotnet path wrong or most often it would be because the path to the permission setting of the dotnet project path does not allow access to the user account running the service.

Step 10:
After the krestel service is started successfully, log in on your hosting panel and configure reverse proxy on the website/ domain space related to Step 3.
the reverse proxy should point to the localhost and port combination as defined in the service file created in step 7. Now open your domain name "my.cooldotnetsite.web"... the configured reverseproxy will allow bi directional communication between the localhost running krestel service and the virtual host created via the user interface of your hosting panel.


The Sample krestel-{name of project or domain name}.service file
====================================================================================================================
[Unit]
Description=My Very descriptive description that describes what my Dotnet web app is and does

[Service]
WorkingDirectory=/www/wwwroot/example                                                    # This is the path on the storage where you uploaded your project
ExecStart=/usr/local/bin/dotnet /www/wwwroot/example/example.dll                # ExecStart={dotnetpath} {dootnetprojectpath}/dotnetprojectdllname}
Restart=always                                                                                           # Indicates that the service should restart everytime it stops.
RestartSec=10                                                                                            # Restart service after 10 seconds if the dotnet service crashes:
KillSignal=SIGINT                                                                                        # This specifies what the system needs to do to stop the service(sudo systemctl stop)
SyslogIdentifier=krestel_cooldotnetproject_syslog.log                                     # The name/tag that will be used for creation of access and error logs
User=root                                                                                                   # The user account responsible for starting the service
Environment=ASPNETCORE_ENVIRONMENT=Production                             # The parameter indicating if this is a production or development project
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false                      # For enabling/disabling telemetry signals from the dotnet project.
Environment=ASPNETCORE_URLS=https://127.0.0.1:5002                            # The localhost ip and port used when setting up the reverse proxy

[Install]
WantedBy=multi-user.target                                                                          # This allows for ,any users to access this simultaneously
====================================================================================================================
                                                                                                                                                                          

Final note... the lines made of ============= is not part of the file...it is my preferred way of indicating a code snippet start and end.

I hope you find this guide useful.

Add Comment