場景
若依官網:
前提:
服務器上安裝Mysql,並將數據庫導入,在SpringBoot中的application-druid.yml配置mysql數據庫連接。
服務器安裝Redis服務端,並在application.yml中配置連接。
具體可以參照:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/107486313
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。
實現
后端部署
首先將后台項目進行打包,在application.yml配置后台端口,這里是8080。

然后在IDEA中將后台系統打成jar包,然后將jar包復制到服務器上,然后運行后台jar包。
這塊比較簡單,可以具體參照下面的官網教程。
后端部署
bin/package.bat 在項目的目錄下執行
然后會在項目下生成 target文件夾包含 war 或jar (多模塊生成在ruoyi-admin)
1、jar部署方式
使用命令行執行:java –jar ruoyi.jar 或者執行腳本:bin/run.bat
2、war部署方式
pom.xml packaging修改為war 放入tomcat服務器webapps
提示
SpringBoot去除內嵌tomcat
<!-- 多模塊排除內置tomcat --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!-- 單應用排除內置tomcat --> <exclusions> <exclusion> <artifactId>spring-boot-starter-tomcat</artifactId> <groupId>org.springframework.boot</groupId> </exclusion> </exclusions>
這里推薦使用Nginx代理的方式,所以需要將其打成jar包運行的方式,不用再搭建Tomcat運行war包。
最終將后台項目運行之后,訪問后台接口
http://localhost:8080/captchaImage
此接口是登錄時的驗證碼接口,是不需要權限驗證的。
對應的后台接口位置
com.ruoyi.project.common下的CaptchaController

設置此接口放開不需要驗證的配置的地方

部署成功后台項目后訪問驗證碼接口如果出現以下界面則是部署成功

前端部署
這里是用VSCode作為前端的IDE,首先打開前端項目的vue.config.js
找到devServer下的proxy代理部分

這里的target是代理的請求后台的接口,對應上面部署的后台接口的url,下面還有一個路徑重寫,添加了一個
process.env.VUE_APP_BASE_API]:
此路徑的配置在

所以在使用
npm run dev
運行項目時,請求都會被代理到你設置的localhost:8080/dev-api下
所以要保證你在自己的本地運行前端項目和后台項目后能代理成功,即本地的前端能訪問到本地的后台接口。
接下來將前端打包。
首先打包之前確保已經安裝完依賴項,即
npm install 成功且沒問題。
然后打開vue.config.js


首先將上面的這個端口改為你要在打包后訪問的端口,即使用nginx代理前的接口。
這個端口默認是80端口,這里把其修改為不會沖突的70端口,不推薦使用80端口。
因為80端口是默認端口在部署到服務器上和下面啟動nginx可能存在占用等問題。
除了這個70端口外,下面的target的url和端口要和你服務器上能訪問到后台的接口一致。
然后在VSCode中新建終端,或者在前端項目下打開cmd
npm run build:prod
進行前端項目的打包


打包前端成功之后

此時會在項目目錄下生成dist目錄

此目錄就是打包之后的前端的資源。
然后將此dist目錄放在服務器上的某個目錄下,下面使用Nginx代理會用

不要動dist下文件的路徑

下載安裝Nginx
Nginx下載地址
http://nginx.org/en/download.html
這里點擊相應版本的Windows版本

下載之后是一個壓縮包,將其解壓到服務器上的某個目錄

Nginx代理配置
進入到上面解壓的conf目錄下,編輯Nginx的配置文件nginx.conf

找到server節點

首先這里的listen下的端口就是代理前的接口,要與前面前端項目的vue.config.js中的端口一致。
server { listen 70; server_name 10.229.36.158;
然后下面的server_name是你服務器的ip,這里即使是使用的本地也建議不要用localhost,避免修改hosts文件導致的問題。
所以這里就直接設置你要部署項目的服務器的ip。
然后下面的location /下面配置的就是代理前前端靜態資源的路徑等。
root 對應的就是在服務器上前端資源的dist目錄的全路徑,即代表根路徑。
下面的兩個配置保持默認不要更改,配置的是防止404和入口頁面。
location / { root D:/www/kaoqin/dist/; try_files $uri $uri/ /index.html; index index.html index.htm; }
然后再下面的location /prod-api/ 就是配置的代理后的地址。
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/; }
這里的 /prod-api/就是跟前面前端項目設置代理的路徑重寫一致。
下面的一些是設置請求頭等,防止出現跨域問題。
然后最下面的proxy_pass就是設置的代理后的地址,即你的服務器上后台接口的url。
通過上面兩個配置就能實現在服務器上所有的請求
只要是通過
http://10.229.36.158/70/dev-api/
發送過來的請求全部會被代理到
下。這樣就能實現前后端項目的請求代理。
完整conf代碼
#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 70; server_name 10.229.36.158; #charset koi8-r; #access_log logs/host.access.log main; location / { root D:/www/kaoqin/dist/; try_files $uri $uri/ /index.html; index index.html index.htm; } 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/; } # 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
來到上面Nginx解壓之后的目錄下(服務器上)即含有nginx.exe的目錄下,在此處打開命令行
start nginx.exe
啟動nginx

如果對nginx的配置文件進行修改的話
nginx -s reload
如果沒配置環境變量或者提示不行的話前面使用nginx.exe的全路徑。
正常停止或關閉Nginx:
nginx -s quit
啟動Nginx成功后打開瀏覽器驗證,輸入
如果能出現頁面,即對應的前端靜態資源的index.html的頁面,並且能顯示驗證碼,驗證碼是通過代理后的
后台接口獲取。那么就是代理成功,記住不要關閉此nginx的命令行。

如果訪問服務器上的地址不成功后檢查70端口是否開放
控制面板-防火牆-高級設置

入站規則-新建規則-端口,添加70

點擊下一步

選擇允許連接


配置連接域點擊下一步


設置名稱點擊保存。
