Deploying Blazor to Linux Server
There are a couple reasons to use a linux server to host your blazor app.
- Cost: we can use low cost vms running ubuntu minnimal to get the most for our money.
- Available tooling to configure a secure website with Lets Encrypt for free SSL certificate.
- Flexibility: Linux servers offer more flexibility in terms of configuration and customization.
1. Setting up the Server
Go to your favorite cloud provider and create a new virtual machine. I recommend using Ubuntu 22.04 LTS for stability and support.
In my case I used Azure to create a new VM with the following settings:
- OS: Ubuntu 22.04 LTS
- Size: Standard B1s (1 vCPU, 1 GiB RAM)
- SSH Key: Used Username and password, some prefer the key but be sure to keep it in a secure place if you go that route
- Networking: Open ports 22, 80 and 443 for SSH, HTTP and HTTPS traffic
Once the VM is created, connect to it using SSH:
ssh username@your-vm-ip-address
2. Installing .NET SDK
To run a Blazor app, you need to have the .NET SDK installed on your server. You can install it using the following commands:
If you are using 9.0 change the 8 to a 9
wget https://dot.net/v1/dotnet-install.sh
chmod +x dotnet-install.sh
./dotnet-install.sh --channel 8.0
export PATH="$HOME/.dotnet:$PATH"
Verify the installation by checking the .NET version:
dotnet --version
3. Deploying Your Blazor App
Now that you have the .NET SDK installed, you can deploy your Blazor app. First, publish your Blazor app locally:
dotnet publish -c Release -o ./publish
Next, copy the published files to your server using SCP or any other file transfer method:
scp -r ./publish username@your-vm-ip-address:~/myapp
4. Create Service File for Web app
To run your Blazor app as a service, create a new service file (You can name your service anything you want in this case its myapp):
sudo nano /etc/systemd/system/myapp.service
Add the following content to the service file:
[Unit]
Description=My Blazor App
After=network.target
[Service]
ExecStart=/home/username/.dotnet/dotnet /home/username/myapp/YourBlazorApp.dll
WorkingDirectory=/home/username/myapp
Restart=always
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target
Replace `username` with your actual username and `YourBlazorApp.dll` with the name of your Blazor app's DLL file.
Save and exit the file, then enable and start the service:
sudo systemctl enable myapp
sudo systemctl start myapp
5. Setting Up Nginx as a Reverse Proxy
To serve your Blazor app over HTTP/HTTPS, you can use Nginx as a reverse proxy. Install Nginx using the following command:
sudo apt install nginx
Once Nginx is installed, create a new configuration file for your Blazor app:
sudo nano /etc/nginx/sites-available/default
Add the following content to the configuration file:
server {
listen 80;
server_name your-domain.com; # Replace with your domain or IP address
location / {
proxy_pass http://localhost:5000; # Assuming your Blazor app runs on port 5000
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Replace `your-domain.com` with your actual domain or IP address.
Save and exit the file, then test the Nginx configuration:
sudo nginx -t
If the configuration is valid, restart Nginx:
sudo systemctl restart nginx
6. Setting Up HTTPS with Let's Encrypt
To secure your Blazor app with HTTPS, you can use Let's Encrypt to obtain a free SSL certificate. Install Certbot using the following command:
sudo apt install certbot python3-certbot-nginx
Once Certbot is installed, run the following command to obtain and install the SSL certificate:
sudo certbot --nginx -d your-domain.com -d www.your-domain.com
Follow the prompts to complete the certificate installation. Certbot will automatically configure Nginx to use the SSL certificate.
7. Testing Your Blazor App
Now that everything is set up, you can test your Blazor app by navigating to your domain or IP address in a web browser. You should see your Blazor app running securely over HTTPS.
8. Point DNS A Record to Your Server
To make your Blazor app accessible via your domain name, update your domain's DNS settings. Set an A record pointing to your server's public IP address.
- Log in to your domain registrar's dashboard.
- Find the DNS management section.
- Add or update an A record for your domain (e.g.,
your-domain.com
) to point to your server's IP address.
DNS changes may take some time to propagate. Once complete, your domain will direct visitors to your Blazor app hosted on your Linux server.
9. When Making Updates
When you make updates to your Blazor app, you can follow these steps:
- Publish your Blazor app locally again using
dotnet publish -c Release -o ./publish
. - Copy the updated files to your server using SCP or any other file transfer method.
scp -r ./publish username@your-vm-ip-address:~/myapp
- Restart your Blazor app service with
sudo systemctl restart myapp
. - If you made changes to the Nginx configuration, test it with
sudo nginx -t
and restart Nginx if needed.
That's it! You have successfully deployed your Blazor app to a Linux server.
If you are having trouble ask ChatGPT for help :), if you want to set up a pipeline in github checkout this post