【netcore基礎】ubuntu 16.04 搭建.net core 2.1 linux 運行環境 nginx反向代理 supervisor配置自啓動

今天來整理下 netcore 在 linux(ubuntu) 上的運行環境搭建php

 

對應版本html

ubuntu 16.04python

.net core 2.1linux

nginx version: nginx/1.10.3 (Ubuntu)nginx

supervisor 配置開機重啓服務自啓動shell

Supervisor
http://supervisord.org/
是用Python開發的一個client/server服務,是Linux/Unix系統下的一個進程管理工具,不支持Windows系統。
它能夠很方便的監聽、啓動、中止、重啓一個或多個進程。
用Supervisor管理的進程,當一個進程意外被殺死,supervisort監聽到進程死後,會自動將它從新拉起,很方便的作到進程自動恢復的功能,再也不須要本身寫shell腳原本控制。

 

 其實之前也整理過一些簡單的運行環境搭建,只是SDK的安裝和netcore命令行運行ubuntu

 例如切換到發佈目錄,執行下面的命令api

python@ubuntu:~/Desktop/publish$ dotnet GeduDistributionApi.dll
Hosting environment: Production
Content root path: /home/python/Desktop/publish
Now listening on: http://localhost:5000
Now listening on: https://localhost:5001
Application started. Press Ctrl+C to shut down.

執行上面的命令行,就能夠臨時開啓服務,訪問就能夠看到 https://localhost:5001/php7

 

基礎配置,具體可參考以前的博文mvc

【.NetCore學習】ubuntu16.04 搭建.net core mvc api 運行環境

 

上面僅僅能運行而已,並不能做爲穩定的服務給咱們用,因此咱們須要下面的操做 

● 使用 nginx 做爲反向代理綁定 ip 和咱們的 netcore 服務

● 使用 supervisor 配置系統自啓動服務,在關機開機、重啓、程序崩潰等狀況下,能自動啓動服務,不須要咱們手動操做

 

今天重點講下這兩點的配置

 

這裏須要注意的是配置時最好使用 root 用戶,由於其餘用戶有不少權限問題,會致使配置失敗

使用 root 用戶能避免那些莫名其妙的問題

 

python@ubuntu:~$ sudo su root
[sudo] python 的密碼:
root@ubuntu:/home/python# service supervisor stop

使用這個命令,咱們能夠看到以前的python用戶切換成了 root 用戶

 

Nginx

 

安裝、卸載通用命令

sudo apt-get install xxx
sudo apt-get remove xxx

 

經過下面的命令

sudo apt-get install nginx

 

而後配置轉發到咱們的端口

 配置文件路徑 /etc/nginx/sites-available/default

這裏須要注意的是文件權限問題,若是沒有權限,須要用下面命令給編輯權限

sudo chmod 777 /etc/nginx/sites-available/default

 

文件原內容以下

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    include snippets/fastcgi-php.conf;
    #
    #    # With php7.0-cgi alone:
    #    fastcgi_pass 127.0.0.1:9000;
    #    # With php7.0-fpm:
    #    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny all;
    #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#    listen 80;
#    listen [::]:80;
#
#    server_name example.com;
#
#    root /var/www/example.com;
#    index index.html;
#
#    location / {
#        try_files $uri $uri/ =404;
#    }
#}
View Code

咱們按照格式配置修改爲以下內容,指定把 80 端口的請求轉發給咱們的 netcore 默認端口 5000,保存配置文件

server {
    listen 80;
    location / {
        proxy_pass http://127.0.0.1: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;
    }
}

 

而後咱們從新加載 nginx 配置生效

sudo nginx -t

sudo nginx -s reload

 

至此咱們 nginx 的配置基本結束了,咱們訪問  http://localhost/ 能夠看到 nginx 的提示信息

<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.10.3 (Ubuntu)</center>
</body>
</html>

若是咱們把服務啓動,就能夠看到請求轉發到咱們的 netcore 了,下面咱們配置開機自啓動

 

Supervisor

 

一樣用下面命令安裝

sudo apt-get install supervisor

 

安裝完以後咱們配置自啓動配置文件,配置文件目錄爲: /etc/supervisor/conf.d/

咱們命名爲:GeduDistributionApi.conf

文件內容,主要修改一下 執行的目錄、命令行、日誌輸出文件等

[program:GeduDistributionApi]
command=dotnet GeduDistributionApi.dll
directory=/home/python/publish
environment=ASPNETCORE__ENVIRONMENT=Production
user=root
stopsignal=INT
autostart=true
autorestart=true
startsecs=5
stderr_logfile=/var/log/GeduDistributionApi.err.log
stdout_logfile=/var/log/GeduDistributionApi.out.log

 

重啓生效

sudo service supervisor stop
sudo service supervisor start

 

配置好以後咱們加入系統自動啓動的配置文件 /etc/rc.local

在這個配置文件的 exit 0 前面一行加上 

service supervisor start

保存。

好像沒有加也能啓動,不知道爲何,應該是配置目錄下的 .conf 文件自動生效了 

 

發佈文件以後重啓生效的命令

service supervisor restart

 

 

其餘參考博客

.Net Core 部署到Ubuntu 16.04 中的步驟

相關文章
相關標籤/搜索