前面的文章介绍了使用IDEA搭建一个若依的前后端分离的简单页面,我们修改完电脑应该如何将程序更新到服务器上呢?
这篇文章简单介绍一下打包部署的流程。
注意:本文假设MySQL、Redis已经在Windows端和Linux端全部安装完毕,且正常运行状态。
1.后台打包
后台打包的命令很简单,首先在控制台中打开项目的路径。
(可以右击项目,选择“打开于”——>”终端”)
注意:打包之前要把配置文件改成生产环境的配置,特别是数据库之类的,不然打包后的程序访问的是错误的数据。

在控制台中输入:
mvn clean install
就可以看到maven在下载相关的依赖了。

这个过程可能会比较长,最后可以看到出现了“SUCCESS”的标记。花费了1分钟56秒。

上面这个操作实际上打了很多jar包,但是若依都给我们整理成一个了。位置在ruoyi-admin文件夹中。

上面的这个target文件中的ruoyi-admin.jar就是后台打包后的程序。
把这个文件复制出来备用。
2.前台打包
注意:前端打包之前也要确定一下服务器上后端的地址和端口,

上面这处要填写服务器上后端的地址和端口,不然会访问不到。
前台打包也很简单,我们首先切换控制台到前端的工作目录ruoyi-ui

在控制台中输入
npm run build:prod

前端打包过程可能比后端要长一些。

打包后,会生成一个“dist”文件夹,在ruoyi-ui文件夹底下, 这个dist文件夹里面就是打完包以后的前端代码,建议压缩一下,方便传到服务器上。
3.Windows上运行
(1)运行后端
找一个windows服务器(或者直接使用开发电脑),确保一下windows环境中支持java,本文直接使用开发环境运行后端。另外需要确定生产环境的数据库配置等信息要和代码中配置的一样。
另外,如果你此时使用IDEA运行后端,请先停掉IDEA,因为会出现端口冲突的问题。
我们找到后端所在的位置,本文直接使用了打包后的target目录。
我们找到后台包所在的位置,按住shift+鼠标右键,弹出菜单后,
点击“在此处打开PowerShell”窗口

在此处输入
java -jar ruoyi-admin.jar

看到下面的界面表示后台启动成功了。

(2)运行前端
前端我们使用nginx作为web服务器,nginx基础教程如下:
windows下如何下载安装配置nginx – 每天进步一点点
我们已经把前台文件打包好了,直接将dist文件复制到nginx目录中。

然后修改一下配置那文件,在conf文件夹中

我将root后面的内容改为dist

配置文件如下,如果按照我的方式,下面的可以直接复制
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8085;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root dist;
index index.html index.htm;
}
# 代理后端 API 请求到 8080 端口
location /prod-api/ {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080/; # 关键!确保这里是后端真实端口
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
然后保存,回到上一级,双击nginx.exe(注意,双击后一闪而过是正常的)
在浏览器中输入
localhost:8085
因为nginx中代理的端口是8085,所以这里访问的是8085端口。
