• Nginx部署前后端分离项目(Linux)


    一、前端打包

    在这里插入图片描述

    npm run build
    
    • 1

    二、后端打包

    通过Maven 使用package打包
    在这里插入图片描述

    三、Linux部署

    1. 安装Nginx
      安装环境
    yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
    
    • 1

    安装wgte

    yum install wget
    
    • 1

    下载nginx

    wget https://nginx.org/download/nginx-1.21.6.tar.gz
    
    • 1

    解压压缩文件

    tar -zxvf nginx-1.21.6.tar.gz
    
    • 1

    进入nginx文件夹

    cd nginx-1.21.6
    
    • 1

    配置

    ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
    
    • 1

    编译和安装

    编译:make
    安装:make install
    
    • 1
    • 2

    Nginx启动、暂停、重启

    启动
    /usr/local/nginx/sbin/nginx
    重启
    /usr/local/nginx/sbin/nginx -s reload
    如果更改了配置文件建议使用这个方式重新启动
    /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    停止服务
    /usr/local/nginx/sbin/nginx -s stop

    安装java环境(用于启动jar)

    官网 Oracle
    解压:

    sudo tar -zxvf jdk-8u371-linux-x64.tar.gz
    
    • 1

    修改配置:

    sudo vim /etc/profile
    
    • 1

    文件最后提交以下内容 注意:java_home需要换成jdk文件地址

    #jdk1.8
    export JAVA_HOME=/home/shtech/jdk1.8.0_371
    export JRE_HOME=${JAVA_HOME}
    export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
    export PATH=${PATH}:${JAVA_HOME}/bin
    
    • 1
    • 2
    • 3
    • 4
    • 5
    按Esc键 输入 :wq  保存并退出
    
    • 1

    刷新配置:

    source /etc/profile 
    
    • 1

    查看版本:

    java -version
    
    • 1

    详细地址:https://blog.csdn.net/A_yonga/article/details/125526307

    启动jar项目:

    nohup java -jar -Dspring.profiles.active=prod wx-api.jar &
    
    • 1

    停止:

    kill -9 进程号
    
    • 1
    1. 配置Nginx
      nginx.conf:
    
    #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   /data/wx/dist;  // 前端文件地址
                index  index.html index.htm;
            }
    
            location /wx {
                proxy_pass http://127.0.0.1:8080/wx;// 后端接口地址及端口
            }
    
            #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  如不需要https 则全用#注释
        #
        server {
            listen       443 ssl;
            server_name  localhost;
    
            ssl_certificate      /application/nginx-wx/ssl/cacert.pem;
            ssl_certificate_key  /application/nginx-wx/ssl/privkey.pem;
    
            ssl_session_cache    shared:SSL:1m;
            ssl_session_timeout  5m;
    
            ssl_ciphers  HIGH:!aNULL:!MD5;
            ssl_prefer_server_ciphers  on;
    
            location / {
                root   /data/wx/dist; 
                index  index.html index.htm;
            }
    
            location /wx {
                proxy_pass http://127.0.0.1:8080/wx;
            }
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    1. 需要开启Https生成ssl临时证书
      https://blog.csdn.net/who7708/article/details/104565369

    服务器部署文件地址:

    前台文件:/data/wx/dist
    后台文件:/data/wx/wx-api.jar
    nginx文件:/usr/local/nginx/

  • 相关阅读:
    基于微信小程序的机房设备故障报修平台
    【Rust日报】2022-08-06 Fang, Rust的一个异步后台处理
    【Android打包】gradlew : 无法将“gradlew”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。
    浅学一下二叉树链式存储结构的遍历
    压缩感知的概述梳理(4)
    附加:一个工具类,什么时候建议被IoC管理,什么时候不建议被IoC管理;(慎看,本篇博客的观点,可能存在偏差、甚至是错误;)
    批量本地英语文档翻译软件
    failed to solve with frontend dockerfile.v0: failed to create LLB definition:
    Empty suite
    Java8新特性你知道哪些?
  • 原文地址:https://blog.csdn.net/weixin_45067120/article/details/132831737