【One by one系列】一步步部署.net core應用

咱們的目標:

  • CentOS系統
  • nginx服務器
  • asp.net core應用
  • mysql服務器
  • 騰訊雲服務器
  • 工具準備
  • 【Xshell】——使用windwos下的工具Xshell,原理就是使用SHH協議讓咱們能夠鏈接其餘計算機,相似於windows的遠程桌面鏈接,只是如今用於遠程騰訊雲主機------------【執行命令】html

  • 【WinSCP】——當咱們的asp.net core網站寫好,發佈文件完成時,須要往CentOS上拷貝,這時使用WinSCP,當配置好ip,鏈接上另一遍的CentOS系統,則能夠實現兩臺計算機文件的共享,拷貝----------【文件拷貝】python

  • 【.net core SDK】——.net core 開發的web或webapp在CentOS上可以運行,就須要環境,.net core去官網看,有linux下各版本的下載安裝方式-----------【安裝.net core】------CentOSmysql

  • 【nginx】——是一個反向代理http服務器,能夠轉發linux

    【安裝】nginx

    curl -o nginx.rpm
    http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
    rpm -ivh nginx.rpm
    yum install nginx #安裝git

    【配置】github

    在 /etc/nginx中
    cd /etc/nginx
    vim nginx.confweb

    • 內容爲:
      user nginx;
      worker_processes 1;sql

      error_log  /var/log/nginx/error.log warn;
        pid        /var/run/nginx.pid;
      
        events {
            worker_connections  1024;
        }
      
        http {
            include       /etc/nginx/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  /var/log/nginx/access.log  main;
      
            sendfile        on;
            #tcp_nopush     on;
            client_max_body_size  2000m;  #最大限制爲2000M --萬一你的web須要上傳文件或者圖片等大文件
      
            keepalive_timeout  65;
      
            #gzip  on;
      
            include /etc/nginx/conf.d/*.conf;
        }

注意最後依據 include ,這個有點像C語言的,意思是這個配置文件是嵌套的,更詳細的配置要去 /etc/nginx/conf.d/*.conf裏面去找shell

cd /etc/nginx/conf.d/
vim default.conf

內容爲下:
    server {
        listen       80;
        server_name  118.24.112.238;

        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;

        location / {
            proxy_pass http://localhost:5009;
            proxy_http_version 1.1;
        proxy_set_header X-real-ip           $remote_addr;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        proxy_connect_timeout    600;
        proxy_read_timeout       600;
        proxy_send_timeout       600;
        }

        #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   /usr/share/nginx/html;
        }
    }
    server {
        listen       81;
        server_name  118.24.112.238;

        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;

        location / {
            proxy_pass http://localhost:5000;
            proxy_http_version 1.1;
        proxy_set_header X-real-ip           $remote_addr;
            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;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }

大概意思就是監聽80端口,轉5009,監聽81端口,轉5000,其餘待後續補充知識

【重載】

nginx配置文件修改後,請必定不要忘記重載,新手很容易忘,
nginx -s reload
  • 【守護進程】

    nginx安裝配置都好了,防火牆80端口開放,dotnet netcore.dll運行,網站打開,80轉發至端口5000,這樣的確是發佈了,可是總不至於每次開機都要執行一次dotnet run ,只是就須要配置守護服務Supervisor,(守護服務-守護進程)======================何謂守護服務,讓其一直運行咱們的web,錯誤時本身處理,本身重啓

    【安裝】

    yum install python-setuptools
    easy_install supervisor #安裝Supervisor

    【配置】

    Supervisor的默認配置文件supervisord.conf  可是沒有使用
    自建了一個supervisor目錄,

    【cmd】:mkdir /etc/supervisor
    而後把配置文件輸出到指定目錄:
    【cmd】:echo_supervisord_conf > /etc/supervisor/supervisord.conf #配置Supervisor
    其中supervisord.conf的文件最後:
    ;[include]
    ;files = relative/directory/.ini
    修改成(【注意】去掉;且不能有空格)
    [include]
    files = conf.d/
    .conf
    而後cd /etc/supervisor/
    mkdir conf.d
    新建文件:
    vim zyhopsys.conf
    vim zyhopsys-admin.conf
    文件內容大概爲:
    [program:opadmin]
    command=dotnet ZYH.Operation.Sys.Admin.dll #(注意)運行程序的命令
    directory= /home/op-admin/ #(注意 注意)對應的你的項目的存放目錄,這個地方好多初學者搞錯!!!
    autorestart=true #程序意外退出是否自動重啓
    environment=ASPNETCORE_ENVIRONMENT=Production #進程環境變量
    stderr_logfile=/var/log/myproject.err.log; #錯誤日誌文件
    stdout_logfile=/var/log/myproject.out.log; #輸出日誌文件
    user=root #進程執行的用戶身份
    stopsignal=INT
    autostart=true
    autorestart=true
    startsecs=1

    【搭載配置文件運行】

    supervisord -c /etc/supervisor/supervisord.conf
      這裏稍微提一句:supervisord的啓動順訊
      supervisord                                   #默認去找$CWD/supervisord.conf,也就是當前目錄
      supervisord                                   #默認$CWD/etc/supervisord.conf,也就當前目錄下的etc目錄
      supervisord                                   #默認去找/etc/supervisord.conf的配置文件
      supervisord -c /home/supervisord.conf         #到指定路徑下去找配置文件
    
      運行後:ps -ef | grep dotnet
      能夠查看本身的網站是否已運行,正常以下
      root      1877  1817  0 16:40 pts/1    00:00:00 grep --color=auto dotnet
      root      4971 26752  0 13:57 ?        00:00:07 dotnet ZYH.Operation.Sys.Admin.dll
      root      4972 26752  0 13:57 ?        00:00:05 dotnet ZYH.Operation.Sys.Web.dll

    【重載】

    supervisorctl reload  #從新加載
      每次從新部署 後,能夠執行一下上面的命令

    【設置開機啓動】

    -創建配置文件
      打開目錄 /usr/lib/systemd/system/ 新建文件 supervisord.service
      cd /usr/lib/systemd/system/
      vim supervisord.service
      內容:   
      # dservice for systemd (CentOS 7.0+)
      # by ET-CS (https://github.com/ET-CS)
      [Unit]
      Description=Supervisor daemon
    
      [Service]
      Type=forking
      ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
      ExecStop=/usr/bin/supervisorctl shutdown
      ExecReload=/usr/bin/supervisorctl reload
      KillMode=process
      Restart=on-failure
      RestartSec=42s
    
      [Install]
      WantedBy=multi-user.target
      執行命令:
      systemctl enable supervisord 
      systemctl is-enabled supervisord #來驗證是否爲開機啓動
  • 【防火牆】

    若是公網ip訪問不了:那是由於CentOs的防火牆攔截了,咱們打開端口。

    firewall-cmd --zone=public --add-port=80/tcp --permanent #(開放80端口)
    systemctl restart firewalld #(重啓防火牆以使配置即時生效)

    firewall-cmd --zone=public --add-port=80/tcp --permanent #(開放80端口)
    systemctl restart firewalld #(重啓防火牆以使配置即時生效)

    --我在使用騰訊雲主機,經過上述命令並不能遠程訪問mysql

    firewall-cmd --zone=public --add-port=3306/tcp --permanent #(開放3306端口)

    最後改用iptables

    【安裝】

    #先檢查是否安裝了iptables
      service iptables status
      #安裝iptables
      yum install -y iptables
      #升級iptables
      yum update iptables
      #安裝iptables-services
      yum install iptables-services

    【中止firewalld】

    #中止firewalld服務
      systemctl stop firewalld
      #禁用firewalld服務
      systemctl mask firewalld

    【配置iptables】

    vim /etc/sysconfig/iptables
      # sample configuration for iptables service
      # you can edit this manually or use system-config-firewall
      # please do not ask us to add additional ports/services to this default configuration
      *filter
      :INPUT ACCEPT [0:0]
      :FORWARD ACCEPT [0:0]
      :OUTPUT ACCEPT [0:0]
      -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
      -A INPUT -p icmp -j ACCEPT
      -A INPUT -i lo -j ACCEPT
      -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
      -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
      -A INPUT -m state --state NEW -m tcp -p tcp --dport 81 -j ACCEPT
      -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
      -A INPUT -m state --state NEW -m tcp -p tcp --dport 23 -j ACCEPT
    
      -A INPUT -j REJECT --reject-with icmp-host-prohibited
      -A FORWARD -j REJECT --reject-with icmp-host-prohibited
      COMMIT
  • 綜上,到目前位置,咱們尚未涉及與數據庫級別的交互,只是.net core在linux上發佈經歷的環境配置
  • CentOS的安裝
          遠程執行終端Xshell
          遠程拷貝文件WinSCP
          .net core 環境的安裝
          服務器nginx的安裝,配置,轉發規則配置等
          守護服務Supervisor的安裝,自啓動
  • 承上啓下,之前都是發佈,可是咱們的動態網站,必有數據源,咱們選擇mysql,mysql經歷安裝,root帳戶登陸,設置密碼,

    # wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
      # rpm -ivh mysql-community-release-el7-5.noarch.rpm
      # yum install mysql-community-server

開啓權限,開啓CentOS防火牆-3306的端口(相似與sqlserver1433端口),重啓防火牆,這樣咱們就能遠程訪問mysql

centOS預裝了mariadb(mysql之父爲了mysql可能存在閉源風險而搞mysql分支)

安裝完之後mariadb自動就被替換了,將再也不生效。

 【安裝】

    # wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
    # rpm -ivh mysql-community-release-el7-5.noarch.rpm
    # yum install mysql-community-server

 【重啓mysql服務】


    # service mysqld restart

 【修改密碼】

初次安裝mysql,root帳戶沒有密碼。

直接 #mysql -u root

# mysql>show databases;

mysql>set password for 'root'@'localhost' =password('設置你的密碼');
Query OK, 0 rows affected (0.00 sec)

不須要重啓數據庫便可生效。

 【配置】

#vim /etc/my.cnf

內容以下:
    # For advice on how to change settings please see
    # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
    [client]
    default-character-set=utf8
    # 加上 省得有中文亂碼

    [mysql]

    [mysqld]

    character-set-server = utf8
    # 加上 省得有中文亂碼

    innodb_log_file_size=640M

    max_allowed_packet = 64M 
    #加上,當你有大量數據要往數據庫中存儲就須要這個配置,例如二進制文件

    #
    # Remove leading # and set to the amount of RAM for the most important data
    # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
    # innodb_buffer_pool_size = 128M
    #
    # Remove leading # to turn on a very important data integrity option: logging
    # changes to the binary log between backups.
    # log_bin
    #
    # Remove leading # to set options mainly useful for reporting servers.
    # The server defaults are faster for transactions and fast SELECTs.
    # Adjust sizes as needed, experiment to find the optimal values.
    # join_buffer_size = 128M
    # sort_buffer_size = 2M
    # read_rnd_buffer_size = 2M

    datadir=/var/lib/mysql

    socket=/var/lib/mysql/mysql.sock

    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0

    default-storage-engine=InnoDB
    max_connections=151

    # Recommended in standard MySQL setup
    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

    [mysqld_safe]

    log-error=/var/log/mysqld.log

    pid-file=/var/run/mysqld/mysqld.pid

【遠程鏈接設置】- 我就想在家,在公司,在任何地方都能進入我本身的數據庫操做一下,navicat連一下

    #把在全部數據庫的全部表的全部權限賦值給位於全部IP地址的root用戶。

    mysql> grant all privileges on *.* to root@'%'identified by 'password';

    #若是是新用戶而不是root,則要先新建用戶

    mysql>create user 'username'@'%' identified by 'password'; 



【重載】

    配置文件修改後,別忘記重啓mysql
    service mysqld restart

參考資料:https://www.cnblogs.com/zhaopei/p/netcore.html---感謝 園友農碼一輩子

相關文章
相關標籤/搜索