部署 Web 项目到Linux上

目录

环境配置

构建项目

​编辑

数据准备

运行并查看日志

杀掉进程


把程序安装到生产环境上, 这个过程称为 "部署",也叫 "上线"。一旦程序部署成功, 那么这个程序就能被外网中千千万万的普通用户访问到。

环境配置

程序配置文件修改

实际工作中, 开发环境, 测试环境以及生产环境的配置都是不一样的。比如如mysql的用户名和密码 我们可以针对不同的环境, 设置不同的配置。

多平台文件配置

针对不同平台创建不同的配置文件, 要求名字为application-XXX.yml或者application-XXX.properties 。以下以application-XXX.yml为例:

固定格式, 只有 - 后面的字母可以修改。

在配置文件里写不同的内容

开发环境(本地的)

application-dev.yml

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/java_blog_spring?characterEncoding=utf8&useSSL=false
    username: root
    password: #本地环境的数据库密码
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  configuration:
    map-underscore-to-camel-case: true 
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl 
  mapper-locations: classpath:mapper/**Mapper.xml

logging:
  file:
    name: logger/spring-blog.log

生产环境(部署到云服务器上的)

application-prod.yml

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/java_blog_spring?characterEncoding=utf8&useSSL=false
    username: root
    password: #生产环境的数据库密码
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  configuration:
    map-underscore-to-camel-case: true 

  mapper-locations: classpath:mapper/**Mapper.xml

logging:
  file:
    name: /var/logger/spring-blog.log

在主配置文件 application.yml 中指定配置文件, 并删除数据库相关配置

spring:
  profiles:
    active: prod

如果想要不手动敲代码转换生产环境和开发环境,使用Maven调整,则可以在pom.xml中添加配置。

构建项目

打包生产环境

把jar包上传到云服务器上

在云服务上登录MySQL,创建项目数据库

mysql -uroot -p

数据准备

在云服务器上使用MySQL建立表

-- 建表SQL
create database if not exists java_blog_spring charset utf8mb4;
-- ⽤⼾表
DROP TABLE IF EXISTS java_blog_spring.user;
CREATE TABLE java_blog_spring.user(
 `id` INT NOT NULL AUTO_INCREMENT,
 `user_name` VARCHAR ( 128 ) NOT NULL,
 `password` VARCHAR ( 128 ) NOT NULL,
 `github_url` VARCHAR ( 128 ) NULL,
 `delete_flag` TINYINT ( 4 ) NULL DEFAULT 0,
 `create_time` DATETIME DEFAULT now(),
 `update_time` DATETIME DEFAULT now(),
 PRIMARY KEY ( id ),
UNIQUE INDEX user_name_UNIQUE ( user_name ASC )) ENGINE = INNODB DEFAULT CHARACTER
SET = utf8mb4 COMMENT = '⽤⼾表';
-- 博客表
drop table if exists java_blog_spring.blog;
CREATE TABLE java_blog_spring.blog (
 `id` INT NOT NULL AUTO_INCREMENT,
 `title` VARCHAR(200) NULL,
  `content` TEXT NULL,
 `user_id` INT(11) NULL,
 `delete_flag` TINYINT(4) NULL DEFAULT 0,
 `create_time` DATETIME DEFAULT now(),
 `update_time` DATETIME DEFAULT now(),
 PRIMARY KEY (id))
ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT = '博客表';
-- 新增⽤⼾信息
insert into java_blog_spring.user (user_name, password,github_url)values("zhangsan","123456","https://gitee.com/Roylele-java-j");
insert into java_blog_spring.user (user_name, password,github_url)values("lisi","123456","https://gitee.com/Roylele-java-j");
insert into java_blog_spring.blog(title,content,user_id) values("第一篇博客","111我是博客正文我是博客正文我是博客正文",1);
insert into java_blog_spring.blog(title,content,user_id) values("第一篇博客","222我是博客正文我是博客正文我是博客正文",2);

运行并查看日志

nohup : 后台运行程序. 用于在系统后台不挂断地运行命令,退出终端不会影响程序的运行

nohup java -jar 运行的jar包 &

跟踪日志

tail -f xxxx.log

过滤日志

tail -f | grep "Exception"
tail -f | grep "Error"
tail -f | grep "xxxx"

杀掉进程

1. 查看当前服务的进程

ps -ef|grep java

2. 杀掉进程

kill -9 PID

kill -1(重新加载进程)

kill -9(s杀死一个进程)

kill -15(正常停止一个进程)

若再次启动继续使用nohup java -jar xxxx.jar &即可

相关推荐

  1. 部署智能合约 polygon Web3项目三实战之三)

    2024-03-11 21:14:01       38 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-11 21:14:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-11 21:14:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-11 21:14:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-11 21:14:01       18 阅读

热门阅读

  1. 景安空间不支持指定运行目录tp5

    2024-03-11 21:14:01       20 阅读
  2. 面试题 -- 内存管理

    2024-03-11 21:14:01       20 阅读
  3. Qt + mqtt对接阿里云平台(二)

    2024-03-11 21:14:01       23 阅读
  4. 2023 年 3 月青少年软编等考 C 语言一级真题解析

    2024-03-11 21:14:01       21 阅读
  5. Linux信号机制

    2024-03-11 21:14:01       19 阅读
  6. PLIP,openbabel安装避坑

    2024-03-11 21:14:01       29 阅读
  7. LLDB-调试

    2024-03-11 21:14:01       18 阅读
  8. leetcode周赛388(1-3)

    2024-03-11 21:14:01       22 阅读
  9. 网络、UDP编程

    2024-03-11 21:14:01       15 阅读
  10. 安卓 Kotlin 面试题 31-40

    2024-03-11 21:14:01       26 阅读
  11. Rust 泛型使用过程中的 <T> 和 ::<T> 的区别

    2024-03-11 21:14:01       16 阅读