Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Deploying an ASP.NET Core 7 App on CentOS 7.9 with aaPanel

Tech May 12 2

Switch to the root account before anything else:

sudo -i
  1. Install aaPanel 8.x

# CentOS 7/8
yum install -y wget \
  && wget -O install.sh http://download.bt.cn/install/install_6.0.sh \
  && bash install.sh

# Ubuntu / Deepin
wget -O install.sh https://download.bt.cn/install/install-ubuntu_6.0.sh \
  && sudo bash install.sh

After the script finishes, note the login URL, username and password.

  1. Add Microsoft package feed

# CentOS 7
sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm

# CentOS 8
sudo rpm -Uvh https://packages.microsoft.com/config/centos/8/packages-microsoft-prod.rpm

If you see a conflict error, force the install:

sudo rpm -Uvh --nodeps --force packages-microsoft-prod.rpm
  1. Install .NET 7 runtime (optional)

# CentOS
sudo yum -y install aspnetcore-runtime-7.0

# Ubuntu / Deepin
sudo apt-get update \
  && sudo apt-get install -y aspnetcore-runtime-7.0

Verify:

dotnet --info
  1. Publish the applicaiton

  • Self-contained: no runtime needed on the server, larger package.
  • Framework-dependent: smaller package, runtime must be installed (step 3).
  1. Upload & test locally

# 1. Upload publish folder to /www/wwwroot/myApp
cd /www/wwwroot/myApp

# 2a. Framework-dependent
dotnet MyApp.dll

# 2b. Self-contained single-file
yum install -y icu
chmod +x ./MyApp
./MyApp

You should see Now listening on: http://localhost:5000.

  1. Configure Nginx reverse proxy

  1. Open aaPanel → Websites → your site → Reverse proxy.
  2. Add a proxy rule: ``` Name: dotnet_backend Target URL: http://localhost:5000 Send domain: $host
    
    
  3. Keep the app alive with Supervisor

# Install
yum install -y supervisor
systemctl enable supervisord --now

Create /etc/supervisord.d/myApp.ini:

[program:myApp]
command=/usr/bin/dotnet /www/wwwroot/myApp/MyApp.dll   # or ./MyApp for self-contained
directory=/www/wwwroot/myApp
user=root
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisor/myApp.log
stderr_logfile=/var/log/supervisor/myApp_error.log
supervisorctl reread
supervisorctl update
supervisorctl start myApp
  1. SPA static-file issues

If you see:

System.InvalidOperationException: The SPA default page middleware could not return the default page '/index.html'

Upload the built front-end to ClientApp/dist and adjust Startup.cs:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseStaticFiles();

    var spaDist = new StaticFileOptions
    {
        FileProvider = new PhysicalFileProvider(
            Path.Combine(env.ContentRootPath, "ClientApp/dist"))
    };
    app.UseSpaStaticFiles(spaDist);

    app.UseRouting();
    app.UseAuthentication();
    app.UseAuthorization();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });

    app.UseSpa(spa =>
    {
        spa.Options.DefaultPageStaticFileOptions = spaDist;
    });
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.