Centos8 下部署 ASP.net Core 程序

1、安装需要的SDK包,如果程序包含3.1版本,需要安装3.1的SDK。

sudo dnf install dotnet-sdk-5.0

dotnet --version

dotnet --list-runtimes

2、net core程序中带有图片验证码需要事先安装GDI的图形库

https://www.mono-project.com/download/stable/#download-lin-centos

yum install libgdiplus

1、Add the Mono repository to your system

CentOS/RHEL 8 (x86_64)

rpmkeys --import "http://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF"
su -c 'curl https://download.mono-project.com/repo/centos8-stable.repo | tee /etc/yum.repos.d/mono-centos8-stable.repo'

CentOS/RHEL 7 (x86_64)

rpmkeys --import "http://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF"
su -c 'curl https://download.mono-project.com/repo/centos7-stable.repo | tee /etc/yum.repos.d/mono-centos7-stable.repo'

CentOS/RHEL 6 (x86_64, i686)

rpm --import "http://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF"
su -c 'curl https://download.mono-project.com/repo/centos6-stable.repo | tee /etc/yum.repos.d/mono-centos6-stable.repo'


Ubuntu 20.04 (amd64, armhf, arm64, ppc64el)

sudo apt install gnupg ca-certificates
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
echo "deb https://download.mono-project.com/repo/ubuntu stable-focal main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list
sudo apt update

Ubuntu 18.04 (i386, amd64, armhf, arm64, ppc64el)

sudo apt install gnupg ca-certificates
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
echo "deb https://download.mono-project.com/repo/ubuntu stable-bionic main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list
sudo apt update

Ubuntu 16.04 (i386, amd64, armhf, arm64, ppc64el)

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
sudo apt install apt-transport-https ca-certificates
echo "deb https://download.mono-project.com/repo/ubuntu stable-xenial main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list
sudo apt update

2、Install Mono

dnf install mono-devel


sudo apt install mono-devel
https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-6.0


yum install nginx -y
systemctl status nginx
systemctl enable --now nginx
systemctl start nginx
server {
    listen        80;
    server_name   example.com *.example.com;
    location / {
        proxy_pass         http://127.0.0.1:5000;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
}

  

把启动命令写成服务(新测试可用不会报错),

/etc/systemd/system/Core.service

Linux的Unit配置及更改访问端口

增加一个Environment配置即可。Environment=ASPNETCORE_URLS=http://*:5123

[Unit]
Description=ZKEACMS

[Service]
WorkingDirectory=/root/cms
ExecStart=/usr/bin/dotnet /root/cms/ZKEACMS.WebHost.dll
Restart=always
RestartSec=10
SyslogIdentifier=zkeacms
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=ASPNETCORE_URLS=http://*:5123

[Install]
WantedBy=multi-user.target

官方给的服务配置文件,供参才考。试了多次都不能启动。

sudo nano /etc/systemd/system/kestrel-helloapp.service

[Unit]
Description=Example .NET Web API App running on Ubuntu

[Service]
WorkingDirectory=/var/www/helloapp
ExecStart=/usr/bin/dotnet /var/www/helloapp/helloapp.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

sudo systemctl enable kestrel-helloapp.service

sudo systemctl start kestrel-helloapp.service

sudo systemctl status kestrel-helloapp.service 

参考链接


https://huchengv5.github.io/post/%E5%A6%82%E4%BD%95%E5%B0%86Asp.net-Core%E7%AB%99%E7%82%B9%E9%83%A8%E7%BD%B2%E5%88%B0CentOS.html