Skip to content

Instantly share code, notes, and snippets.

@shawty
Created September 4, 2021 11:39
Show Gist options
  • Save shawty/449e39362f27be4a7e36d07bfda09518 to your computer and use it in GitHub Desktop.
Save shawty/449e39362f27be4a7e36d07bfda09518 to your computer and use it in GitHub Desktop.
Linux Bash Provisioning Script to deploy a Blazor Server app to a Linux LXC container. #linux #lxd #container #dotnet #blazor
#! /bin/bash
#Container is a new instance, so first thing we do is update the base software
apt update
apt upgrade -y
#Then we add the base tools we require along with the apache webserver
apt install mc -y
apt install gnupg -y
apt install apache2 -y
apt install git -y
#Next we install the dotnet(cre) rutimes we want (In this case 3.11 & 5)
wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
apt update
apt install apt-transport-https -y
apt update
apt install dotnet-sdk-3.1 -y
apt install dotnet-sdk-5.0 -y
#Once the software is added, we enable the "proxy" modules on the webserver and reload it
a2enmod proxy*
systemctl restart apache2
#Then we clone our blazor app git repo, and build it
git clone http://USER:PASSWORD@GITSEREVR/BasicBlazorServerApp.git
cd BasicBlazorServerApp
dotnet restore
dotnet build
dotnet publish -c Release -o pub
#Once we have a release published build, we create a folder in the web server root for the app
mkdir /var/www/apps
mkdir /var/www/apps/BasicBlazorApp
#And we copy the published app to it
mv ./pub/* /var/www/apps/BasicBlazorApp
#We set the app and all it's files to match the user of the web-server
chown -R www-data:www-data /var/www/apps
#Now we use a "Linux Inline Here Doc" to define the config file for our apache webserver
#and write it to the apache config folder. This config basically sends ALL inbound port 80
#traffic directly to our blazor app
cat > /etc/apache2/sites-available/000-default.conf << EOFAPACHE
<VirtualHost *:80>
ServerAdmin admin@BasicBlazorServerApp.com
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ProxyPass / http://localhost:5000/
ProxyPassReverse / http://localhost:5000/
</VirtualHost>
EOFAPACHE
#We use another "Here Doc" to define a systemd service control file, systemd is very much like
#the windows service control manager, the control file below tells the linux instance how to
#run our app as a service in the operating system, and what it's dependencies are.
cat > /etc/systemd/system/basicblazorserverapp.service << EOFSYSD
[Unit]
Description=Basic Blazor Server App
[Service]
Type=simple
User=www-data
Group=www-data
NoNewPrivileges=true
WorkingDirectory=/var/www/apps/BasicBlazorApp
ExecStart=/var/www/apps/BasicBlazorApp/BasicBlazorServerApp
Restart=always
StandardOutput=append:/var/log/basicblazorserverapp.stdout.log
StandardError=append:/var/log/basicblazorserverapp.stderr.log
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=myapp
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=ASPNETCORE_URLS=http://localhost:5000/
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target
EOFSYSD
#Finally we stop, then restart the webserver so it sees to proxy config to our web app
service apache2 stop
service apache2 start
#Then we make the OS system control system aware of our new service control file
systemctl daemon-reload
systemctl enable basicblazorserverapp
#and start our blazor app running.
service basicblazorserverapp start
@shawty
Copy link
Author

shawty commented Sep 4, 2021

Provisioning script as used in the following video:

https://youtu.be/W5sRN7l8d1Q

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment