Linux部署jar项目

一、前后端分离部署

1.把后端jar包上传到远程机
2.启动jar包(nohup java -jar xxx.jar > xxx.log &)
3.postman测试任意接口,确保jar包内程序正常运行,如有错误,查看日志(cat xxx.log)
4.删掉所有ngnix(docker rm ngnix)
5.新建nginx,采用数据卷(docker run --name nginx01 -v /root/vhr/html/:/usr/share/nginx/html/ -d -p 80:80 nginx)
6.上传静态资源到/root/vhr/html/
7.修改nginx配置:
  前往当前静态目录(cd vhr cd html)  
  docker exec -it nginx01 /bin/bash
  find / -name nginx.conf
  cd /etc/nginx
  cd conf.d
  cat default.conf
  复制default.conf中的内容,例如:
    server {
        listen       80;
        listen  [::]:80;
        server_name  localhost;

        #access_log  /var/log/nginx/host.access.log  main;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

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

        # 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;
        #}
    }
  编辑default.conf(vi default.conf)
    server {
        listen       80;
        listen  [::]:80;
        server_name  localhost;

        #access_log  /var/log/nginx/host.access.log  main;

        location / {
            #root   /usr/share/nginx/html;
            #index  index.html index.htm;
            proxy_pass http://网址ip:端口号;
        }
        
        location ~ .*\.(js|css|ico|png|jpg|eot|svg|ttf|woff|html|txt|pdf|) {
           root /usr/share/nginx/html/;#所有静态文件直接读取硬盘
           index index.html;
           expires 30d; #缓存30天
        }

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

        # 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;
        #}
    }
8.docker cp ./default.conf nginx01:/etc/nginx/conf.d/
9.docker restart nginx01

二、前后端不分离部署

  1. 前端执行 npm run build,生成 dist 目录

  2. 将 dist 目录中的东西,拷贝到 Java 工程的 src/resources/static 目录下。

  3. 重新配置 Spring Security,为静态资源放行。

  4. 重新配置数据库地址。

  5. 用 idea 启动项目,启动之后访问。

  6. 打包。

  7. 先在 Windows 上运行 jar 包。

  8. 将 jar 上传到 Linux。

  9. nohup java -jar xxx.jar > 8081.log &

  10. Windows 上的 MySQL 默认不支持远程访问,需要首先允许远程访问,并且关闭 Windows 防火墙。

  11. 可以先在 Linux 上运行 jps 查看 jar 是否真的启动了。

  12. curl http://localhost:8081/index.html 查看 Linux 上是否可以直接访问。

  13. 关闭 Linux 防火墙。

  14. 最后,Windows 上访问 Linux 上的服务。

你可能感兴趣的:(Linux,linux,spring,boot)