Linux(Centos 8.2)配置asp.net core 6.0运行环境(不含数据库)

今年双十一在腾讯云上买了一个2CPU 4GB 8M的轻量云服务器,70元一年,香喷喷的。购买服务器后,我用CentOS 8.2初始化了系统,要用就用崭新的!

昨天晚上微软也发布了.NET 6的正式版,于是,Linux(Centos 8.2)搭配Nginx,运行asp.net core 6.0的环境配置教程如下(不含数据库):

第一步:运行命令dnf update -y,对系统基础包进行升级。
这里为什么不用yum命令?因为dnf是新一代的rpm软件包管理器,它取代了yum,已经预装在 CentOS 8中了。要用就用崭新的!
约莫一个打飞机的功夫,就更新完毕。

.

第二步:安装Nginx
1,运行命令dnf info nginx,查看一下软件库中Nginx的版本,你可能会看到1.14.1版本,不够新,我们要安装最新的稳定版本,我们需要自定义Ngnix包源。
2,运行命令vim /etc/yum.repos.d/nginx.repo,创建一个nginx.repo文件。
3,进入vim编辑界面后,先按i键,切换到编辑模式,复制下面的内容粘贴进去

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

然后按ESC键回到查看模式,再输入:wq 然后回车
4,执行nginx安装命令dnf install nginx -y,安装完成后,你应该能看到版本是1.20.1
5,启动Nginx服务命令systemctl start nginx
然后浏览器访问服务器IP,应该就可以看到Nginx的默认页,如果你访问不起,看看云服务器平台是否给该服务器开放了80端口。

第三步:安装.NET 6运行时
1,执行命令dnf info dotnet,查看软件包里.NET的版本,我看到的是5.0.208,微软昨天发布了.NET 6,显然这里还没更新。
有可能你看到这篇教程时,查看到的是6.0的版本,那么直接运行dnf install dotnet直接安装。
2,手工去找.NET 6运行时安装包地址。打开https://dotnet.microsoft.com/download/dotnet/6.0,找到ASP.NET Core Runtime 6.0.0下,Linux下,Binaries下的X64,直接会新窗口下载,不让他下载,直接获取下载地址,我获取到的是:
https://download.visualstudio.microsoft.com/download/pr/a8dd1c9d-1a47-4135-8ad8-7091ff6bbe1e/6af53c3eee71038248937daf4599f06a/aspnetcore-runtime-6.0.0-linux-x64.tar.gz
3,运行命令curl -Lo dotnet.tar.gz https://download.visualstudio.microsoft.com/download/pr/a8dd1c9d-1a47-4135-8ad8-7091ff6bbe1e/6af53c3eee71038248937daf4599f06a/aspnetcore-runtime-6.0.0-linux-x64.tar.gz
4,运行命令mkdir dotnet
5,运行命令tar -C dotnet -xf dotnet.tar.gz
6,运行命令export DOTNET_ROOT=~/dotnet
7,运行命令export PATH=$PATH:~/dotnet
8,运行命令dotnet --info,看看是不是成功安装了6.0.0

第四步:配置Linux服务
1,在服务器根目录(/)下创建一个名 /app/myweb 的目录,用来存放我们的发布文件
2,最好先把asp.net core 6测试项目发布后,上传到上面的目录中
3,为使每次服务器重启能自动启动我们的应用程序和监测应用程序的运行状态,我们需要把我们的应用配置为 Linux 服务。运行命令vim /etc/systemd/system/myweb.service
复制下面的内容粘贴进去,然后退出保存(方法请参考前面的描述)

[Unit]
Description=MyWeb

[Service]
WorkingDirectory=/app/myweb
ExecStart=/root/dotnet/dotnet /app/myweb/myweb.Web.dll --urls=http://localhost:3000
Restart=always
RestartSec=10
SyslogIdentifier=myweb
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

注意,ExecStart一行,/app/myweb/myweb.Web.dll换成你自己项目的dll文件
4,执行systemctl enable myweb.service
5,执行systemctl start myweb.service
6,执行systemctl status myweb.service,若看到绿色的active(running)字样说明服务正常启动了。

第五步:配置Nginx站点
1,创建vim /etc/nginx/conf.d/MyWeb.conf

server {
    listen        80;
    server_name   localhost;
    location / {
        proxy_pass         http://127.0.0.1:3000;
        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;
    }
}

2,将/etc/nginx/conf.d/ 中的 default 文件删除或改为其它端口号,不然会冲突。
3,执行nginx -s reload

用IP打开,就能看到你的测试项目了。