1、Springboot项目maven配置
<groupId>com.zxl</groupId>
<artifactId>myblog</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
packaging打包方式是以jar包的形式进行打包
2、利用maven把Springboot项目进行打包,可以利用命令,也可以利用鼠标点击操作
如图,在IDEA界面的右上角,有个maven选项,点击它
3、双击package(网上有一些进行clean,compile等操作,我这里没进行那么繁琐的操作,直接双击package)
4、看控制台输出信息
当出现BUILD SUCCESS的字样的时候,表示编译打jar包成功了,Building jar:后面的路径是存放jar包的路径,myblog.jar是编译之后的jar包,至于名字为什么后面一大串的字符,是因为我在maven进行配置了
5、把生成的jar包上传到Linux服务器上(我放到developer文件夹下)
6、可以利用自动化脚本运行,新建myblog.service文件
[Unit]
Description=myblog
After=syslog.target
[Service]
ExecStart= /usr/bin/java -jar/developer/myblog.jar #这里改成你自己的路径和jar包名
[Install]
WantedBy=multi-user.target
7、新建myblog.sh文件
#!/bin/bash
cd `dirname $0`
CUR_SHELL_DIR=`pwd`
CUR_SHELL_NAME=`basename ${BASH_SOURCE}`
#修改这里jar包名即可
JAR_NAME="myblog.jar"
JAR_PATH=$CUR_SHELL_DIR/$JAR_NAME
#JAVA_MEM_OPTS=" -server -Xms1024m -Xmx1024m -XX:PermSize=128m"
JAVA_MEM_OPTS=""
#SPRING_PROFILES_ACTIV="-Dspring.profiles.active=eureka2"
SPRING_PROFILES_ACTIV=""
LOG_DIR=$CUR_SHELL_DIR/logs
LOG_PATH=$LOG_DIR/${JAR_NAME}.log
echo_help()
{
echo -e "syntax: sh $CUR_SHELL_NAME start|stop"
}
if [ -z $1 ];then
echo_help
exit 1
fi
if [ ! -d "$LOG_DIR" ];then
mkdir "$LOG_DIR"
fi
if [ ! -f "$LOG_PATH" ];then
touch "$LOG_DIR"
fi
if [ "$1" == "start" ];then
# check server
PIDS=`ps --no-heading -C java -f --width 1000 | grep $JAR_NAME | awk '{print $2}'`
if [ -n "$PIDS" ]; then
echo -e "ERROR: The $JAR_NAME already started and the PID is ${PIDS}."
exit 1
fi
echo "Starting the $JAR_NAME..."
# start
nohup java $JAVA_MEM_OPTS -jar $SPRING_PROFILES_ACTIV $JAR_PATH >> $LOG_PATH 2>&1 &
COUNT=0
while [ $COUNT -lt 1 ]; do
sleep 1
COUNT=`ps --no-heading -C java -f --width 1000 | grep "$JAR_NAME" | awk '{print $2}' | wc -l`
if [ $COUNT -gt 0 ]; then
break
fi
done
PIDS=`ps --no-heading -C java -f --width 1000 | grep "$JAR_NAME" | awk '{print $2}'`
echo "${JAR_NAME} Started and the PID is ${PIDS}."
echo "You can check the log file in ${LOG_PATH} for details."
elif [ "$1" == "stop" ];then
PIDS=`ps --no-heading -C java -f --width 1000 | grep $JAR_NAME | awk '{print $2}'`
if [ -z "$PIDS" ]; then
echo "ERROR:The $JAR_NAME does not started!"
exit 1
fi
echo -e "Stopping the $JAR_NAME..."
for PID in $PIDS; do
kill $PID > /dev/null 2>&1
done
COUNT=0
while [ $COUNT -lt 1 ]; do
sleep 1
COUNT=1
for PID in $PIDS ; do
PID_EXIST=`ps --no-heading -p $PID`
if [ -n "$PID_EXIST" ]; then
COUNT=0
break
fi
done
done
echo -e "${JAR_NAME} Stopped and the PID is ${PIDS}."
else
echo_help
exit 1
fi
打开浏览器输入自己的网址(没有域名的话,就输入ip)
如果访问不了,那么防火墙端口没开
服务器 iptables 配置,开放 80端口
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save
关闭的命令则为:./myblog.sh stop