记录 vue + springboot 项目部署在 ubuntu20.04 云服务器上的过程和部分细节 😀

nginx 部署 vue 项目

  • 部署 nginx 环境
1
sudo apt-get install nginx
  • nginx 默认配置文件路径
1
vim /etc/nginx/sites-available/default
  • nginx 模板代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# root为项目路径
# vue项目注意使用try_files这行防止404BUG
server {
listen 9010;
server_name _;
gzip: on; # 如果项目开启了gzip压缩
location / {
root /var/www/html/chess-app/dist;
index index.html index.htm index.nginx-debian.html;
try_files $uri $uri/ /index.html;
}

# 如果vue项目中对跨域进行了proxy配置
location /api {
rewrite ^/api/(.*)$ /$1 break;
proxy_pass http://152.136.154.181:8060; #后台接口地址
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
}
}

部署 springboot 项目

  • 部署 java8 环境
1
sudo apt-get install openjdk-8-jdk
  • 开启 springboot 项目永久部署(使用此方法重新部署时需要杀掉此端口的项目进程)
1
nohup java -jar ToDoList-backend-0.0.1-SNAPSHOT.jar &
  • 查看端口占用(springboot 默认端口号为 8060)
1
sudo lsof -i:8060
  • 关闭指定 PID 的进程
1
sudo kill -9 <PID>
  • 可用脚本自动化上述过程
1
2
3
4
5
6
7
8
#! /bin/bash
kill -9 `sudo lsof -i:8060`

rm nohup.out

nohup java -jar ToDoList-backend-0.0.1-SNAPSHOT.jar &

echo 'already rebuild! -- http://152.136.154.181:8060/api/test'

部署 mysql

该部分原作者 Zzzz0zzzZ (Zzzz0_0zzzZ) (github.com) 😘

  • 部署 mysql 环境
1
2
sudo apt install mysql-server
sudo apt-get install mysql-client
  • 创建用户设置密码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 查看默认⽤户名、密码
sudo cat /etc/mysql/debian.cnf

# 登陆root⽤户
sudo mysql -u "⽂件中的user名" -p

# -->⾸次使⽤sudo,先输⼊sudo密码,然后输⼊user的密码
# 进⼊mysql,创建新⽤户和密码,并授权
create user "⽤户名"@"%" identified by "密码";
flush privileges;
grant all privileges on *.* to "⽤户名"@"%";

# 注:删除⽤户
drop user "⽤户名"@"%"
  • 修改配置文件
1
2
3
# 使⽤vim编辑器,注意⽂件是mysqld.cnf,别少了'd'
# 找到bind-address,修改127.0.0.1为0.0.0.0,代表全ip可⽤
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
  • 常用命令
1
2
3
4
5
6
7
8
9
10
11
12
sudo service mysql start
sudo service mysql stop
sudo service mysql restart
# 登录mysql
sudo mysql -u ⽤户名 -p

# 查看数据库
show databases;
# 进入数据库,使用use可直接切换数据库,无需退出
use <`database`>
# 查看数据库中的表
show tables;

部署Redis

2022.12.06 是个值得纪念的日子,首次被黑客攻击了捏,就是因为部署时候没有设置密码

  • 使用sudo vim /etc/redis/redis.conf 编辑配置文件 redis.conf
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
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
bind 127.0.0.1 <服务器地址>

protected-mode no

port <端口号>

tcp-backlog 511

requirepass <密码>

timeout 500

tcp-keepalive 300

daemonize yes

supervised no

pidfile /var/run/redis_5378.pid

loglevel notice

logfile ""

databases 30

always-show-logo yes

save 900 1
save 300 10
save 60 10000

stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

dbfilename dump.rdb

dir ./

replica-serve-stale-data yes

replica-read-only yes

repl-diskless-sync no

repl-disable-tcp-nodelay no

replica-priority 100

lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no

appendonly yes

appendfilename "appendonly.aof"

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

aof-load-truncated yes

aof-use-rdb-preamble yes

lua-time-limit 5000

slowlog-max-len 128

notify-keyspace-events ""

hash-max-ziplist-entries 512
hash-max-ziplist-value 64

list-max-ziplist-size -2

list-compress-depth 0

set-max-intset-entries 512

zset-max-ziplist-entries 128
zset-max-ziplist-value 64

hll-sparse-max-bytes 3000

stream-node-max-bytes 4096
stream-node-max-entries 100

activerehashing yes

hz 10

dynamic-hz yes

aof-rewrite-incremental-fsync yes

rdb-save-incremental-fsync yes
  • 修改配置文件后重启
1
2
cd /etc/redis
/bin/redis-server redis.conf
  • redis-cli -p <端口> -a <密码> 连接进入redis

部署MongoDB

MongoDB 建议使用 docker 进行部署

  • 拉取镜像 docker pull mongo:latest
  • 创建并运行容器 docker run -itd --name mongo-test -p <自定义端口号>:27017 mongo --auth
  • 进入容器 docker exec -it mongo-test mongosh
  • 创建管理员和用户
1
2
3
4
5
6
use admin

#创建管理员用户
db.createUser({user: "admin",pwd: "ABC",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]})
db.auth("admin", "ABC")
db.createUser({user: 'tea',pwd: 'abc',roles: [{role: "readWrite", db: "admin"}]})