项目部署实践
发表于:2023-09-05 | 分类: 实用技术

项目部署

方式一: nginx 部署

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 下载nginx
24 cd services/
25 curl -o nginx-1.24.0.tar.gz https://nginx.org/download/nginx-1.24.0.tar.gz
# 解压
26 tar -zxvf nginx-1.24.0.tar.gz
27 ls
# 进入解压目录
28 cd nginx-1.24.0/
29 ls
# 运行配置文件
30 ./configure
# 安装pcre
31 apt-get install libpcre3 libpcre3-dev
32 ./configure
# 安装zlib 开发版
sudo apt install zlib1g-dev
# 安装openssl
33 apt-get install openssl libssl-dev
34 ./configure
# 设置系统配置参数
37 ./configure --with-http_ssl_module --with-http_v2_module --with-stream
# 开始编译
39 make
# 安装
40 make install

1
2
# 进入目录配置环境变量
vim /etc/profile

新增环境变量

1
export PATH=$PATH:/usr/local/nginx/sbin	
1
2
3
4
5
6
7
# 重新激活环境变量
source /etc/profile
# 启动nginx
nginx
# 查看端口启动情况
netstat -ntlp

部署前端项目
1
2
3
4
5
6
7
8
9
10
11
12
# 放文件入服务器
# 下载解压软件
82 apt install unzip
# 解压
83 unzip dist.zip -d user_center_b
# 删除多余目录
84 cd user_center_b/dist
85 mv * ../
86 cd ../
87 rm -r dist
# 修改配置文件
88 vim /usr/local/nginx/conf/nginx.conf

修改配置文件

1
2
3
4
location/{
root /root/services/user_center_b; # 修改后的 配置目录
index index.html index.htm;
}

修改权限

1
user root;
1
2
3
4
# 重新加载nginx
nginx -s reload
# 查看nginx 详细
ps -ef|grep 'nginx'

访问公网IP响应

403 Forbidden

部署宝塔

1
2
3
4
外网面板地址: https://47.92.126.51:29319/4e1eb856
内网面板地址: https://172.17.144.26:29319/4e1eb856
username: qysqtvoz
password: f27640e0
后端部署

java、maven

1
2
3
4
5
6
7
8
# 安装jdk
apt install -y openjdk-8-jdk # 因为使用apt安装的java 所以不用配置环境变量
# 安装maven
curl -o apache-maven-3.9.4-bin.tar.gz https://dlcdn.apache.org/maven/maven-3/3.9.4/binaries/apache-maven-3.9.4-bin.tar.gz
# 解压
tar -zxvf apache-maven-3.9.4-bin.tar.gz
# 配置环境变量
vim /etc/profile

最后一行添加为maven文件路径

1
export PATH=$PATH:/usr/local/nginx/sbin:/root/services/apache-maven-3.9.4/bin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 刷新配置文件
source /etc/profile
# mvn -v 可以看到maven可以使用
mvn -v
$ Apache Maven 3.9.4 (dfbb324ad4a7c8fb0bf182e6d91b0ae20e3d2dd9)
$ Maven home: /root/services/apache-maven-3.9.4
$ Java version: 1.8.0_362, vendor: Private Build, runtime: /usr/lib/jvm/java-8-openjdk-amd64/jre
$ Default locale: en_US, platform encoding: UTF-8
$ OS name: "linux", version: "4.15.0-213-generic", arch: "amd64", family: "unix"
# 安装git
apt install -y git
# 使用git 克隆代码
git clone https://gitee.com/znxs159357/user_center_c.git
# 记得输入gitee用户名和秘密啊
username:
password:
# 用户maven打包 用-DskipTests跳过测试
# mvn package -DskipTests

# 直接把打包好了的jar包上传到项目user_center_c文件夹下

# 给jar包添加可执行权限
chmod a+x user_center_c-0.0.1-SNAPSHOT.jar
# 启动项目
java -jar user-center-c-0.0.1-SNAPSHOT.jar --spring.profiles.active=pord
# 如果项目这样运行就会卡死页面 并且ssh关闭项目也会关闭
# 所以就要让项目后台运行
nohup java -jar user-center-c-0.0.1-SNAPSHOT.jar --spring.profiles.active=pord &
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
root@VM-4-7-ubuntu:~/services/user_center_c# nohup java -jar user-center-c-0.0.1-SNAPSHOT.jar --spring.profiles.active=pord &
[1] 29853
nohup: ignoring input and appending output to 'nohup.out'
root@VM-4-7-ubuntu:~/services/user_center_c# jobs
[1]+ Running nohup java -jar user-center-c-0.0.1-SNAPSHOT.jar --spring.profiles.active=pord &
root@VM-4-7-ubuntu:~/services/user_center_c# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 821/rpcbind
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1469/nginx: worker
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 1223/systemd-resolv
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 14364/sshd
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 12081/sshd: root@pt
tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 32447/sshd: root@pt
tcp6 0 0 :::111 :::* LISTEN 821/rpcbind
tcp6 0 0 :::8080 :::* LISTEN 29853/java
root@VM-4-7-ubuntu:~/services/user_center_c# jps
30225 Jps
29853 jar

到这里,项目的部署就完成了,虽然没有数据库,但是前后端都已经部署完成了

方式二:宝塔部署

宝塔linux

直接在服务器里面安装宝塔

1
2
3
4
外网面板地址:  http://101.35.50.21:8888/tencentcloud
内网面板地址: http://10.0.4.7:8888/tencentcloud
username: norwtlt4
password: d2102b31f9ca

安装完宝塔记得关闭tomcat服务器

方式三:Docker部署

宝塔下载Docker

后端 Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
FROM maven:3.5-jdk-8-alpine as builder

# Copy local code to the container image.
# 指定工作目录
WORKDIR /app
# pom复制到工作目录中
COPY pom.xml .
# 复制源码到工作目录
COPY src ./src

# Build a release artifact.
RUN mvn package -DskipTests

# Run the web service on container startup.
CMD ["java","-jar","/app/target/father-backend-0.0.1-SNAPSHOT.jar","--spring.profiles.active=prod"]

dockerfile 文件名记得别取错了

拉取项目到

/www/wwwroot/目录下

创建docker镜像

1
docker build -t user_center_c:v0.0.1 .
前端Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
FROM nginx

WORKDIR /usr/share/nginx/html/
USER root

COPY ./docker/nginx.conf /etc/nginx/conf.d/default.conf

COPY ./dist /usr/share/nginx/html/

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]

nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
server {
listen 80;

# gzip config
gzip on;
gzip_min_length 1k;
gzip_comp_level 9;
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";

root /usr/share/nginx/html;
include /etc/nginx/mime.types;

location / {
try_files $uri /index.html;
}

}

拉取项目到

/www/wwwroot/目录下

创建docker镜像

1
docker build -t user_center_b:v0.0.1 .
上一篇:
Postman使用教程
下一篇:
git使用实践