本文从Linux小白的视角, 在CentOS上搭建一个ASP.NET Core Web应用程序; 在Linux上部署ASP.NET Core
程序有不少类似步骤教程,但是总感觉知其然不知其所以然,小白总感觉没有理清部署思路,胡子眉毛一把抓。

  本文给Linux新手呈现一个最小化的部署实例,循序渐进的讲解部署的思路, 最后文末提供相关的避坑资料、必备知识点。
  部署架构:
  首先明确dotnet程序是一个独立进程, 原本可不依赖反向代理服务器运行;

  第二明确Nginx反向代理服务器的作用;

  第三明确dotnet程序需要在Linux系统中以守护进程的方式运行,可使用 supervisor、systemd等方式。
 
1. 安装环境
sudo rpm -Uvh https://
packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm --
rpm是一种软件包管理方式,这里的微软软件包仓库以rpm包的形式提供,包含仓库配置和供发行版认证软件包的公钥,你可以理解为添加了一个nuget包仓库 sudo
yum update sudo yum install aspnetcore-runtime-2.2                             
-- 基于rpm包管理,能够从指定服务器自动下载rpm包并且安装,可自动处理依赖关系,并一次安装所有依赖软件包。
 

2. dotnet程序发布、测试

  - 使用VS项目右键发布到指定目录

       - 使用zip方式打包

       - 使用scp,SFTP工具上传到Linux服务器, 一般情况下拷贝到var目录
scp D:\production\eqidproxyServer.zip root@10.201.80.126:/var/www
--以下命令将zip包拷贝到 /var/www目录下
        - 在CentOS服务器上解压
unzip -d eqidproxyServer eqidproxyServer.zip
       -  执行dotnet EqidProxyServer.dll

 

3. 使用systemd将dotnet进程设置成Linux守护进程

   完成以上步骤,dotnet程序并不能在后台作为服务运行,Nginx虽然能作为反向代理服务器转发请求到dotnet进程,
但是并不具备管理dotnet进程的能力。

 下面使用 systemd来将dotnet进程设定为系统服务。

systemd是一个Linux的系统服务管理器,其作用是提供系统服务依赖管理 、实现系统初始化时服务的并行启动。
① 创建服务文件:vim /etc/systemd/system/kestrel-eqidproxyserver.service [Unit]
Description=EqidProxyServer deploy on centos [Service] WorkingDirectory=
/var/www/eqidproxyserver/eqidproxyServer ExecStart=/usr/bin/dotnet
/var/www/eqidproxyserver/eqidproxyServer/EqidProxyServer.dll Restart=always #
Restart service after10 seconds if the dotnet service crashes: RestartSec=10
TimeoutStopSec=90 KillSignal=SIGINT SyslogIdentifier=dotnet-example User=www-
data Environment=ASPNETCORE_ENVIRONMENT=Production Environment
=DOTNET_PRINT_TELEMETRY_MESSAGE=false [Install] WantedBy=multi-user.target
  注意:Linux 是大小写敏感的文件系统,设定ASPNETCORE_ENVIRONMENT=Production 会在配置文件中搜索如下配置文件:
appsettings.Production.json, 故配置和文件名需要留意匹配。

 
② 启用、启动服务 sudo systemctl enable kestrel-eqidproxyserver.service       // 启用服务
sudo systemctl start kestrel-eqidproxyserver.service        // 指定服务名启动 sudo
systemctl status kestrel-eqidproxyserver.service       // 验证服务状态  以下是验证服务状态的输出:
● kestrel-eqidproxyserver.service - EqidProxyServer deploy on centos Loaded:
loaded (/etc/systemd/system/kestrel-eqidproxyserver.service; enabled; vendor
preset: disabled) Active: active (running) since Thu2019-02-28 18:04:20 CST;
3min 2s ago Main PID:52859 (dotnet) Memory: 46.3M CGroup: /system.slice/kestrel-
eqidproxyserver.service └─52859 /usr/bin/dotnet
/var/www/eqidproxyserver/eqidproxyServer/EqidProxyServer.dll Feb 28 18:06:18
gs-server-5809 dotnet-eqidproxyserver[52859]: info:
Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Feb 28 18:06:18 gs-server-5809
dotnet-eqidproxyserver[52859]: Request finished in 136.6715ms 200 Feb 28 18:06:
23 gs-server-5809 dotnet-eqidproxyserver[52859]: info:
Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Feb 28 18:06:23 gs-server-5809
dotnet-eqidproxyserver[52859]: Request starting HTTP/1.1 GET http://127.0.0.1/
Feb28 18:06:23 gs-server-5809 dotnet-eqidproxyserver[52859]: info:
Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Feb 28 18:06:23 gs-server-5809
dotnet-eqidproxyserver[52859]: Request finished in 3.5599ms 200 Feb 28 18:06:32
gs-server-5809 dotnet-eqidproxyserver[52859]: info:
Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Feb 28 18:06:32 gs-server-5809
dotnet-eqidproxyserver[52859]: Request starting HTTP/1.1 GET http://
10.201.80.126/ Feb 28 18:06:32 gs-server-5809 dotnet-eqidproxyserver[52859]:
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Feb 28 18:06:32
gs-server-5809 dotnet-eqidproxyserver[52859]: Request finished in 1.3498ms 200
 

4. 搭配Nginx部署web程序
  ① 安装Nginx
    -  sudo yum install nginx      【首次安装需要显式启动: sudo service nginx start】
 CentOS安装的nginx并没有作为守护进程运行,执行sudo systemctl enable nginx 启用nginx守护进程: 
https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-centos-7

<https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-centos-7>
 Ubuntu 中使用 apt-get 安装的nginx, 安装器会创建systemd init
script,也就是说nginx会随着系统启动作为守护程序运行。
  -    在终端使用curl localhost 测试nginx
  ② 配置Nginx作为反向代理服务器
   - 修改/etc/nginx/nginx.conf 文件: sudo vi /etc/nginx/nginx.conf

   - 配置nginx在80端口将请求转发到Kestrel服务器(localhost:5000)
server { listen 80; server_name default_website; root /usr/share/nginx/html; #
Load configuration filesfor the default server block. include
/etc/nginx/default.d/*.conf; location / { proxy_pass http://localhost: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; }
error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504
/50x.html; location = /50x.html { } }
  一旦nginx配置完成,可以使用sudo nginx -t 测试配置文件的语法;

  如果配置文件合法,就可以重启nginx: sudo nginx -s reload

 

  完成以上步骤之后,现在已经可以从127.0.0.1、127.0.0.1:5000、 服务器IP访问web程序。
 
5.查看进程日志

     使用systemd方式管理进程,所有事件和进程都会记录到某个集中日志,该集中日志包含所有被systend管理的服务和进程的日志。

这个日志功能相当于windows服务器中的事件查看器。

 

查看刚才建立的服务日志, 可使用下面的命令:
sudo journalctl -fu kestrel-eqidproxysever.service
 时间过滤:
sudo journalctl -fu kestrel-eqidproxysever.service --since "2018-11-18" --until
"2019-03-28 04:00"
  

  部分内容参考:
https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?viw=aspnetcore-2.2

<https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?viw=aspnetcore-2.2>

  rpm包源:
https://docs.microsoft.com/en-us/windows-server/administration/linux-package-repository-for-microsoft-software

<https://docs.microsoft.com/en-us/windows-server/administration/linux-package-repository-for-microsoft-software>

  rpm/yum区别: https://zhuanlan.zhihu.com/p/27724520
<https://zhuanlan.zhihu.com/p/27724520>

  Linux进程管理:https://linux.cn/article-3801-1.html
<https://linux.cn/article-3801-1.html>

 

--------------如有问题请大胆斧正,不吝赐教;如果你觉得文章对你有价值,请【推荐】或【关注】,蟹蟹--------------~~。。~~
--------------

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信