一、云服务器开通
(一)腾讯云服务器开通
腾讯云服务器官网搜索云服务器购买后重置密码:
(二)管理远程服务器工具
eleterm下载 新建书签配置ssh连接:
二、前端部署
1、更改生产环境配置路径
在.env.production文件中配置:
VITE_APP_BASE_URL=‘http://域名或云服务器ip+niginx监听端口号’
2、项目打包
在Vue3项目中执行 :pnpm run build 会产生dist文件夹
3、将项目上传至云服务器
上传目录:/root/www
上传工具:eleterm
三、nginx安装及使用
(一)nginx安装
1、配置Nginx yum存储库
创建/etc/yum.repos.d/nginx.repo
文件
vim /etc/yum.repos.d/nginx.repo
增加如下内容
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
2、在线安装Nginx
执行以下命令,安装Nginx
yum -y install nginx
3、启动Nginx
执行以下命令启动Nginx
systemctl start nginx
执行以下命令查看Nginx运行状态
systemctl status nginx
执行以下命令设置开机自启
systemctl enable nginx
(二)使用Nginx作为静态资源服务器
执行以下命令
cd /etc/nginx
执行以下命令进行nginx.conf配置
vim nginx.conf
添加如下内容
server {
listen 8080; #默认监听端口号
server_name 192.168.29.102; #云服务器ip地址
location / {
root /root/www/dist; #默认静态资源路径
index index.html; #默认执行文件类型
}
}
重新加载Nginx的配置文件
systemctl reload nginx
清理临时文件
rm .nginx.conf.swp
rm .nginx.conf.swo
四、后端部署
(一)项目打包并上传:
终端执行命令:
mvn clean
mvn package
在 web - admin 和 web - app 模块的target
目录下找到web-admin-1.0-SNAPSHOT.jar
和web-app-1.0-SNAPSHOT.jar
。
将后端项目的两个jar包上传到server01
服务器的/opt/lease
目录下,若目录不存在,自行创建即可。
创建命令:
mkdir /opt/lease
移动jar包命令:
cd \www
mv web-admin-1.0-SNAPSHOT.jar /root
mv web-admin-1.0-SNAPSHOT.jar /opt/lease
(二)云服务器安装JDK
1、可在服务器执行以下命令可直接下载(版本需和项目版本一直)
wget https://download.oracle.com/java/17/archive/jdk-17.0.8_linux-x64_bin.tar.gz
2、解压JDK安装包
执行以下命令将jdk解压到/opt
目录
tar -zxvf jdk-17.0.8_linux-x64_bin.tar.gz -C /opt
3、 测试JDK安装效果
执行以下命令,观察输出是否正常
/opt/jdk-17.0.8/bin/java -version
(三)安装MySQL
-
安装MySQL yum库
-
下载yum库
下载地址为https://dev.mysql.com/downloads/repo/yum/。需要根据操作系统选择相应版本,Centos7需选择
mysql80-community-release-el7-9.noarch.rpm
。执行以下命令可直接下载到服务器
wget https://dev.mysql.com/get/mysql80-community-release-el7-9.noarch.rpm
-
安装yum库
在上述
rpm
文件所在路径执行如下命令rpm -ivh mysql80-community-release-el7-9.noarch.rpm
-
配置国内镜像
修改
/etc/yum.repo.d/mysql-community.repo
文件中的[mysql80-community]
中的baseUrl
参数,修改内容如下:[mysql80-community] name=MySQL 8.0 Community Server baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-8.0-community-el7-$basearch/ enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
-
-
安装MySQL
执行如下命令安装MySQL
yum install -y mysql-community-server
-
启动MySQL
执行如下命令启动MySQL服务
systemctl start mysqld
执行以下命令查看MySQL运行状态
systemctl status mysqld
-
root用户相关配置
-
查看root用户初始密码
MySQL启动后会将root用户的初始密码写入日志,通过以下命令可以获取密码
cat /var/log/mysqld.log | grep password
-
使用初始密码登录
执行以下命令登录MySQL
mysql -uroot -p'password'
-
修改root用户密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Yourpassword123';
注意:MySQL默认安装了validate_password 插件,默认情况下,要求密码要包含大写字母、小写字母、数字和特殊符号,且密码长度最小为8。若需设置简单密码,可禁用该插件,或调整该插件的密码强度级别。
-
授予root用户远程登录权限
CREATE USER 'root'@'%' IDENTIFIED BY 'Yourpassword123'; GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES;
-
(四)部署Redis
安装方式采用yum在线安装,安装版本为redis-7.0.13
,具体步骤如下
-
安装Redis yum仓库
-
下载yum仓库
Redis所在的仓库为remi-release,下载地址为:http://rpms.famillecollet.com/enterprise/remi-release-7.rpm,可使用如下命令直接下载到服务器
wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
-
安装yum仓库
执行如下命令进行安装
rpm -ivh remi-release-7.rpm
-
-
安装Reids
执行以下命令安装Redis
yum --enablerepo=remi -y install redis-7.0.13
注:
--enablerepo
选项的作用为启用一个仓库 -
配置Redis允许远程访问
Redis服务默认只允许本地访问,若需要进行远程访问,需要做出以下配置。
修改Redis配置文件
vim /etc/redis/redis.conf
修改如下参数
#监听所有网络接口,默认只监听localhost bind 0.0.0.0 #关闭保护模式,默认开启。开始保护模式后,远程访问必须进行认证后才能访问。 protected-mode no
-
启动Redis
执行以下命令启动Redis
systemctl start redis
执行以下命令查看Redis的运行状态
systemctl status redis
执行以下命令设置Redis开机自启
systemctl enable redis
(五)部署MinIO
安装方式采用rpm离线安装,具体步骤可参考官方文档。
-
获取MinIO安装包
下载地址如下:https://dl.min.io/server/minio/release/linux-amd64/archive/minio-20230809233022.0.0.x86_64.rpm,通过以下命令可直接将安装包下载至服务器
wget https://dl.min.io/server/minio/release/linux-amd64/archive/minio-20230809233022.0.0.x86_64.rpm
-
安装MinIO
rpm -ivh minio-20230809233022.0.0.x86_64.rpm
-
集成Systemd
-
Systemd概述
Systemd
是一个广泛应用于Linux系统的系统初始化和服务管理器,其可以管理系统中的各种服务和进程,包括启动、停止和重启服务,除此之外,其还可以监测各服务的运行状态,并在服务异常退出时,自动拉起服务,以保证服务的稳定性。系统自带的防火墙服务firewalld
,我们自己安装的mysqld
和redis
均是由Systemd
进行管理的,此处将MinIO服务也交给Systemd管理。 -
编写MinIO服务配置文件
Systemd所管理的服务需要由一个配置文件进行描述,这些配置文件均位于
/etc/systemd/system/
或者/usr/lib/systemd/system/
目录下,下面创建MinIO服务的配置文件。执行以下命令创建并打开
minio.service
文件vim /etc/systemd/system/minio.service
内容如下,具体可参考MinIO官方文档。
[Unit] Description=MinIO Documentation=https://min.io/docs/minio/linux/index.html Wants=network-online.target After=network-online.target AssertFileIsExecutable=/usr/local/bin/minio [Service] WorkingDirectory=/usr/local ProtectProc=invisible EnvironmentFile=-/etc/default/minio ExecStartPre=/bin/bash -c "if [ -z \"${MINIO_VOLUMES}\" ]; then echo \"Variable MINIO_VOLUMES not set in /etc/default/minio\"; exit 1; fi" ExecStart=/usr/local/bin/minio server $MINIO_OPTS $MINIO_VOLUMES Restart=always LimitNOFILE=65536 TasksMax=infinity TimeoutStopSec=infinity SendSIGKILL=no [Install] WantedBy=multi-user.target
注意:
重点关注上述文件中的以下内容即可
EnvironmentFile
,该文件中可配置MinIO服务所需的各项参数ExecStart
,该参数用于配置MinIO服务的启动命令,其中$MINIO_OPTS
、$MINIO_VOLUMES
,均引用于EnvironmentFile
中的变量。MINIO_OPTS
用于配置MinIO服务的启动选项,可省略不配置。MINIO_VOLUMES
用于配置MinIO服务的数据存储路径。
Restart
,表示自动重启
-
编写
EnvironmentFile
文件执行以下命令创建并打开
/etc/default/minio
文件vim /etc/default/minio
内容如下,具体可参考官方文档。
MINIO_ROOT_USER= MINIO_ROOT_PASSWORD= MINIO_VOLUMES=/data MINIO_OPTS="--console-address :9001"
注意
-
MINIO_ROOT_USER
和MINIO_ROOT_PASSWORD
为用于访问MinIO的用户名和密码,密码长度至少8位。 -
MINIO_VOLUMES
用于指定数据存储路径,需确保指定的路径是存在的,可执行以下命令创建该路径。mkdir /data
-
MINIO_OPTS
中的console-address
,用于指定管理页面的地址。
-
-
-
启动MinIO
执行以下命令启动MinIO
systemctl start minio
执行以下命令查询运行状态
systemctl status minio
设置MinIO开机自启
systemctl enable minio
(六)设置防火墙
1、腾讯云服务器添加防火墙规则
- 注:需要根据具体的后端代码来配置
server: port: 8080 spring: datasource: type: com.zaxxer.hikari.HikariDataSource url: jdbc:mysql://:3306/lease?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2b8 username: root password: hikari: connection-test-query: SELECT 1 # 自动检测连接 connection-timeout: 60000 #数据库连接超时时间,默认30秒 idle-timeout: 500000 #空闲连接存活最大时间,默认600000(10分钟) max-lifetime: 540000 #此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟 maximum-pool-size: 12 #连接池最大连接数,默认是10 minimum-idle: 10 #最小空闲连接数量 pool-name: SPHHikariPool # 连接池名称 jackson: time-zone: GMT+8 data: redis: host: port: 6379 database: 0 #用于打印框架生成的sql语句,便于调试 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl minio: endpoint: http://119.29.195.76:9000 access-key: secret-key: bucket-name: springdoc: default-flat-param-object: true
2、ssh客户端设置
-
启动防火墙命令
systemctl start firewalld
-
设置端口永久打开
sudo firewall-cmd --zone=public --add-port= xxxx /tcp --permanent
-
重启防火墙服务
firewall-cmd --reload
-
检查是否打开
firewall-cmd --list-all
-
前后端项目运行后测试远程端口是否可以访问:
后台运行java项目:nohup /opt/jdk-17.0.8/bin/java -jar /opt/lease/web-admin-1.0-SNAPSHOT.jar &
-
单独运行
/opt/jdk-17.0.8/bin/java -jar /opt/lease/web-admin-1.0-SNAPSHOT.jar
补充:
- 关闭端口:
firewall-cmd --zone=public --remove-port=xxxx/tcp --permanent
- 设置防火墙开机启动:
systemctl enable firewalld.service
- 产看进程并关闭:
sudo netstat -ntpl sudo kill 进程号
五、nginx 反向代理
Nginx实现反向代理:详细配置与代码注释
nginx.conf:
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
root /root/www/dist; #PC端
index index.html index.html;
}
location /admin {
proxy_pass http://云服务器ip:8080;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 81;
listen [::]:81;
server_name _;
location / {
root /root/www/dist_app; #移动端
index index.html index.html;
}
location /app {
proxy_pass http://云服务器ip:8081;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
配置错误时查看错误日志:
cat /var/log/nginx/error.log;