首页 技术 正文
技术 2022年11月9日
0 收藏 482 点赞 2,675 浏览 5667 个字

本人刚接触.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网络 要使用桥接网卡 也许是公司网络原因

说不定也不需要怎么做 主要是为了外部机能访问虚拟机的网络

.net core3.0部署Linux服务器 使用Docker容器和Nginx反代理教程

在终端中切换root限权

.net core3.0部署Linux服务器 使用Docker容器和Nginx反代理教程

接下来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 是否能访问

.net core3.0部署Linux服务器 使用Docker容器和Nginx反代理教程

打开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

.net core3.0部署Linux服务器 使用Docker容器和Nginx反代理教程

等待安装完成之后 查看安装过的镜像

[root@localhost ~]# docker images

.net core3.0部署Linux服务器 使用Docker容器和Nginx反代理教程

这里的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

.net core3.0部署Linux服务器 使用Docker容器和Nginx反代理教程

进入容器

[root@localhost ~]# docker exec -it 容器ID /bin/bash

输入ls查看目录 会找到创建容器时所指向的文件夹名

.net core3.0部署Linux服务器 使用Docker容器和Nginx反代理教程

在输入ls就可以看到共享目录中的程序文件

在程序文件中找到要运行的程序名.dll文件

root@localhost:/myFirstApp# dotnet 程序名.dll

.net core3.0部署Linux服务器 使用Docker容器和Nginx反代理教程

这样你的.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 删除某个镜像

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289