.net5發布到Linux指南


目錄

Aspnetcore api 服務發布指南

一、 搭建nginx服務器

  1. 下載安裝gcc編譯庫
  2. 下載安裝pcre正則表達式庫
  3. 下載安裝openssl安全加密庫
  4. 下載安裝zlib解壓庫
  5. 下載解壓並編譯安裝nginx
  6. 啟動nginx
  7. 設置nginx開機啟動
  8. 查看nginx狀態
  9. 常用nginx命令

二、 安裝.net5 runtime

  1. 設置下載源
  2. 安裝運行時

三、 上傳發布的項目文件並創建服務

  1. 安裝工具WinSCP
  2. 使用WinSCP在linux服務器創建服務目錄
  3. 將vs2019發布的項目使用WinSCP上傳到指定目錄
  4. 使用putty創建linux服務
  5. linux服務操作相關命令

四、 配置nginx代理和防火牆

  1. 查看防火牆狀態
  2. 啟動防火牆
  3. 添加指定的端口
  4. 查看網口
  5. 添加網口到公開區域中
  6. 添加nginx反向代理配置

五、 運行截圖

  1. nginx運行截圖
  2. Aspnetcore api運行截圖

 

 

一、搭建nginx服務器

  1. 下載安裝gcc編譯庫

gcc -v

yum -y install gcc

  1. 下載安裝pcre正則表達式庫

yum install -y pcre pcre-devel

  1. 下載安裝openssl安全加密庫

yum install -y openssl openssl-devel

  1. 下載安裝zlib解壓庫

yum install -y zlib zlib-devel

  1. 下載解壓並編譯安裝nginx

wget http://nginx.org/download/nginx-1.9.9.tar.gz

tar -zxvf nginx-1.9.9.tar.gz

cd nginx-1.9.9

./configure --prefix=/usr/local/nginx

make&&make install

  1. 啟動nginx

cd /usr/local/nginx/sbin

./nginx

  1. 設置nginx開機啟動

#在rc.local增加啟動代碼即可

vi /etc/rc.local

#增加一行 /usr/local/nginx/sbin/nginx,增加后保存

#設置執行權限

cd /etc

chmod 755 rc.local

  1. 查看nginx狀態

通過端口查詢: lsof -i:80 #nginx默認是80端口

通過進程查詢: ps -ef | grep nginx

訪問網頁查詢 curl 127.0.0.1

  1. 常用nginx命令

nginx -s stop :快速關閉Nginx,可能不保存相關信息,並迅速終止web服務。

nginx -s quit :平穩關閉Nginx,保存相關信息,有安排的結束web服務。

nginx -s reload :因改變了Nginx相關配置,需要重新加載配置而重載。

nginx -s reopen :重新打開日志文件。

nginx -c filename :為 Nginx 指定一個配置文件,來代替缺省的。

nginx -t :不運行,而僅僅測試配置文件。nginx將檢查配置文件的語法的正確性,並嘗試打開配置文件中所引用到的 文件。

nginx -v:顯示 nginx 的版本。

nginx -V:顯示 nginx 的版本,編譯器版本和配置參數

二、安裝.net5 runtime

  1. 設置下載源

sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm

  1. 安裝運行時

sudo yum install dotnet-runtime-5.0

sudo yum install aspnetcore-runtime-5.0

三、上傳發布的項目文件並創建服務

  1. 安裝工具WinSCP
  2. 使用WinSCP在linux服務器創建服務目錄
  3. 將vs2019發布的項目使用WinSCP上傳到指定目錄
  4. 使用putty創建linux服務

cd /lib/systemd/system

sudo touch Oceania.ShopConfigService.service

sudo nano Oceania.ShopConfigService.service

配置ShopConfig.service服務文件

[Unit]

Description= ShopConfigService

[Service]

WorkingDirectory=/root/apiservices/Oceania.ShopConfigService/

ExecStart=/usr/bin/dotnet /root/apiservices/Oceania.ShopConfigService/Oceania.ShopConfigService.Api.Host.dll

Restart=always

RestartSec=10

SyslogIdentifier= ShopConfigService

User=root

Environment=ASPNETCORE_ENVIRONMENT=Production

Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

 

[Install]

WantedBy=multi-user.target

然后輸入

ctrl+x ->y 回車

  1. linux服務操作相關命令

systemctl  enable Oceania.ShopConfigService.service

systemctl  start Oceania.ShopConfigService.service

systemctl  status Oceania.ShopConfigService.service

 

systemctl [command] [unit]

 

 

 

command 主要有:

 

start:立刻啟動后面接的 unit

 

stop:立刻關閉后面接的 unit。

 

restart:立刻關閉后啟動后面接的 unit,亦即執行 stop 再 start 的意思。

 

reload:不關閉 unit 的情況下,重新載入配置文件,讓設置生效。

 

enable:設置下次開機時,后面接的 unit 會被啟動。

 

disable:設置下次開機時,后面接的 unit 不會被啟動。

 

status:目前后面接的這個 unit 的狀態,會列出有沒有正在執行、開機時是否啟動等信息。

 

is-active:目前有沒有正在運行中。

 

is-enable:開機時有沒有默認要啟用這個 unit。

 

kill :不要被 kill 這個名字嚇着了,它其實是向運行 unit 的進程發送信號。

 

show:列出 unit 的配置。

 

mask:注銷 unit,注銷后你就無法啟動這個 unit 了。

 

unmask:取消對 unit 的注銷。

四、配置nginx代理和防火牆

  1. 查看防火牆狀態

firewall-cmd --state

  1. 啟動防火牆

service firewalld start

  1. 添加指定的端口

firewall-cmd --zone=public --add-port=80/tcp

firewall-cmd --zone=public --add-port=5000/tcp

  1. 查看網口

ifconfig

  1. 添加網口到公開區域中

firewall-cmd --zone=public --add-interface=ens192

  1. 添加nginx反向代理配置

 

#user  nobody;

worker_processes  1;

 

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

 

#pid        logs/nginx.pid;

 

 

events {

    worker_connections  1024;

}

 

 

http {

    include       mime.types;

    default_type  application/octet-stream;

 

    #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  logs/access.log  main;

 

    sendfile        on;

    #tcp_nopush     on;

 

    #keepalive_timeout  0;

    keepalive_timeout  65;

 

    #gzip  on;

 

    server {

        listen       80;

        server_name  localhost;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   html;

            index  index.html index.htm;

        }

 

           location /shopconfigservice/ {

            proxy_pass         http://localhost:5000;  #本地應用地址及端口,可以使用 curl 命令測試一下

            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;

            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

            proxy_set_header   X-Forwarded-Proto $scheme;

        }

 

        #error_page  404              /404.html;

 

        # redirect server error pages to the static page /50x.html

        #

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

 

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80

        #

        #location ~ \.php$ {

        #    proxy_pass   http://127.0.0.1;

        #}

 

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        #

        #location ~ \.php$ {

        #    root           html;

        #    fastcgi_pass   127.0.0.1:9000;

        #    fastcgi_index  index.php;

        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

        #    include        fastcgi_params;

        #}

 

        # deny access to .htaccess files, if Apache's document root

        # concurs with nginx's one

        #

        #location ~ /\.ht {

        #    deny  all;

        #}

    }

 

 

    # another virtual host using mix of IP-, name-, and port-based configuration

    #

    #server {

    #    listen       8000;

    #    listen       somename:8080;

    #    server_name  somename  alias  another.alias;

 

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}

 

 

    # HTTPS server

    #

    #server {

    #    listen       443 ssl;

    #    server_name  localhost;

 

    #    ssl_certificate      cert.pem;

    #    ssl_certificate_key  cert.key;

 

    #    ssl_session_cache    shared:SSL:1m;

    #    ssl_session_timeout  5m;

 

    #    ssl_ciphers  HIGH:!aNULL:!MD5;

    #    ssl_prefer_server_ciphers  on;

 

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}

 

}

 

 

五、運行截圖

  1. nginx運行截圖

 

 

  1. Aspnetcore api運行截圖

 

 

也可以輸入:http://192.168.11.120/shopconfigservice/swagger/index.html 測試

六、發布更新

  1. 發布郵件接口服務

cd /usr/lib/systemd/system

sudo touch Oceania.MailApiService.service

sudo nano Oceania.MailApiService.service

 

[Unit]

Description= Oceania.MailApiService.service

[Service]

WorkingDirectory=/root/net5services/Oceania.MailService.Api.Host

ExecStart=/usr/bin/dotnet /root/net5services/Oceania.MailService.Api.Host/Oceania.MailService.Api.Host.dll urls=”http://*:5011”

Restart=always

RestartSec=10

SyslogIdentifier= Oceania.MailApiService.service

User=root

Environment=ASPNETCORE_ENVIRONMENT=Production

Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

 

[Install]

WantedBy=multi-user.target

 

先Ctrl+x 再y

systemctl enable Oceania.MailApiService.service

systemctl start Oceania.MailApiService.service

systemctl status Oceania.MailApiService.service

 

查詢端口

lsof -i tcp:80

 

curl http://127.0.0.1:5010/api/mail/getemailserverlist

或者

http://192.168.11.120/mailservice/swagger/index.html

 

 

  1. 發布郵件發送服務

使用winSCP上傳文件

cd /usr/lib/systemd/system

sudo touch Oceania.MailService.service

sudo nano Oceania.MailService.service

 

[Unit]

Description= Oceania.MailService.service

[Service]

WorkingDirectory=/root/net5services/Oceania.MailService.Host

ExecStart=/usr/bin/dotnet /root/net5services/Oceania.MailService.Host/Oceania.MailService.Host.dll

Restart=always

RestartSec=10

SyslogIdentifier= Oceania.MailService.service

User=root

Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

 

[Install]

WantedBy=multi-user.target

 

先Ctrl+x 再y

systemctl enable Oceania.MailService.service

systemctl start Oceania.MailService.service

systemctl status Oceania.MailService.service


免責聲明!

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



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