Spring Boot博客开发日常记录-将写好的部分部署到云服务器

之前完成了写博客,展示博客两个功能,以及初步算是入门了,现在尝试将博客部署到云服务器
部署的方式是采用的是 Nginx+Docker,相关步骤写在了shell中,直接调用shell可以完成自动化部署

Nginx安装

这部分不是主要内容,直接输入

apt-get install nginx

即可完成安装,很简单
在来看看nginx的conf文件,/etc/nginx/nginx.conf

server{
listen 80;
          server_name localhost www.nevergetme.com;#我的网址

          location ~ .*\.(gif|jpg|jpeg|png)$ {
                  expires 24h;
                  root /home/image/;#指定图片存放路径
                  proxy_store on;
                  proxy_store_access user:rw group:rw all:rw;
                  proxy_temp_path     /home/image/;#图片访问路径
                  proxy_redirect     off;
                  proxy_set_header    Host 127.0.0.1;
                  client_max_body_size  10m;
                  client_body_buffer_size 1280k;
                  proxy_connect_timeout  900;
                  proxy_send_timeout   900;
                  proxy_read_timeout   900;
                  proxy_buffer_size    40k;
                  proxy_buffers      40 320k;
                  proxy_busy_buffers_size 640k;
                  proxy_temp_file_write_size 640k;
                  if ( !-e $request_filename)
				  {
                          proxy_pass http://127.0.0.1;#默认80端口
                  }
          }
          location /{
                  proxy_pass http://localhost:8080/;
          }
          }

}

以上需要注意两个点

  • 将根目录访问映射到8080端口,也就是项目占用的端口
  • 图片存储映射的路径为**/home/image/**,这个在后面会用到

输入nginx -t可以测试conf文件是否正确
输入nginx -s reload可以重写加载该conf文件

MySQL设置

  1. 创建一个用户并给权限:
    1. create user ‘user’@’%’ identified by ‘passwd’
    2. create database MyBlog;
    3. grant all on MyBlog,* to ‘user’@’%’;
  2. 导入SQL文件 source MyBlog.sql
  3. 修改bind-address= 0.0.0.0 让用户可以远程登录

使用MAVEN编译jar

先编写properties
本地properties内容如下所示
application-dev.properties

server.port=8080
logging.level.org.springframework=DEBUG
#springboot   mybatis
mybatis.mapper-locations = classpath:mapper/*Mapper.xml
mybatis.config-location = classpath:mapper/config/sqlMapConfig.xml
mybatis.type-aliases-package = com.nevergetme.nevergetmeweb.bean

spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/MyBlog?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = hzw
spring.datasource.password = hzw123456

#thymeleaf start
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
#static 文件夹下的静态文件访问路径
file.uploadPath=D:/program/java/NevergetmeWeb/NeverGetMeWeb/src/main/resources/static/source/img/
#thymeleaf end

spring.mvc.static-path-pattern=/**
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${file.uploadPath}

生产环境的properties如下所示
application-prod.properties

server.port=8080

logging.level.org.springframework=DEBUG
#springboot   mybatis
mybatis.mapper-locations = classpath:mapper/*Mapper.xml
mybatis.config-location = classpath:mapper/config/sqlMapConfig.xml
mybatis.type-aliases-package = com.nevergetme.nevergetmeweb.bean

spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://www.nevergetme.com:3306/MyBlog?useUnicode=true&characterEncoding=utf-8

spring.datasource.username = ****
spring.datasource.password = *****
#thymeleaf start
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=true
#static 文件夹下的静态文件访问路径
file.uploadPath=/home/img/
#thymeleaf end

spring.mvc.static-path-pattern=/**
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${file.uploadPath}

这地方file.uploadPath=/home/img需要注意一下,在后面会用到
然后在application.properties中将环境切换到生产环境

spring.profiles.active=prod

然后是修改template中的文件
例如index.html

<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script th:src="@{/js/bootstrap.min.js}"></script>
    <script th:src="@{/js/popper.min.js}"></script>
    <link th:href="@{/css/bootstrap.css}" rel="stylesheet">
    <script th:src="@{/js/jquery.min.js}"></script>
</head>

也就是将src和href修改成thymeleaf格式的,要不然之后会出现访问不了的问题
修改xml中的packaging,修改为jar

	<packaging>jar</packaging>

双击Maven中package
在这里插入图片描述
然后得到日志如下
在这里插入图片描述
表示成功输出jar文件
tips:我之前在这个地方的test无法通过,之后找了好久才发现是少了依赖的问题,IDEA缺少mysql-connector-java依赖也是能够运行的,但是部署的时候会提示无法满足依赖问题,所以在pom.xml中加入该依赖项即可

Dockerfile内容

新建一个目录并切换到该目录下,并输入

vim Dockerfile

Dockerfile中的内容如下所示

FROM java:8
MAINTAINER hzw
ADD app.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

稍微解释一下:
使用的是java8版本,用户是hzw,将该文件夹下app.jar 复制到docker根目录下,然后执行java -jar /app.jar
再输入

vim run.sh

run.sh中的内容如下所示

#!/bin/bash
docker build -t wisely/myapps .
docker run -d --name myapps -v /home/image:/home/img -p 8080:8080 wisely/myapps

第一个命令是编译当前目录下的Dockerfile
第二个命令是启动Docker,Docker的name是myapps,将/home/image挂载到Docker中的/home/img,端口是从8080映射到8080,选择的image是上面build的image
这个目录的映射是为了下次我再更新代码时,这个目录中的图片还会存在

通过lrzsz 将jar文件传输到服务器,然后进行一系列操作

rz
mv *.jar app.jar
chmod +x ./run.sh
./run.sh

之后就能够访问了
在这里插入图片描述
也能够通过地址访问图片
在这里插入图片描述
编辑器中显示图片
在这里插入图片描述
这样一个大致的框架就完成了
项目地址https://github.com/hTangle/NeverGetMeWeb

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值