.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.1.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 刪除某個鏡像


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM