.net core3.0部署Linux服务器 使用Docker容器和Nginx反代理教程
本人刚接触.net core 由于公司项目需要部署在Linux上 近些日子学习和网上大面积搜教程
我在这给大家归拢归拢借鉴的教程做了套方案(我写的可以实现 但不一定是最好的 仅供参考)
我只用过core3.0 之前的版本没接触过
在项目中"Program.cs"文件中找到CreateHostBuilder方法
使用.UseUrls()方法指定单个网址
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseUrls("http://localhost:5000");
webBuilder.UseStartup<Startup>();
});
项目中就需要配置这些
接下来是Linux 我在这里使用的是Oracle VM VirtualBox虚拟机启动的CentOS8
这步是可选的①
虚拟机也需要配置一下网络 不要使用NAT网络 要使用桥接网卡 也许是公司网络原因
说不定也不需要怎么做 主要是为了外部机能访问虚拟机的网络

在终端中切换root限权

接下来Xshell中操作 不了解的也可以使用终端 在这里不细说
首先安装EPEL源
[root@localhost ~]# sudo yum install epel-release
安装Nginx
[root@localhost ~]# sudo yum install nginx
启动Nginx
[root@localhost ~]# sudo systemctl start nginx
开机启动Nginx
[root@localhost ~]# sudo systemctl enable nginx
关闭防火墙或开启指定端口
关闭防火墙
[root@localhost ~]# sudo firewall-cmd --permanent --zone=public --add-service=http
[root@localhost ~]# sudo firewall-cmd --permanent --zone=public --add-service=https
开放指定端口
[root@localhost ~]# sudo firewall-cmd --zone=public --add-port=端口名/tcp --permanent
重启防火墙
[root@localhost ~]# sudo firewall-cmd --reload
到这一步试试外网能否访问 浏览器输入虚拟机的ip 是否能访问

打开Nginx 端口映射配置 也可以使用Xftp打开此路径文件
[root@localhost ~]# sudo vi /etc/nginx/nginx.conf
在中server配置中的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;
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf; events {
worker_connections 1024;
} http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048; include /etc/nginx/mime.types;
default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf; server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html; # Load configuration files for 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;
} error_page 404 /404.html;
location = /40x.html {
} error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
} # Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# } }
重启Nginx服务器
[root@localhost ~]# sudo nginx -s reload
关闭SELinux
在终端输入sestatus,查看SELinux status是否为disabled 如果是这步跳过 不是请执行以下命令修改SELinux配置
[root@localhost ~]# vi /etc/selinux/config
SELINUX=disabled
重启服务器
Nginx配置到此结束,接下来是Docker容器
------------------分割线-----------------
先把yum包更新到最新
[root@localhost ~]# yum update
安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的
[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
设置yum源
[root@localhost ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
可以查看所有仓库中所有docker版本,并选择特定版本安装
[root@localhost ~]# yum list docker-ce --showduplicates | sort -r
安装Docker,命令:yum install docker-ce-版本号
[root@localhost ~]# yum install docker-ce-18.03..ce
启动Docker并加入开启启动
[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl enable docker
验证Docker(有client和service两部分表示docker安装启动都成功)
[root@localhost ~]# docker version
设置Docker镜像加速
修改/etc/docker/daemon.json文件来实现加速(如果文件夹没有此文件,可以创建)
配置内容:
{
"storage-driver": "devicemapper",
"registry-mirrors": [ "https://6kx4zyno.mirror.aliyunce.com","http://f1361db2.m.daocloud.io"]
}
重启Docker
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
docker info 查看docker连接的地址是否生效
下载并安装.net core的镜像 "latest"默认下载最新.net core镜像
[root@localhost ~]# docker pull mcr.microsoft.com/dotnet/core/sdk:latest

等待安装完成之后 查看安装过的镜像
[root@localhost ~]# docker images

这里的IMAGE ID是随机的
接下来是开启一个docker容器
[root@localhost ~]# docker run -i -d -t -v /home/core:/myFirstApp --network=host -v /etc/localtime:/etc/localtime 镜像名
| -i | 以交互模式运行容器,通常与 -t 同时使用; |
| -t | 为容器重新分配一个伪输入终端,通常与 -i 同时使用; |
| -d | 后台运行容器,并返回容器ID; |
| -v | 指定一个本机的路径(这里选择的是.net core程序的路径)冒号后面的是容器中的路径 |
--network=host 使用主机端口
-v /etc/localtime:/etc/localtime 因为容器中获取的时间会比系统时间慢8个小时 所有指定使用主机时间
镜像名也可以写镜像ID 注意空格
查看创建的容器
[root@localhost ~]# docker ps -a

进入容器
[root@localhost ~]# docker exec -it 容器ID /bin/bash
输入ls查看目录 会找到创建容器时所指向的文件夹名

在输入ls就可以看到共享目录中的程序文件
在程序文件中找到要运行的程序名.dll文件
root@localhost:/myFirstApp# dotnet 程序名.dll

这样你的.net core程序就运行成功了 但是这样不能继续操作 使用[ctrl + P][ctrl + Q]退出而不终止容器运行
确保这上面显示的5000端口和nginx配置的5000端口一样
附上常用的docker命令
----------------- docker ps 查看当前正在运行的容器
----------------- docker ps -a 查看所有容器的状态
----------------- docker start/stop id/name 启动/停止某个容器
----------------- docker attach id 进入某个容器(使用exit退出后容器也跟着停止运行)
----------------- docker exec -ti id 启动一个伪终端以交互式的方式进入某个容器(使用exit退出后容器不停止运行)
----------------- docker images 查看本地镜像
----------------- docker rm id/name 删除某个容器
----------------- docker rmi id/name 删除某个镜像
.net core3.0部署Linux服务器 使用Docker容器和Nginx反代理教程的更多相关文章
- (总结)Linux服务器上最简单的Nginx反向代理配置
Nginx不但是一款高性能的Web服务器,也是高性能的反向代理服务器.下面简单说说Nginx的反向代理功能. 反向代理是什么? 反向代理指以代理服务器来接受Internet上的连接请求,然后将请求转发 ...
- Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列之自签TLS证书及Etcd集群部署(二)
0.前言 整体架构目录:ASP.NET Core分布式项目实战-目录 k8s架构目录:Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录 一.服务器设置 1.把每一 ...
- Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列之集群部署环境规划(一)
0.前言 整体架构目录:ASP.NET Core分布式项目实战-目录 k8s架构目录:Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录 一.环境规划 软件 版本 ...
- Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列之flanneld网络介绍及部署(三)
0.前言 整体架构目录:ASP.NET Core分布式项目实战-目录 k8s架构目录:Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录 一.flanneld介绍 ...
- Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列之部署master/node节点组件(四)
0.前言 整体架构目录:ASP.NET Core分布式项目实战-目录 k8s架构目录:Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录 1.部署master组件 ...
- 高性能Linux服务器 第6章 ext3文件系统反删除利器ext3grep extundelete工具恢复rm -rf 误删除的文件
高性能Linux服务器 第6章 ext3文件系统反删除利器ext3grep extundelete工具恢复rm -rf 误删除的文件 只能用于ext3文件系统!!!!!!!高俊峰(高性能Linux ...
- Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录
0.目录 整体架构目录:ASP.NET Core分布式项目实战-目录 k8s架构目录:Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录 一.感谢 在此感谢.net ...
- 附加进程 到远程服务器中Docker容器内 调试
很多时候,我们在本地开发过程中程序运行很正常,但是发布到线上之后由于环境的原因,可能会有一些异常.通常我们会通过日志来分析问题,除了日志还有一种常用的调试手段就是:附加进程. VS中的附加进程非常强大 ...
- docker,构建nginx反向代理tomcat
Nginx实现负载均衡是通过配置nginx.conf来实现的,nginx.conf的全部内容如下: user nginx; worker_processes 1; error_log /var/log ...
随机推荐
- 拿 C# 搞函数式编程 - 2
前一阵子在写 CPU,导致一直没有什么时间去做其他的事情,现在好不容易做完闲下来了,我又可以水文章了哈哈哈哈哈.顺便成立了自己的专栏:hez2010 的编程日常,欢迎大家关注(逃 有关 FP 的类型部 ...
- 下一代容器架构已出,Docker何去何处?看看这里的6问6答!!
我猜很多人一看这个标题已经感觉很懵逼了,什么?下一代容器都出来了,我还没学Docker呢!!! 咳咳~~在这里我给大家做一个保证,下一代容器目前也只是各个公司在测试阶段,Github上面也有很多Iss ...
- Numpy用于数组数据的存储和读取
Python的Numpy模块可用于存储和读取数据: 1.将一个数组存储为二进制文件 Numpy.save:将一个数组以.npy的格式保存为二进制文件 调用格式:numpy.save(file, arr ...
- [leetcode] H-Index (Hash Table)
题目: Given an array of citations (each citation is a non-negative integer) of a researcher, write a f ...
- idea中自定义快捷键
idea中自定义快捷键 在开发的过程中,总要写一些重复的代码,那么使用自定义快捷键,可以将这些经常用到的代码,自动生成. 1.在idea工具中点击File-->Settings 2.在弹出来的界 ...
- go基础之服务退出问题
最近学习公司微服务的代码,看到每一个微服务的main函数都阻塞在那里,然后里面起的goroutine一直在哪里运行. package main import( "fmt" &quo ...
- Mybatis_resultMap的关联方式实现多表查询(一对多)
a)在 ClazzMapper.xml 中定义多表连接查询 SQL 语句, 一次性查到需要的所有数据, 包括对应学生的信息. b)通过<resultMap>定义映射关系, 并通过<c ...
- SSM 轻量级框架构建:图书管理系统
一.接业务,作分析 1.大致业务要求 1.1 使用 SSM( Spring MVC + Spring + MyBatis )实现图书信息管理系统, MySQL5.5 作为后台数据库,该系统包括查询图书 ...
- 捅娄子了,写个bug被国家信息安全漏洞共享平台抓到了?
摸不了鱼了 2019 年 11 月 26 日,本来应该是无比平静的一天,开开会,改改bug,摸摸鱼之后等着下班.刷着新闻的间隙,手机的消息提示音响了起来,收到了一条邮件,平时收到邮件我都会选择稍后处理 ...
- Appium之实操(了解配置项)
使用Appium,测试对象APP的运行环境有两种:① 真实设备 如手机 ②模拟器 如夜神 连接真实设备: - 进入开发者模式,启动usb调试 - 在电脑上 执行adb命令 adb devices ...