前言
最近公司的项目打算移植到. Net Core 平台, 所以调研了一下. Net Core 在 Linux 下的安装部署. 本篇文章会一步步的描述从安装到配置到部署的全部过程. 在文章的结构和内容里, 笔者借鉴了很多其他博文的内容, 但感觉其他博文中都只是实现了一部分或者没有将配置内容写全. 笔者做的是整理一下自己的实际部署过程.
目录
1. 准备工作
2. 创建一个非 root 用户
3. 更新服务器
4. 安装. Net Core
5. 创建 Demo
6. 守护进程部署
7. Nginx 反向代理
8. 安装 Docker
9. 在 Docker 中部署. Net Core 项目
步骤
1. 准备工作
一台 Linux 服务器: 笔者用的是 VMWare 的虚拟机模拟的环境, 有条件的朋友可以使用真实服务器作为部署环境
Linux 终端: 笔者用的是 MobaXterm
2. 创建一个非 root 用户
$ sudo adduser docker_user
$ sudo usermod -aG sudo docker_user
$ exit
3. 更新服务器
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get dist-upgrade
$ sudo reboot
4. 安装. Net Core
我们要运行. Net Core 的项目, 自然要安装. Net Core 的环境啦. 这里微软提供了大部分 Linux 版本的安装方案, 我使用的是 Ubuntu16.04, 所以以下代码是针对这个系统版本的, 如果你使用的是其他版本可以参考( https://docs.microsoft.com/en-us/dotnet/core/linux-prerequisites?tabs=netcore2x )
- // 注册微软产品 Key 为被信任的
- $ curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor> microsoft.gpg
$ sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
- // 设置所需要的版本主机包
- $ sudo sh -c 'echo"deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-xenial-prod xenial main"> /etc/apt/sources.list.d/dotnetdev.list'
- // APT 支持 Https
$ sudo apt-get install apt-transport-https
$ sudo apt-get update
// 安装. Net Core SDK
$ sudo apt-get install dotnet-sdk-2.0.3
- // 检查安装是否成功
- $ dotnet --version
5. 创建 Demo
因为是 Demo 项目, 所以就在用户根目录下创建一个文件夹, 将该文件夹作为项目的根目录.
5.1 创建项目
- $ cd ~/
- $ mkdir helloworld
- $ cd helloworld
$ dotnet new mvc
5.2 发布项目
$ dotnet restore
$ dotnet publish -c Release
5.3 运行发布好的程序
通过上面的截图我们发现, 发布完的程序和在 PC 平台上的差不多, 是在 bin/Release 的目录下. 我们先移动到这个目录, 然后用 "dotnet" 命令运行这个 Demo
- $ cd bin/Release/netcoreapp2.0/publish/
- $ dotnet helloworld.dll
执行命令以后, 终端会如上图所示. 这个时候就说明程序已经跑起来了. 倒数第二行则说明了, 程序默认的端口号是 5000. 这个时候我们就可以通过服务器 IP + 端口号 5000 访问这个网站了.(注: 如果是外网访问, 需要在防火墙打开 5000 端口的权限. 这个就不在本文中扩展了.)
笔者是通过开启另一个终端来测试程序是否正常的, 结果如下:
- // 通过 wget 请求网站
- $ wget http://localhost:5000
6. 守护进程部署
走到这一步, 有不少同学就发现了. 现在的程序是需要我们始终打开终端才能保证网站可以被访问的, 那接下来我们要做的就是将这个网站部署成一个自定义服务. 让网站可以在后台进程中运行.
6.1 准备运行目录
按照 Linux 部署习惯, 我们在 var 目录下创建一个 www 文件夹, 并将刚才发布的 Demo 放到该目录下
$ cd /var
$ sudo mkdir www
$ cd www
$ sudo mkdir helloworld
$ cd helloworld
$ sudo cp -r ~/helloworld/bin/Release/netcoreapp2.0/publish/* .
6.2 创建服务配置文件
$ cd /etc/systemd/system/
$ sudo touch dotnet-helloworld.service
6.3 编写服务配置文件
$ sudo vi dotnet-helloworld.service
先通过 vi 进入该文件(注: vi 是 Linux 下最常用的文本编辑器之一, 如果对 vi 不了解, 可以自行百度, 这里就不扩展了). 进入编辑后, 将以下代码写入配置文件
- [Unit]
- Description=DotNet Core HelloWorld Running on Ubuntu
- [Service]
- WorkingDirectory=/var/www/helloworld // 工作目录, 这里我们写的是刚才创建的网站工作目录
- ExecStart=/usr/bin/dotnet /var/www/helloworld/helloworld.dll // 服务的实际执行命令
- Restart=always
- RestartSec=10
- SyslogIdentifier=dotnet-example
- User=docker_user // 已 docker_user 的身份启动
- Environment=ASPNETCORE_ENVIRONMENT=Production
- Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
- [Install]
- WantedBy=multi-user.target
上面这段配置文件我是参考微软官方的部署文档写的(文档链接 https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-2.1&tabs=aspnetcore2x )
6.4 启动服务
$ sudo systemctl enable dotnet-helloworld.service
$ sudo systemctl start dotnet-helloworld.service
6.5 检查服务状态
$ sudo systemctl status dotnet-helloworld.service
当服务启动后, 运行 status 命令查看服务状态时, 会返回服务执行的日志, 终端会显示类似于下图的代码.
这段代码和我们之前直接用命令行启动 dotnet 项目是相似, 这也就说明我们的服务部署成功了. 我们再使用 wget 验证, 同样可以访问这个网站了.
7. Nginx 反向代理
做到这里,.Net Core 项目的部署已经差不多了. 接下来我们就通过 Nginx 进行反向代理.
7.1 安装 Nginx
// 安装命令
$ sudo apt-get install nginx
- // 完成安装后检查安装是否成功
- $ nginx -v
7.2 配置反向代理
// 移动到 Nginx 配置文件夹
$ cd /etc/nginx/conf.d/
// 创建代理配置文件
$ sudo touch helloworld.conf
// 编辑配置文件
$ sudo vi helloworld.conf
- // 配置内容
- 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;
- }
- }
- // 测试配置
$ sudo nginx -t
// 重新加载配置
$ sudo nginx -s reload
当完成这一步后, 我们就可以在浏览器里直接输入服务器的 IP 地址来访问 Demo 网站了.
注: 这里有一个小坑, Nginx 有一个默认配置文件, 文件路径在:/etc/nginx/sites-available 下. 文件名是 default. 如果访问的时候打开的不是上面这个网站, 那需要到这个配置文件下, 把所有的配置都注释掉, 再重新 reload 一下 Nginx.
8. 安装 Docker
通过上面的教程, 大家已经可以在自己电脑上访问 Linux 服务器上的. Net Core 项目了. 接下来我们将. Net Core 项目放到 Docker 下运行, 并同样通过 Nginx 方向代理, 使我们能访问到. Net Core 项目.
8.1 卸载旧版本 Docker
因为 Ubuntu 系统可能自带 Docker, 所以在安装新版 Docker 前先将旧版的 Docker 清楚干净
$ sudo apt-get remove docker \
- docker-engine \
- docker.io
8.2 安装
$ sudo apt-get update
// 由于 apt 源使用 HTTPS 以确保软件下载过程中不被篡改. 因此, 我们首先需要添加使用 HTTPS 传输的软件包以及 CA 证书.
$ sudo apt-get install \
- apt-transport-https \
- ca-certificates \
- curl \
- software-properties-common
- // 鉴于国内网络问题, 强烈建议使用国内源, 官方源请在注释中查看.
- // 为了确认所下载软件包的合法性, 需要添加软件源的 GPG 密钥.
- $ curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
- // 官方源
- // $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
- // 向 source.list 中添加 Docker 软件源
- sudo add-apt-repository \
- "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
- $(lsb_release -cs) \
- stable"
$ sudo apt-get update
// 执行安装命令
$ sudo apt-get install docker-ce
8.3 启动 Docker 服务
$ sudo systemctl enable docker
$ sudo systemctl start docker
8.4 建立 Docker 用户组
默认情况下, docker 命令会使用 Unix socket 与 Docker 引擎通讯. 而只有 root 用户和 docker 组的用户才可以访问 Docker 引擎的 Unix socket. 出于安全考虑, 一般 Linux 系统上不会直接使用 root 用户. 因此, 更好地做法是将需要使用 docker 的用户加入 docker 用户组.
// 建立 docker 用户组, 这行命令执行的时候可能会报该用户组已存在. 如果报这个错, 可以直接跳过这条命令.
$ sudo groupadd docker
// 将当前用户加入 docker 用户组
$ sudo usermod -aG docker $USER
执行完这条命令后, 退出终端重新登录一下. 就可以使用 docker 命令来管理 docker 了.
8.5 镜像加速器配置
镜像 (image) 是 docker 的一个基础元素, 但当我们在国内的网络环境中拉取 Docker Hub(官方 Docker 镜像库)镜像时会变得比较困难, 这时我们就可以配置镜像加速器. 这里我使用的是 Docker 官方提供的国内加速器服务.
- // docker 文件夹默认是只允许 root 访问的. 这里笔者偷个懒就直接开放 docker 的文件夹权限了.
- sudo chmod 777 /etc/docker
- cd /etc/docker
- // 创建并编辑加速器源
- sudo touch daemon.js
- sudo vi daemon.js
- // 文件内容
- {
- "registry-mirrors": [
- "https://registry.docker-cn.com"
- ]
- }
- // 重启服务
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
9. 在 Docker 中部署. Net Core 项目
9.1 拉取. Net Core 镜像
$ sudo docker pull microsoft/dotnet
9.2 修改 Demo 程序
之前我们用的是默认的 Demo 程序运行, 程序是运行在 5000 端口上的, 在我们的实际环境中, 不可能每个服务都用同一个端口号, 所以这次我们将端口好做一个小变动.
- // 移动到最开始我们创建的 Demo 中. 编辑 Program.cs
- // $ cd ~/helloworld
- // $ vi Program.cs
内容为
- namespace helloworld
- {
- public class Program
- {
- public static void Main(string[] args)
- {
- BuildwebHost(args).Run();
- }
- public static IWebHost BuildWebHost(string[] args) =>
- WebHost.CreateDefaultBuilder(args)
- .UseStartup<Startup>()
- .UseUrls("http://*:10001") // 使用 10001 作为程序运行时端口
- .Build();
- }
- }
完成之后, 我们重新发布
$ dotnet restore
$ dotnet publish -c Release
9.3 编写 Dockerfile
- // 移动到发布目录
- $ cd ~/helloworld/bin/Release/netcoreapp2.0/publish/// 创建 Dockerfile
$ sudo touch Dockerfile
// 编辑 Dockerfile
$ sudo vi Dockerfile
以下是 Dockerfile 的内容
- // 基于. Net Core 镜像来构建我们的镜像
- FROM microsoft/dotnet
- // 将程序复制到镜像中的 publish 目录
- COPY . /publish
- // 定义工作目录为 publish
- WORKDIR /publish
- // 设置 Docker 容器对外暴露的端口
- EXPOSE 10001
- // 执行 dotnet 命令
- CMD ["dotnet", "helloworld.dll"]
9.4 构建 Docker 镜像
$ docker build -t helloworld:1.0 .
注意最后有一个 "."
9.5 运行构建的镜像
$ docker run --name helloworld -d -p 10001:10001 helloworld:1.0
9.6 修改 Nginx 配置
在本文前半段, 我们让 Nginx 代理了 5000 端口, 现在我们把他改为 10001.
// 编辑之前创建的 Nginx 配置
$ cd /etc/nginx/conf.d/
- $ vi helloworld.conf
- // 只需要修改代理的端口就行
- server {
- listen 80;
- location / {
- proxy_pass http://localhost:10001;
- 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;
- }
- }
- // 重载 Nginx
$ sudo nginx -t
$ sudo nginx -reload
好了. 大功告成, 我们现在又可以在浏览器中用服务器 IP 直接访问到 Demo 网站了.
总结
这次学习的过程, 总共花了大概两天时间, 反反复复看了很多其他的教程. 但大部分教程都感觉要么没说清楚, 要么就是缺少某个步骤而导致最后运行不起来, 所以在完成配置以后, 决定写下这篇文章, 算是给自己一个交代. 也希望可以帮到各位看客.
来源: https://www.cnblogs.com/noahji/p/8947151.html