本人刚接触.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反代理教程的更多相关文章

  1. (总结)Linux服务器上最简单的Nginx反向代理配置

    Nginx不但是一款高性能的Web服务器,也是高性能的反向代理服务器.下面简单说说Nginx的反向代理功能. 反向代理是什么? 反向代理指以代理服务器来接受Internet上的连接请求,然后将请求转发 ...

  2. Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列之自签TLS证书及Etcd集群部署(二)

    0.前言 整体架构目录:ASP.NET Core分布式项目实战-目录 k8s架构目录:Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录 一.服务器设置 1.把每一 ...

  3. Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列之集群部署环境规划(一)

    0.前言 整体架构目录:ASP.NET Core分布式项目实战-目录 k8s架构目录:Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录 一.环境规划 软件 版本 ...

  4. Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列之flanneld网络介绍及部署(三)

    0.前言 整体架构目录:ASP.NET Core分布式项目实战-目录 k8s架构目录:Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录 一.flanneld介绍 ...

  5. Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列之部署master/node节点组件(四)

    0.前言 整体架构目录:ASP.NET Core分布式项目实战-目录 k8s架构目录:Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录 1.部署master组件 ...

  6. 高性能Linux服务器 第6章 ext3文件系统反删除利器ext3grep extundelete工具恢复rm -rf 误删除的文件

    高性能Linux服务器 第6章  ext3文件系统反删除利器ext3grep  extundelete工具恢复rm -rf 误删除的文件 只能用于ext3文件系统!!!!!!!高俊峰(高性能Linux ...

  7. Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录

    0.目录 整体架构目录:ASP.NET Core分布式项目实战-目录 k8s架构目录:Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录 一.感谢 在此感谢.net ...

  8. 附加进程 到远程服务器中Docker容器内 调试

    很多时候,我们在本地开发过程中程序运行很正常,但是发布到线上之后由于环境的原因,可能会有一些异常.通常我们会通过日志来分析问题,除了日志还有一种常用的调试手段就是:附加进程. VS中的附加进程非常强大 ...

  9. docker,构建nginx反向代理tomcat

    Nginx实现负载均衡是通过配置nginx.conf来实现的,nginx.conf的全部内容如下: user nginx; worker_processes 1; error_log /var/log ...

随机推荐

  1. goal

    线段树进阶,主席树,莫队,相似kmp,悬线,单调栈,单调队列,ac自动机,后缀数组,后缀自动机 图论:最短路,差分约束,二分图,网络流,连通分量 不学图论了我太难了   三分找,单调增+max+单调减 ...

  2. UVA-11995

    There is a bag-like data structure, supporting two operations:1 x Throw an element x into the bag.2 ...

  3. CoderForces-375D

    You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. We will ass ...

  4. LNMP架构的搭建

    第9章 LNMP架构的搭建 9.1 什么是LNMP 9.1.1 LNMP的组成 L                linux N                nginx:实现静态的服务处理 M    ...

  5. 一款简单的C++猜数字游戏(新手必学)

    前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:1只小弛 废话不多说,直接上代码! #include<bits/s ...

  6. 从多谐振荡器详细解析到555定时器基本电路(控制LED闪烁)

    在学期末,笔者参加了学校的电工实习,前六天做都很快,但是今天要做一个关于555多谐振荡器的LED闪烁电路,由于笔者没有提前准备,导致今天就算把电路搭建出来也不懂具体原理,耗费了不少时间,所以我打算专门 ...

  7. Linux 怎么清理缓存

    linux清理缓存的命令   查看缓存的命令 free -m 清理缓存的命令  echo 1 > /proc/sys/vm/drop_caches echo 2 > /proc/sys/v ...

  8. 更新Preloader与uboot

    1.用bsp-editor 产生Preloader 参考https://rocketboards.org/foswiki/Documentation/AVGSRD160Preloader 根据QSYS ...

  9. plsql修改某一行数据

    plsql修改某一行数据时, 在查询语句后面加上for update,再点击

  10. 【搞定Jvm面试】 面试官:谈谈 JVM 类文件结构的认识

    类文件结构 一 概述 在 Java 中,JVM 可以理解的代码就叫做字节码(即扩展名为 .class 的文件),它不面向任何特定的处理器,只面向虚拟机.Java 语言通过字节码的方式,在一定程度上解决 ...