centos7下部署nginx+supervisor+netcore2.1服務器環境

因爲工做須要,本身部署了一臺centos7服務器,用於部署netcore2.1的服務器環境,涉及到的相關內容主要有如下幾點php

  1. netcore2.1版本的部署調試
  2. supervisor守護進程的部署
  3. nginx反向代理部署以及簡單的負載均衡示例

其餘用到的相關軟件有vs2017,xshell,xftp html

內容比較基礎,適合剛剛接觸的新手,高手就忽略吧linux

 

1、netcore2.1版本部署nginx

首先須要一臺服務器,固然用虛擬機也能夠,我這裏是本身買了一臺阿里雲服務器,最低配置的,價格便宜,用xshell遠程連接界面以下c++

如今須要在服務器上安裝netcore運行時,執行如下命令web

sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
sudo yum update
sudo yum install dotnet-sdk-2.1

參考內容 https://www.microsoft.com/net/learn/get-started/linux/centosshell

安裝好之後能夠查看版本信息vim

須要注意的一點是,本機的開發版本要和服務器的版本一致,我本地的版本原來的2.0的,服務器安裝了2.1在進行項目部署的時候就會有問題,這個注意下就好centos

使用vs2017建立一個web應用程序,這裏注意選擇2.1,而後繼續bash

下圖是新建好的項目

在Program.cs文件裏新增UseUrls,注意127.0.0.1換成你的服務器IP,端口咱們先用8001

 public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseUrls("http://127.0.0.1:8001");
    }

如今發佈代碼到本地,這是發佈好的代碼

在服務器上新建一個名爲myapp的文件夾

使用xftp工具將代碼傳到服務器上

在服務器上進入項目目錄,執行命令

dotnet myapp.dll

如今項目已經跑起來了

直接訪問

 

2、supervisor守護進程的部署

目前存在一個問題,我使用的xshell遠程連接服務器,當我關閉xshell工具的時候,dotnet myapp.dll 命令也就自動取消了,如今須要解決這個問題,就要用到守護進程,linux下守護進程的軟件比較多,我選擇用supervisor的緣由主要是微軟推薦的

首先安裝supervisor

yum install supervisor

安裝成功以後會有一個默認的配置文件在 /etc/supservisord.conf,在這個文件的最後一行有一句代碼,這句代碼的意思就是supervisor會讀取supervisord.d文件夾裏全部的ini後綴的配置文件

[include] 
files = supervisord.d/*.ini

咱們接下來給myapp站點寫一個supervisor的配置文件後綴就是ini,而且放到supervisord.d文件夾裏

[program:myapp]  //項目名稱
command=dotnet myapp.dll  //執行的命令
directory=/myapp //執行的目錄
autostart=true //自動啓動
autorestart=true //自動重啓 
user=root  //用戶 
numprocs=1 //進程數 
stdout_logfile=/var/log/supervisor-myapp.log //日誌路徑

爲了使用方便,我把supervisor配置開機啓動而且啓動

systemctl enable supervisord
systemctl start supervisord

如今web站點已經被加入到守護進程,而且守護進程也是自動啓動,即便服務器由於意外緣由重啓,也不用擔憂站點沒法訪問

 

3、nginx反向代理部署以及簡單的負載均衡示例

使用nginx做爲反向代理服務器有它自身的優勢,在此很少說了,直接開始部署,首先下載安裝包,兩種方式,一是直接在服務器經過wget命令下載,二能夠直接在本機下載而後經過xftp工具上傳到服務器,咱們這了直接用wget下載,首先在服務器安裝一下wget

yum install wget

而後使用wget命令下載

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

下載完成以後解壓

tar zxvf nginx-1.15.0.tar.gz

而後進入解壓好的目錄執行

./configure

我這裏出現了錯誤,缺乏相關依賴庫,安裝如下依賴庫

yum install gcc-c++
yum install pcre
yum install pcre-devel
yum install zlib 
yum install zlib-devel
yum install openssl
yum install openssl-devel

安裝完成以後進入剛纔nginx解壓的目錄繼續安裝nginx,默認安裝位置爲/usr/local/nginx

make && make install

進入安裝目錄的conf文件夾,修改配置文件

vim nginx.conf

在server節點下,修改location內容,注意ip地址換成你的服務器IP,也就是Program.cs文件裏配置的那個IP

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / { proxy_pass http://127.0.0.1:8001;
 }

        #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;
        #}
    }

同supervisor同樣,把nginx設置爲開機啓動,不過nginx是用源碼編譯安裝的,因此要手動建立nginx.service服務文件

vim /lib/systemd/system/nginx.service

寫入配置,若是你修改了安裝目錄,配置文件的目錄也要修改

[Unit]
Description=nginx
After=network.target
  
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target

建立開機啓動而且啓動,這裏有一點,若是服務器的80端口被佔用,nginx啓動可能會有問題,建議其餘程序不要使用80端口

systemctl enable nginx.service
systemctl start nginx.service

如今咱們能夠直接經過IP訪問網站,不須要再經過端口,這是由於nginx作了代理,若是想經過域名訪問,能夠在nginx裏配置server節點下的server_name的值

而後重啓nginx

systemctl restart nginx.service

最後再簡單介紹一下nginx作負載均衡的配置,咱們把剛纔的站點首頁改爲webA,而且從新發布,記得更新完以後要重啓一下supervisor,否則修改不會生效

systemctl restart supervisord

如今頁面這樣的

而後在建一個如出一轍的站點,首頁改爲webB,Program.cs文件端口改爲8002,在服務器上新建一個myappb的文件夾,把b站點發布的代碼放到這裏,而後複製一份myapp.ini這個文件myappb.ini,也放到supervisord.d文件夾裏,注意配置文件裏的路徑要改爲myappb站點的路徑,其實就是按照myapp站點的方式重來一遍

重啓supervisor

systemctl restart supervisord

如今你能夠訪問8001和8002兩個站點了,首頁一個是webA一個是webB,而後在配置nginx,在http模塊中添加以下配置,注意IP換成你的服務器IP,那個weight就是權重,不具體說了

upstream webservers {
    server 127.0.0.1:8001 weight=10;
    server 127.0.0.1:8002 weight=10;
}

proxy_pass 修改

  location / {
            proxy_pass   http://webservers;
        }

這裏是修改完的配置

最後重啓一下nginx

systemctl restart supervisord

好了,如今訪問你nginx配置的IP或者域名,不斷的刷新,就會發現webA和webB會隨機訪問,這樣一個簡單的負載均衡就實現了

相關文章
相關標籤/搜索