一、基于Nginx部署前端项目
-
通过webpack打包前端项目,打包后的多个文件存储在dist文件夹中并压缩成tar压缩包。
-
将dist.tar文件上传到CentOS中,并通过如下指令解压到/usr/local/nginx/html目录下:
tar -xvf dist.tar -C /usr/local/nginx/html
-
修改Nginx配置文件nginx.conf
将nginx.conf配置文件中,原有的监听80, 82, 8080端口号的虚拟主机注释掉,引入如下的配置信息:
server {
listen 80;
server_name localhost;
location / {
root html/dist;
index index.html;
}
location ^~ /api/ {
rewrite ^/api/(.*)$ /$1 break;
proxy_pass http://192.168.138.101:8080; #安装Nginx主机的ip
}
location = /50x.html {
root html;
}
}
4.开启Nginx服务,根据ip访问页面
nginx
二、基于Shell脚本自动部署后端项目
1.在Linux中安装JDK、maven、Git
2.克隆远程仓库中的代码到本地仓库
mkdir -p /usr/local/javaapp
cd /usr/local/javaapp #切换目录
git clone https://gitee.com/alex/hello.git #克隆远程仓库代码
- 编写Shell脚本(拉取代码、编译、打包、启动),并命名为bootStart.sh
#!/bin/sh
echo =================================
echo 自动化部署脚本启动
echo =================================
echo 停止原来运行中的工程
APP_NAME=hello #项目名称
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'Stop Process...'
kill -15 $tpid
fi
sleep 2
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'Kill Process!'
kill -9 $tpid
else
echo 'Stop Success!'
fi
echo 准备从Git仓库拉取最新代码
cd /usr/local/javaapp/hello #本地仓库代码路径
echo 开始从Git仓库拉取最新代码
git pull
echo 代码拉取完成
echo 开始打包
output=`mvn clean package -Dmaven.test.skip=true`
cd target
echo 启动项目
nohup java -jar hello-1.0-SNAPSHOT.jar &> reggie-order.log &
echo 项目启动完成
- 为用户授予执行Shell脚本的权限
chmod 777 bootStart.sh
- 执行Shell脚本
sh bootStart.sh或
./bootStart.sh