RuoYi's front-end and back-end are separated and the back-end multi-module is deployed to the server through docker after secondary development (latest on 2022.11.15)

1.Environment preparation

1.centos 7
2.docker
3.mysql  8.0.16
4.java   8
5.nginx  latest
6.redis  latest

2. Modification of project configuration files and project packaging

2.1 Backend file modification

application.yml Modify the HTTP port of the server, the default is 8080, modify it according to needs

application-druid.yml modifies the database mysql and redis connection configuration

 ​​​​​

 2.2 Backend project packaging

Although RruoYI is multi-module, the ruoyi-admin module depends on other modules, so you only need to obtain the jar package of the admin module , but it needs to be packaged globally when packaging.

Find the root directory of the project for global packaging. Then find the generated target directory under the ruoyi-admin module and store ruoyi-admin.jar on the desktop to facilitate subsequent transfer to the server.

 

 2.3 Modification of front-end files

①Modify the vue.config.js file

 2.4 Front-end packaging

 ① Run the npm run build:prod command to generate a dist folder and store it on the desktop to facilitate subsequent transfer to the server.

2.5. File transfer to server

Connect to the server through xshell, and then transfer dist and ruoyi-admin.jar to the server through Xftp

 3. Server side

1. Software: downloading docker and pulling images of nginx, redis, and mysql

2. Mirror startup:

mysql:

docker run \
-p 3306:3306 \
--name mysql \
--privileged=true \ //让容器具有root的权限
--restart unless-stopped \ //自动重启
-e MYSQL_ROOT_PASSWORD=****** \ //密码谨慎配置不要123456了,不然黑客会找到你!!!
-d mysql \
--lower_case_table_names=1  //建表时候所有字符转为小写

Note: In addition to setting a complex password, it is best to modify the default root username of MySQL so that it will be more difficult for hackers to brute force.

 redis:

docker run \
-p 6379:6379 \
--name redis \
--restart=always \
-v /home/redis/data/:/data \
-d redis:3.2 redis-server \
--appendonly yes  //必须持久化

Note: When running the image, be sure to set the persistence operation --appendonly yes. Then after running the redis image, it is best to enter redis to set the corresponding password, otherwise you will suffer a lot later (you can also set the password with --requirepass 123456, but Sometimes it doesn't work).

1.进入redis
docker exec -it 容器id /bin/bash

2.运行命令:
redis-cli

3.查看现有的redis密码:
config get requirepass

4.设置redis密码
config set requirepass 密码

5.exit

nginx:

docker run --privileged  -p 80:80 -p 443:443 \
  -d --restart=always \
 --name nginx \
-v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /home/nginx/conf/conf.d:/etc/nginx/conf.d:ro \
-v /home/nginx/html:/usr/share/nginx/html:rw \
-v /home/nginx/logs:/var/log/nginx -d nginx

Note: The mounting directory of nginx needs to be created in advance, otherwise it will crash immediately when starting nginx.

Then enter the nginx.conf configuration file and add the following content

 server {
        listen       80;
        server_name  服务器ip;		# 用服务器ip代替

        location / {
            root   /usr/share/nginx/html/dist/;
            index  index.html index.htm index login;
            try_files $uri $uri/ /index.html last;
        }
        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://服务器ip:8085/;		# 可以用服务器ip代替
        }
        error_page   500 502 503 504  /50x.html;
            location = /50x.html {
            root   html;
        }
     }

Note: After adding the above content to nginx.conf, you need to modify the default.conf file in the /home/nginx/conf/conf.d folder at the same time. Because nginx.conf introduces default.conf, you need to change default. Delete the corresponding server part of the conf file, otherwise the front-end configuration file /usr/share/nginx/html/dist/ will not take effect. But you can also only change the server part of the default.conf file without changing the nginx.conf file.

3.Creation of DockerFile

3.1 First put the ruoyi-admin.jar package and dockerFile file in the same directory

3.2 Create

FROM java:8
EXPOSE 8085

VOLUME /tmp

ENV TZ=Asia/Shanghai
RUN ln -sf /usr/share/zoneinfo/{TZ} /etc/localhosttime && echo "{TZ}" > /etc/timezone

ADD ruoyi-admin.jar /app.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-jar","/app.jar"]

Run the following command to generate an image:

docker build -t ruoyi-vue .

Note: Don’t miss one .

Use docker images to check whether the five images of redis, mysql, nginx, ruoyi-vue, and java exist

Then run the image:

docker run -d -p 8085:8085 --name vue ruoyi-vue 

Check the running image through the docker ps command. Redis, mysql, nginx, and ruoyi-vue are all running normally.

Finally, you can access it through the normal browser through ip: 8085! ! !

Reminder : 1. If you find that the startup is successful when starting the image, but after a while you can’t see it is running using docker ps, you can use docker logs container id to check the log, and then solve the problem by reporting the corresponding error.

2. If the functions in your springBoot project are related to the system time, you need to unify the system time with the real time, otherwise your project will report an error!

3. When starting the image, it is best to set --restart=always. At the same time, docker should also be set to start automatically on boot to prevent unexpected exit.

4. After all tasks are completed, set the firewall to open the specified port (80, 3306, 6379, 8085), and then turn on the firewall! ! !

I wish you all the best! ! !

Guess you like

Origin blog.csdn.net/weixin_47450271/article/details/127867272