这一章主要是讲解一下怎么部署上线项目
腾讯云服务器一台(2核2g)、域名一个 (可有可无)
说明:当然了服务器的配置根据项目来选择,2核2g跑这个博客小项目是完全没有问题的
用yum 去安装
比如要安装java ,百度搜 yum -y install java
比如要安装数据库,百度搜 yum如何安装mysql 5.7
好吧我承认当初安装时看的教程文章随手搜的,网址没保存,后期我找了好的教程会补充上地址
1、安装docker
- # 1、yum 包更新到最新
- yum update
- # 2、安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的
- yum install -y yum-utils device-mapper-persistent-data lvm2
- # 3、 设置yum源
- yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
- # 4、 安装docker,出现输入的界面都按 y
- yum install -y docker-ce
- # 5、 查看docker版本,验证是否验证成功
- docker -v
- #启动docker
- /bin/systemctl start docker.service
2、拉取镜像
- docker pull nginx
- docker pull redis:5.0.3
- docker pull java:8
- docker pull mysql:5.7
3、mysql配置
创建容器,设置端口映射、目录映射
- mkdir /mnt/docker/mysql
- cd /mnt/docker/mysql
- docker run -id \
- -p 3307:3306 \
- --name=c_mysql \
- -v /mnt/docker/mysql/conf:/etc/mysql/conf.d \
- -v /mnt/docker/mysql/logs:/logs \
- -v /mnt/docker/mysql/data:/var/lib/mysql \
- -e MYSQL_ROOT_PASSWORD=123456 \
- mysql:5.7
在/mnt/docker/mysql/conf 创建my.cnf
- [mysqld]
- #
- # Remove leading # and set to the amount of RAM for the most important data
- # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
- # innodb_buffer_pool_size = 128M
- #
- # Remove leading # to turn on a very important data integrity option: logging
- # changes to the binary log between backups.
- # log_bin
- #
- # Remove leading # to set options mainly useful for reporting servers.
- # The server defaults are faster for transactions and fast SELECTs.
- # Adjust sizes as needed, experiment to find the optimal values.
- # join_buffer_size = 128M
- # sort_buffer_size = 2M
- # read_rnd_buffer_size = 2M
- datadir=/var/lib/mysql
- socket=/var/lib/mysql/mysql.sock
- character-set-server=utf8
- # Disabling symbolic-links is recommended to prevent assorted security risks
- symbolic-links=0
- lower_case_table_names=1
- pid-file=/var/run/mysqld/mysqld.pid
- sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
注意:这里端口用宿主机的3306,数据库也用宿主机的,也就是你服务器的ip和端口,因为我用docker的那个ip和端口数据页面数据显示不出来
- #去查看mysql对应的docker容器ip地址,配置到项目
- docker inspect c_mysql
4、redis
docker run -id --name=redis -p 6379:6379 redis:5.0.3
5、定义dockerfile,发布springboot项目
将spring boot项目打包,上传到以下目录,并将jar包重命名为blog_api.jar
切换到/mnt/docker/app目录
vim blog_dockerfile
- FROM java:8
- MAINTAINER mszlu <test@mszlu.com>
- ADD ./blog_api.jar /app.jar
- CMD java -jar /app.jar --spring.profiles.active=prod
在本目录下构建执行命令
docker build -f ./blog_dockerfile -t app .
6、安装Docker Compose
- # Compose目前已经完全支持Linux、Mac OS和Windows,在我们安装Compose之前,需要先安装Docker。下面我 们以编译好的二进制包方式安装在Linux系统中。
- curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
- # 设置文件可执行权限
- chmod +x /usr/local/bin/docker-compose
- # 查看版本信息
- docker-compose -version
7、创建docker-compose目录
- mkdir /mnt/docker/docker-compose
- cd /mnt/docker/docker-compose
8、编写 docker-compose.yml 文件
- version: '3'
- services:
- nginx:
- image: nginx
- container_name: nginx
- ports:
- - 80:80
- - 443:443
- links:
- - app
- depends_on:
- - app
- volumes:
- - /mnt/docker/docker-compose/nginx/:/etc/nginx/
- - /mnt/mszlu/blog:/mszlu/blog
- network_mode: "bridge"
- app:
- image: app
- container_name: app
- expose:
- - "8888"
- network_mode: "bridge"
9、创建./nginx目录
mkdir -p ./nginx
10、在./nginx目录下 编写nginx.conf文件
-
- user nginx;
- worker_processes 1;
-
- error_log /var/log/nginx/error.log warn;
- pid /var/run/nginx.pid;
-
-
- events {
- worker_connections 1024;
- }
-
-
- http {
- include /etc/nginx/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 /var/log/nginx/access.log main;
-
- sendfile on;
- #tcp_nopush on;
-
- keepalive_timeout 65;
-
- #gzip on;
-
- include /etc/nginx/conf.d/*.conf;
- }
-
11、在nginx下创建conf.d目录,conf.d下创建blog.conf
无域名情况下:
- gzip_min_length 1k;
- gzip_buffers 4 16k;
- gzip_comp_level 2;
- gzip_vary off;
- upstream appstream{
-
- server app:8888;
- }
-
-
- server{
- listen 80;
- server_name localhost;
- location /api {
- proxy_pass http://appstream;
- }
-
- location / {
- root /mszlu/blog/;
- index index.html;
- }
- location ~* \.(jpg|jpeg|gif|png|swf|rar|zip|css|js|map|svg|woff|ttf|txt)$ {
- root /mszlu/blog/;
- index index.html;
- add_header Access-Control-Allow-Origin *;
- }
- }
有域名情况下:
- gzip_min_length 1k;
- gzip_buffers 4 16k;
- gzip_comp_level 2;
- gzip_vary off;
- upstream appstream{
-
- server app:8888;
- }
-
-
- server{
- listen 80;
- server_name blog.lcheng.fun;
- location /api {
- proxy_pass http://appstream;
- }
-
- location / {
- root /mszlu/blog/;
- index index.html;
- }
- location ~* \.(jpg|jpeg|gif|png|swf|rar|zip|css|js|map|svg|woff|ttf|txt)$ {
- root /mszlu/blog/;
- index index.html;
- add_header Access-Control-Allow-Origin *;
- }
- }
部署前端
1、项目打包,运行npm run build
2、打完包后会出现一个dist文件夹,里面有static和index.html,dist.zip自己压缩软件压缩即可
3、连上我们的服务器
4、切换到/mnt/mszlu/blog目录
5、打开xftp把打包的前端文件上传到服务器现在的目录
6、查看是否上传成功
切换到/mnt/docker/docker-compose目录下,使用docker-compose 启动容器
- docker-compose up
-
- docker-compose up -d #代表后台启动