在CentOS 8 上 部署 .Net Core 应用程序

在Centos 8 上 部署 .Net Core 应用程序

                                                                          —— 记录篇


 

1、更新dnf 源

1 dnf update

2、安装 Asp.Net Core 运行时

1 dnf install aspnetcore-runtime-3.1

2.1、验证是否安装成功

1 dotnet --info

 

 

出现如上图所示就说明安装成功

3、安装Nginx

dnf -y install nginx

3.1、查看nginx版本

nginx -v

3.2、设置开机自启动

1 systemctl enable nginx

3.3、启动 nginx 服务

service nginx start

3.4、其他 相关 指令

1 # 卸载 
2 dnf remove nginx
3 # 停止 服务
4 service nginx stop
5 # 重启
6 service nginx restart
7 # 加载配置文件 
8 service nginx reload

4、MySql 安装

4.1、下载

 wget https://repo.mysql.com//mysql80-community-release-el8-1.noarch.rpm

4.2、使用rpm 安装 mysql 

 rpm -ivh mysql80-community-release-el8-1.noarch.rpm

4.3、dnf 安装 mysql 服务

dnf -y install mysql-server

4.4、设置开机自启动

systemctl enable mysqld.service

4.5、启动mysql

systemctl start mysqld.service

4.6、设置远程连接(可选)

因我时在某云上,所以需要设置我本地连接,如果是在自己虚拟器可跳过此步骤

4.6.1、进入 mysql 命令行

 

 4.6.2、更新 系统表(user)
update mysql.user set host="%" where user="root";

 

 

4.6.3、设置 root 密码
-- 切换数据库
use mysql;
-- 执行语句
ALTER  USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123';
-- 刷新修改后的权限
flush privileges;
-- 退出
exit;
4.6.4、测试

 

 

5、将应用程序发布后的包上传

5.1、创建文件夹(用于存放应用程序包)

mkdir -p /var/www/web

5.2、ftp 上传应用程序文件到 上一步创建的文件夹(/var/www/web/)中

6、Redis 安装(可选)

如果项目中没有用到 Redis 可以跳过此步骤

6.1、下载、解压、编译

 wget http://download.redis.io/releases/redis-6.0.6.tar.gz
 tar xzf redis-6.0.6.tar.gz
 cd redis-6.0.6
 dnf install tcl
 make

6.2、编译测试

make test

6.3、迁移到指定的目录(可选)

 1 mkdir -p /usr/local/soft/redis
 2 cd /usr/local/soft/redis/
 3 mkdir bin
 4 mkdir conf
 5 cd bin/
 6 cp /redis-6.0.6/src/redis-cli ./
 7 cp /redis-6.0.6/src/redis-server ./
 8 cd ../conf/
 9 cp /redis-6.0.6/redis.conf ./
10 # 配置 redis-server 的 配置文件为 /usr/local/soft/conf/redis.conf
11 /usr/local/soft/redis/bin/redis-server /usr/local/soft/redis/conf/redis.conf
12 # 检查端口是否在使用
13 netstat -anp | grep 6379

6.4、使用 systemd 方式守护 redis 进程

6.4.1、编辑 redis.service 文件
vim /lib/systemd/system/redis.service
  6.4.2、设置redis.service 内容

[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis_6379.pid
ExecStart=/usr/local/soft/redis/bin/redis-server /usr/local/soft/redis/conf/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

6.4.3、重载系统
systemctl daemon-reload
6.4.4、设置开机启动及其他指令
# 开机自启
systemctl enable redis
# 启动
systemctl start redis
# 查看状态
systemctl status redis
# 停止
systemctl stop redis

7、配置.Net Core 应用程序的守护进程

7.1、编辑 aspnetCore.service 文件

文件名 自定义,这里我起名为 aspnetCore.service

vim /lib/systemd/system/aspnetCore.service

7.2、编辑内容

[Unit]
Description=AspnetCoreDemo running on Centos8 

[Service]
# 应用程序所在的文件目录 WorkingDirectory
=/var/www/web/ ExecStart=/usr/bin/dotnet /var/www/web/Carefree.AspNetCoreDemo.dll Restart=always # 如果dotnet服务崩溃,10秒后重新启动服务 RestartSec=10 KillSignal=SIGINT SyslogIdentifier=AspNetCoreDemo User=root #Production:生产环境 Development:开发环境 Environment=ASPNETCORE_ENVIRONMENT=Development Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false [Install] WantedBy=multi-user.target

7.3、重载系统及设置开机启动

# 重载系统
systemctl daemon-reload
# 开机自启动
systemctl enable aspnetCore.service

8、Nginx 代理

8.1、编辑配置文件

vim /etc/nginx/conf.d/web.conf

8.2、编辑内容

server
{
   listen       80;
   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;
   }
}

8.3、编辑 nginx.conf 文件

 

 

8.4、验证配置文件是否正确及加载配置文件

# 验证配置文件
nginx -t
# 加载配置文件
nginx -s reload

至此我们的应用程序可正常访问了。如有何问题可与我联系,共同学习。

 

posted @ 2020-10-27 11:38  ZerekZhang  阅读(898)  评论(0编辑  收藏  举报