Springboot 项目部署到服务器上
项目部署到服务器上,有两种方式,一种 jar 包,一种 war 包
jar包 部署时,后续的域名配置,SSL证书等在nginx中配置
war包 部署时,后续的域名配置可以在tomcat中配置就好,修改项目时直接丢到webapps下就行
jar包#
直接在IDEA 中将项目打成一个jar包,所以的修改均在打包之前完成。
jar包方式启动,也就是使用spring boot内置的tomcat运行。服务器上面只要你配置了jdk1.8及以上即可,不需要外置tomcat。另外 jdk安装【参考】
准备#
环境
IDEA +Maven
Linux+JDK( ≥1.8)+nginx
开始#
1、打开idea编辑器下方的terminal窗口,这也是idea编辑器自带的命令提示符,通过cd进入到项目目录下
2、打包
1 | mvn clean install -Dmaven.test.skip |
稍等一会,看到 BUILD SUCCESS 后说明已经打包完成!
3、此时到项目目录下会看到Target目录,在Target目录下有打成的jar文件
注:******将依赖包也打包进来
增加build插件:
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 | <?xml version= "1.0" encoding= "UTF-8" ?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion>4.0.0</modelVersion> <groupId>com</groupId> <artifactId>EnctyptParamTool</artifactId> <version>1.0-SNAPSHOT</version> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <fastjson.version>1.2.32</fastjson.version> </properties> <dependencies> <!--辅助工具类--> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.5.9</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <!--这部分可有可无,加上的话则直接生成可运行jar包--> <!--<archive>--> <!--<manifest>--> <!--<mainClass>${exec.mainClass}</mainClass>--> <!--</manifest>--> <!--</archive>--> <descriptorRefs> <descriptorRef>jar- with -dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build> </project> |
具体参考:1、maven将依赖第三方包打包(package)到jar中
部署#
1、上传到服务器上
任意新建一个文件夹,将 打包好的 jar包 移入
2、运行
1 | nohup java -jar ***.jar >log.txt & |
这种方法会把日志文件输入到你指定的文件中,没有则会自动创建,进程会在后台运行。
3、访问
1 | ip+端口 |
4、nginx代理
nginx安装配置参考:链接
参考:链接
配置nginx.conf:
1 | vim /etc/nginx/nginx.conf 打开配置文件 |
1 | vim /etc/nginx/conf.d/ default .conf |
只需该改动2处即可:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | server { listen 80; server_name span.fun; #换上你准备的域名 #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { #配置访问的项目路径(注:这里重点) proxy_pass http: //47.100.207.28:8000; #此处一定加冒号,不然重启服务报错 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 100m; root /usr/share/nginx/html; index index.html index.htm; } |
5、配置SSL证书,https访问
参考:链接
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 | server { listen 443 ssl; server_name span.fun; #SSL配置 ssl_certificate "**.pem" ; #将domain name.pem替换成您证书的文件位置+名称。 ssl_certificate_key "*.key" ; #将domain name.key替换成您证书的密钥文件位置+名称。 ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #使用此加密算法。 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用该协议进行配置。 ssl_prefer_server_ciphers on ; location / { #配置访问的项目路径(注:这里重点) proxy_pass http: //47.100.207.28:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 100m; root /usr/share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { # root /usr/share/nginx/html; } } # 接收http请求,转到https server { listen 80; server_name span.fun; #需要将yourdomain.com替换成证书绑定的域名。 rewrite ^(.*)$ https: //$host$1; #将所有HTTP请求通过rewrite指令重定向到HTTPS。 location / { #配置访问的项目路径(注:这里重点) proxy_pass http: //47.100.207.28:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; index index.html index.htm; } } |
此处需注意:
若出现错误:ERR_SSL_PROTOCOL_ERROR
参考:链接
6、域名访问
1 |
war包 #
参考:链接
作者:Hang Shao
出处:https://www.cnblogs.com/pam-sh/p/14354762.html
版权:本作品采用「知识共享」许可协议进行许可。
声明:欢迎交流! 原文链接 ,如有问题,可邮件(mir_soh@163.com)咨询.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2020-02-01 Win10系统下如何将中文登录名改为英文登录名
2020-02-01 Windows10怎么用Administrator登录?