.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 ...
随机推荐
- HOOK的类型
- 洛谷 题解 P5534 【【XR-3】等差数列】
我又双叒叕被包菜辣! 题目 这道题是不久前的考试题,现在来水一篇题解 扯回正题 题目很明显的告诉你了,这是一个等差数列, 然后,还告诉你了首项, 第二项, 项数. 你还想咋滴 告诉了你首项和第二项,相 ...
- 使用Fiddler 对ios 设备进行HTTPS 的抓取
http://blog.csdn.net/skylin19840101/article/details/43485911 使用Fiddler 对ios 设备进行HTTPS 的抓取
- python实现十大核心算法(桶排没实例)
# author:sevenduke # 2019-06-11 # 一.交换排序 # 排序算法的温故:冒泡排序 def dubblesort(arr): for i in range(0, len(a ...
- 使用.NET Core创建Windows服务 - 使用.NET Core工作器方式
原文:Creating Windows Services In .NET Core – Part 3 – The ".NET Core Worker" Way 作者:Dotnet ...
- [TimLinux] CSS 计数功能实现递归目录
内容引用自<css世界>: count-reset 与 counter 为父子关系,兄弟关系会导致序号混乱 调用一次 count-increment 将给序号进行一次报数,调用 count ...
- ARTS-S linux查看进程打开的文件数
当怀疑进程打开文件没有关闭时,可以反复执行以下命令,查看进程打开的文件数是否会不断增加. ls -l /proc/18707/fd | wc -l 其中18707是进程id
- 如何使用Jmeter进行压力测试
Jmeter做压力测试的操作:Jmeter不仅可以做接口测试,还可以做压力测试,下面介绍介绍如何jmeter进行简单地压力测试.具体步骤如下: 第一步:添加请求,这里不介绍具体步骤,详见(https: ...
- C# 使用自带Microsoft.Office.Interop.Excel简单操作Excel文件
项目添加应用 Microsoft.Office.Interop.Excel.dll 文件 引用命名空间: using Excel = Microsoft.Office.Interop.Excel; 简 ...
- jfinal shiro共享
和上一篇tomcat sexxion共享一样,用的也是redis 代码: package com.test.shiro; import com.jfinal.log.Log; import com.j ...