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


    我们的目标:

    • CentOS系统
    • nginx服务器
    • asp.net core应用
    • mysql服务器
    • 腾讯云服务器
    工具准备
    • 【Xshell】——使用windwos下的工具Xshell,原理就是使用SHH协议让我们可以连接其他计算机,类似于windows的远程桌面连接,只是现在用于远程腾讯云主机------------【执行命令】

    • 【WinSCP】——当我们的asp.net core网站写好,发布文件完成时,需要往CentOS上拷贝,这时使用WinSCP,当配置好ip,连接上另外一遍的CentOS系统,则可以实现两台计算机文件的共享,拷贝----------【文件拷贝】

    • 【.net core SDK】——.net core 开发的web或webapp在CentOS上能够运行,就需要环境,.net core去官网看,有linux下各版本的下载安装方式-----------【安装.net core】------CentOS

    • 【nginx】——是一个反向代理http服务器,可以转发

    【安装】
    
    ``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  #安装``
    
     【配置】
     
    在 /etc/nginx中
    cd /etc/nginx
    vim nginx.conf 
    
    - 内容为:
            user  nginx;
            worker_processes  1;
    
            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里面去找

    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---感谢 园友农码一生

  • 相关阅读:
    Delphi 拖动
    Unknown picture file extension
    鼠标指针形状
    C_FD_PhysRDBMSKinds
    delphichromiumembedded
    delphi使用 DockForm DesignEditors F2613 Unit 'DockForm' not found
    TBitConverter
    sql 存储过程返回值 变量名
    XE7 数据库独立运行需要的文件
    C++Builder 内存泄露检测
  • 原文地址:https://www.cnblogs.com/RandyField/p/10959970.html
Copyright © 2020-2023  润新知