发布 .NET Core 程序到Linux

前提条件

  • 开发环境:Windows 或其他系统,安装 .NET SDK(例如 .NET 8)。
  • 目标环境:Linux 服务器(以 Ubuntu 20.04/22.04 为例),有 SSH 访问权限。

步骤 1:发布 .NET Core 程序(依赖框架)

依赖框架部署仅包含程序和第三方依赖,不包含 .NET 运行时。

  1. 打开终端(Windows PowerShell)
    cd E:\work\myapp
  2. 发布程序
    dotnet publish -c Release -r linux-x64 --self-contained false -o ./publish
    • -c Release:优化发布版本。
    • -r linux-x64:目标运行时为 Linux 64 位(确保兼容性)。
    • --self-contained false:依赖框架模式,不包含运行时。
    • -o ./publish:输出到 publish 目录。
  3. 检查输出
    • 进入 publish 目录:
    • 你会看到:
      • Lims.AuthServer.dll(主程序集)。
      • 其他 .dll 文件和 Lims.AuthServer(可执行文件,调用 dotnet 运行)。

步骤 2:传输到 Linux

将发布文件上传到 Linux 服务器。

  1. 使用 SCP
    scp -r .\publish\* user@linux-server:/home/user/myapp
    • 替换 user@linux-server 为你的用户名和 IP。
    • /home/user/myapp 是目标目录。
  2. 登录 Linux(SSH)
     
    ssh user@linux-server

步骤 3:安装 .NET 运行时(Linux)

目标 Linux 需要安装 .NET 运行时,因为依赖框架部署不包含它。以 Ubuntu 为例:

  1. 更新包索引
     
    sudo apt update
  2. 安装 .NET 运行时
     
    sudo apt install -y dotnet-runtime-8.0
    • 如果你的程序基于其他版本(例如 .NET 6),改为 dotnet-runtime-6.0。
  3. 验证安装
     
    dotnet --version
    • 应返回类似 8.0.xxx 的版本号。

其他发行版

  • CentOS/RHEL:sudo yum install dotnet-runtime-8.0

步骤 4:运行程序并测试

在 Linux 上运行程序,确保正常工作。

  1. 导航到部署目录
     
    cd /home/user/myapp
  2. 运行程序
     
    dotnet Lims.AuthServer.dll
    • 默认监听 http://localhost:5000。
    • Ctrl+C 停止测试。
  3. 本地测试
     
    curl http://localhost:5000
    • 确认返回响应。

步骤 5:配置为系统服务

让程序在后台运行并开机自启。

  1. 创建服务文件
     
    sudo nano /etc/systemd/system/myapp.service
    输入以下内容:
    [Unit]
    Description=My .NET Core Application
    After=network.target
    
    [Service]
    ExecStart=/usr/bin/dotnet /home/user/myapp/Lims.AuthServer.dll
    WorkingDirectory=/home/user/myapp
    Restart=always
    User=user
    Environment="ASPNETCORE_ENVIRONMENT=Production"
    
    [Install]
    WantedBy=multi-user.target

     

    • ExecStart 使用 dotnet 运行 .dll 文件。
    • 替换 User=user 为你的实际用户名。
  2. 保存并退出
    • Ctrl+O,回车保存,Ctrl+X 退出。
  3. 启用并启动服务
     
    sudo systemctl enable myapp.service sudo systemctl start myapp.service
  4. 检查状态
     
    sudo systemctl status myapp.service
  5. 查看日志
     
    journalctl -u myapp.service -f

步骤 6:安装并配置 Nginx 反向代理

使用 Nginx 转发外部请求到 .NET Core 程序。

  1. 安装 Nginx
     
    sudo apt update sudo apt install -y nginx
  2. 创建 Nginx 配置文件
     
    sudo nano /etc/nginx/sites-available/myapp
    输入以下内容:
    server {
        listen 80;
        server_name your-domain.com; # 替换为域名或 IP
    
        location / {
            proxy_pass http://localhost:5000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
    • 如果无域名,用 IP(例如 server_name 192.168.1.100;)。
  3. 启用配置
     
    sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
  4. 测试配置
     
    sudo nginx -t
  5. 重启 Nginx
     
    sudo systemctl restart nginx

步骤 7:开放端口

确保外部访问。

  1. 配置防火墙
     
    sudo ufw allow 80 sudo ufw status
  2. 测试访问
     
    curl http://your-domain.com
    或用浏览器访问 http://linux-server-ip。

步骤 8:更新程序

当你修改程序(例如更新 DLL):

  1. 发布新版本
    powershell
    dotnet publish -c Release -r linux-x64 --self-contained false -o ./publish
  2. 传输文件
    powershell
    scp -r .\publish\* user@linux-server:/home/user/myapp
  3. 重启服务
     
    sudo systemctl restart myapp.service
  4. 验证
     
    curl http://your-domain.com

常见问题

  1. 运行时未安装
    • 如果 dotnet 命令失败,确认运行时:
       
      sudo apt install -y dotnet-runtime-8.0
  2. 端口调整
    • 修改监听端口(例如 8080):
      json
      // appsettings.json "Urls": "http://localhost:8080"
    • 更新 Nginx 的 proxy_pass http://localhost:8080。
  3. HTTPS
    • 添加 SSL:
       
      sudo apt install -y certbot python3-certbot-nginx sudo certbot --nginx -d your-domain.com
posted @ 2025-03-13 21:59  Hi同学  阅读(492)  评论(0)    收藏  举报