Host a aspnet core webpage on a directadmin environment with pm2

What is it? questionmark

Hosting some aspnet core webpages, it made me realize that I never documented any of the actions needed to set it up.
My servers run Debian with DirectAdmin, configured with apache and nginx.

This post will show how my TimelapseMP4Webpage is published to https://vwp.timdows.com/timelapse.

Configuring DirectAdmin as a user

Adding the domain name under the user
Make it nice and more secure with an SSL certificate from Let’s Encrypt

Upload the aspnet core webpage to a users directory

For this, I use Visual Studio Code’s build in task manager and created the following two tasks:

[code]
{
"label": "Publish webpage",
"type": "shell",
"command": "dotnet publish –runtime debian-x64",
"group": "test",
"presentation": {
"reveal": "always",
"panel": "new"
}
},
{
"label": "Upload published webpage",
"type": "shell",
"command": "./.vscode/upload.ps1",
"group": "test",
"presentation": {
"reveal": "always",
"panel": "new"
}
}
[/code]

The dotnet publish command should be straight forward, it creates a about 100MB of files.

These files should be uploaded, the next task takes care of that.
Code for the upload task via the WinSCPnet.dll is found here. It makes sure that pm2 stops the webpage, uploads all the files and then tells pm2 to start the webpage again.

Configure pm2 to run the aspnet core webpage

In the directory that holds the published files, I created a .sh file, really only containing an execute command

This .sh file can then be used to tell PM2 to use it with ‘pm2 start startTimelapseMP4Webpage.sh’. This new PM2 configuration can be saved with ‘pm2 save’.

Information about the process

Use nginx to act as a reverse proxy

The aspnet core webpage just runs on http://localhost:5020, nothing fancy there. Next we need to use nginx to act as a reverse proxy to make https://vwp.timdows.com/timelapse serving http://localhost:5020.

Running on localhost:5020

As an admin within DirectAdmin we can configure this.

Under Extra Features go to Custom HTTPD Configurations

[code]
location /timelapse/ {
proxy_pass http://localhost:5020/;

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
}
[/code]

Use the above code to make nginx serve the aspnet core webpage.

All done and time for beer ;.)