Deploy Piranha CMS to ubuntu

Piranha CMS is a open source CMS based on Asp.Net core. It means we can host it on a Linux based system or any other system that support dotnet core.



We need the following stuff:

Dotnet core 3.1

Follow this page to install the dotnet core sdk. 

Piranha CMS

Follow this page to setup Piranha CMS. You can setup the CMS project at your local first and deploy to the server when everything is ready. 

Apache2

Ubuntu have Apache 2 install by default, so no need to install seperately. Apache2 will be used as reverse proxy server. Beside from this there are servals other options we can use, such as Nginx, Haproxy. You even can use Kestrel directly if you prefer.

Database

Piranha CMS is using SQL Lite as database by default, it's enough for a personal site.

1. Setup a new website in Apache 2

<VirtualHost *:80> 
RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
ProxyPreserveHost On   
ProxyPass / http://127.0.0.1:5010/   
ProxyPassReverse / http://127.0.0.1:5010/
ServerAdmin hi@achievecreative.com
ServerName achievecreative.com
ServerAlias www.achievecreative.com

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

The following are 2 mandatory modules we need: 

HTTP and HTTP_Proxy

Enable the modules:

sudo a2enmod http
sudo a2enmod http_proxy

After enable the moules, restart the Apache 2:

sudo /etc/init.d/apache2 restart


2. Modify the Project

Kerstrel is the real web server we used to host Asp.Net core project in Linux and we use Apache 2 in front of it. We need to let Kerstrel know about this.

Open the Startup.cs file, find Configure method, add the following code:

app.UseForwardedHeaders(new ForwardedHeadersOptions(){
ForwardedHeaders=ForwardedHeaders.XForwardedFor|ForwardedHeaders.XForwardedProto
});


3. Create a deployment package

dotnet publish --configuration Release


4. Copy everything in publish folder to your server. Be sure to copy the database file piranha.blog.db too.

5. Start Kerstrel to receive the request from Apache 2

dotnet blog.dll --urls http://localhost:5010

Kerstrel use 5000 as default port. We can change it by using urls parameter. The dll name probably different, it actually is your CMS project name.

Optional

Create a service to start the Kesterl server automatically

sudo nano /etc/systemd/system/kerstel-blog.service

Following is the service file example:

[Unit]
Description = Achievecreative Blogs

[Service]
WorkingDirectory=/var/www/achievecreative
ExecStart=/usr/bin/dotnet /var/www/achievecreative/blog.dll --urls http://localhost:5010
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User={replace with your user name}
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

Enable and start the service:

sudo systemctl enable /etc/systemd/system/kerstel-blog.service
sudo systemctl start kerstel-blog.service