欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CentOS7.6 部署.netcore3.1

程序员文章站 2023-12-30 15:55:58
...

前置工作
安装宝塔,MYSQL,FTP等软件
宝塔官网

目录

  1. 安装.netcore 3.1 SDK
  2. 配置Nginx反向代理
    2.1 运行.netcore项目
    2.2 https无法访问
  3. 配置Supervisor守护进程

安装.netcore 3.1 SDK

# 添加微软官方源
rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
# 安装SDK
yum install dotnet-sdk-3.1
# 验证安装是否成功
dotnet --info

配置Nginx反向代理

/www/server/panel/vhost/nginx/网站名称.conf

server
{
    listen 80;
    server_name 网址;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/网站根目录;
    
    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
    #error_page 404/404.html;
    #SSL-END
    
    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    #ERROR-PAGE-END
    
    #PHP-INFO-START  PHP引用配置,可以注释或修改
    include enable-php-00.conf;
    #PHP-INFO-END
    
    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
    include /www/server/panel/vhost/rewrite/对应文件名.conf;
    #REWRITE-END
    
    location / {
        proxy_pass http://localhost:5000;  #把80端口的访问反向代理到5000端口(你程序的启动端口),请根据实际情况修改自己的端口
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
        proxy_set_header Host $http_host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        
        # 客户端真实ip
        proxy_set_header X-Real-IP $remote_addr;
    }
    
    #禁止访问的文件或目录
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
    
    #一键申请SSL证书验证目录相关设置
    location ~ \.well-known{
        allow all;
    }
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        proxy_pass http://127.0.0.1:5000;
        expires      30d;
        error_log off;
        access_log /dev/null;
    }

    location ~ .*\.(js|css)?$
    {
        proxy_pass http://127.0.0.1:5000;
        expires      12h;
        error_log off;
        access_log /dev/null;
    }
	access_log  /www/wwwlogs/网址.log;
    error_log  /www/wwwlogs/网址.error.log;
}

/www/server/panel/vhost/nginx/网站名称.ssl.conf

server
{
    listen       443 ssl;
    server_name  网址;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/网站根目录;
    ssl_certificate      /www/wwwroot/网站根目录/SSL/证书.crt;
    ssl_certificate_key  /www/wwwroot/网站根目录/SSL/证书.key;
    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;
    
    location / {
        proxy_pass http://localhost:5000;  #把80端口的访问反向代理到5000端口(你程序的启动端口),请根据实际情况修改自己的端口
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
        proxy_set_header Host $http_host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        
        # 客户端真实ip
        proxy_set_header X-Real-IP $remote_addr;
    }
    
    #禁止访问的文件或目录
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
    
    #一键申请SSL证书验证目录相关设置
    location ~ \.well-known{
        allow all;
    }
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        proxy_pass http://127.0.0.1:5000;
        expires      30d;
        error_log off;
        access_log /dev/null;
    }

    location ~ .*\.(js|css)?$
    {
        proxy_pass http://127.0.0.1:5000;
        expires      12h;
        error_log off;
        access_log /dev/null;
    }
	access_log  /www/wwwlogs/网址.log;
    error_log  /www/wwwlogs/网址.error.log;
}

重启nginx

nginx -s reload

运行.netcore项目

cd进入网站根目录

dotnet 项目名称.dll
#显示以下内容即成功运行
Hosting environment: Production
Content root path: 网站根目录
Now listening on: http://localhost:5000
Now listening on: http://localhost:5001
Application started. Press Ctrl+C to shut down.

https 无法访问,可能是443端口未开放

#开放443/80端口
firewall-cmd --zone=public --add-port=443/tcp --permanent
#重启防火墙
firewall-cmd --reload

配置Supervisor守护进程

安装supervisor

yum install supervisor
#启动服务
supervisord -c /etc/supervisord.conf

进入etc/supervisord.d文件夹创建 xxx.ini文件

[program:项目名称]
command=dotnet 项目名称.dll
directory=/www/wwwroot/网站根目录
autostart=true
autorestart=true
startretries=5
startsecs=1
user=root
priority=999
stderr_logfile=/var/log/AbpMPA.err.log
stdout_logfile=/var/log/AbpMPA.out.log
environment=ASPNETCORE_ENVIRONMENT=Production
stopsignal=INT

刷新supervisord配置

supervisorctl reload

查看supervisorctl守护项目状态

supervisorctl
.

网站成功部署

上一篇:

下一篇: