Linux部署前后端项目

部署SpringBoot项目

创建SpringBoot项目

先确保有一个可以运行的springboot项目,这里就记录创建项目的流程了,可以自行百度。

命令行启动

2.1、在linux中,我是在data目录下新创建的一个project目录(此目录创建位置不限制,根据自己的来定)

bash 复制代码
mkdir project   -- 创建目录命令

2.2、进入project目录下,将springBoot项目的jar包上传进来

2.3、创建 nohup.out 日志文件,用于输出项目启动的日志输出

bash 复制代码
touch nohup.out  -- 创建文件

2.4、运行jar 文件

bash 复制代码
nohup java -jar xxx.jar                                   -- 运行jar命令
nohup java -jar xxx.jar --server.port=8080                -- 指定端口号启动
nohup java -jar xxx.jar --spring.profiles.active=prod     -- 指定配置文件启动

2.5、查看nohup.out文件

bash 复制代码
tail -f nohup.out

脚本文件启动

3.1、创建 server.sh 脚本文件,并赋予所有者执行权限

bash 复制代码
touch server.sh     -- 创建脚本文件
chmod u+x server.sh -- 赋予所有者执行权限

3.2、编辑 server.sh 内容

bash 复制代码
#!/bin/bash
#这里可替换为你自己的执行程序,其他代码无需更改
APP_NAME=nav-0.0.1-SNAPSHOT.jar
DEPLOY_DIR=`pwd`
#使用说明,用来提示输入参 数
usage() {
 echo "Usage: sh 脚本名.sh [start|stop|restart|status]"
 exit 1
}
 
#检查程序是否在运行
is_exist(){
 pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
 #如果不存在返回1,存在返回0 
 if [ -z "${pid}" ]; then
 return 1
 else
 return 0
 fi
}
 
#启动方法
start(){
 is_exist
 if [ $? -eq "0" ]; then
 echo "${APP_NAME} is already running. pid=${pid} ."
 else
 nohup java -jar $DEPLOY_DIR/$APP_NAME --spring.profiles.active=prod > $DEPLOY_DIR/nohup.out 2>&1 &
 echo "${APP_NAME} start success"
 fi
}
 
#停止方法
stop(){
 is_exist
 if [ $? -eq "0" ]; then
 kill -9 $pid
 else
 echo "${APP_NAME} is not running"
 fi 
}
 
#输出运行状态
status(){
 is_exist
 if [ $? -eq "0" ]; then
 echo "${APP_NAME} is running. Pid is ${pid}"
 else
 echo "${APP_NAME} is NOT running."
 fi
}
 
#重启
restart(){
 stop
 start
}
 
#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
 "start")
 start
 ;;
 "stop")
 stop
 ;;
 "status")
 status
 ;;
 "restart")
 restart
 ;;
 *)
 usage
 ;;
esac

3.3、操作命令

bash 复制代码
./server.sh start     -- 启动项目
./server.sh stop      -- 停止项目
./server.sh status    -- 查看项目运行状态
./server.sh restart   -- 重启项目

可能出现的问题

如果使用脚本启动报以下错误:是因为对*.sh文件的读、写、运行权限不足;

bash 复制代码
[root@Captian blog]# ./server.sh start
-bash: ./server.sh: Permission denied

则可以使用以下命令赋权限。

bash 复制代码
[root@Captian blog]# chmod 777 ./*.sh

部署Vue项目

初始工作

1、先确保vue项目可以在本地正常的运行

2、在linux中新建网站文件夹home

我们放到home文件中

bash 复制代码
/home/www/dist

此时我们需要在服务器上新建www文件夹:

bash 复制代码
cd /home
mkdir www
bash 复制代码
[root@Captian /]# cd home
[root@Captian home]# ll
total 0
[root@Captian home]# mkdir www
[root@Captian home]# ll
total 4
drwxr-xr-x 2 root root 4096 Aug 13 14:04 www
[root@Captian home]# 

我们没有新建dist文件夹,因为我们待会儿vue项目打包就会生成dist文件夹。

打包部署vue项目

1、打包网站

使用vue打包命令,生成dist文件夹:

bash 复制代码
npm run build

如果这个报错的话使用下面的

bash 复制代码
npm run build:prod

你可以在你的项目中看到package.json文件中有设置。

2、上传到服务器

使用ftp工具将dist文件夹上传至/home/www目录下

3、修改nginx配置

进入到nginx文件中

bash 复制代码
[root@Captian nginx]# cd conf
[root@Captian conf]# ll
total 68
-rw-r--r-- 1 root root 1077 Aug 12 17:23 fastcgi.conf
-rw-r--r-- 1 root root 1077 Aug 12 17:23 fastcgi.conf.default
-rw-r--r-- 1 root root 1007 Aug 12 17:23 fastcgi_params
-rw-r--r-- 1 root root 1007 Aug 12 17:23 fastcgi_params.default
-rw-r--r-- 1 root root 2837 Aug 12 17:23 koi-utf
-rw-r--r-- 1 root root 2223 Aug 12 17:23 koi-win
-rw-r--r-- 1 root root 5349 Aug 12 17:23 mime.types
-rw-r--r-- 1 root root 5349 Aug 12 17:23 mime.types.default
-rw-r--r-- 1 root root 2656 Aug 12 17:23 nginx.conf
-rw-r--r-- 1 root root 2656 Aug 12 17:23 nginx.conf.default
-rw-r--r-- 1 root root  636 Aug 12 17:23 scgi_params
-rw-r--r-- 1 root root  636 Aug 12 17:23 scgi_params.default
-rw-r--r-- 1 root root  664 Aug 12 17:23 uwsgi_params
-rw-r--r-- 1 root root  664 Aug 12 17:23 uwsgi_params.default
-rw-r--r-- 1 root root 3610 Aug 12 17:23 win-utf
[root@Captian conf]# vim nginx.conf

我们打开nginx.conf文件,修改以下配置

1、修改端口,为了防止和其他的端口有冲突,我们将80端口改为9090端口(自定义)。

2、修改root里dist的路径。

bash 复制代码
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       9090;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /home/www/dist;   # 路径改成自己的dist路径
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

改完这些之后,我们重启一下nginx

bash 复制代码
systemctl restart nginx

配置防火墙

由于我们把端口改了,所以现在需要我们把加入的端口加入到防火墙中。

添加端口

bash 复制代码
firewall-cmd --zone=public --add-port=9090/tcp --permanent

然后重启防火墙

bash 复制代码
systemctl restart firewalld.service

我姑且举灰黑的手装作喝干一杯酒

我将在不知道时候的时候独自远行。

​ ------鲁迅

相关推荐
A小辣椒14 小时前
TShark:Wireshark CLI 功能
linux
A小辣椒18 小时前
TShark:基础知识
linux
AlfredZhao20 小时前
OCI 明明分配了 200G 系统盘,为什么 df 只看到 30G?
linux·oci
AlfredZhao1 天前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
用户9718356334662 天前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
猪脚踏浪2 天前
linux 拷贝文件或目录到指定的位置
linux
大树882 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠2 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质2 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
bush42 天前
嵌入式linux学习记录十四、术语
linux·嵌入式