Linux部署前后端项目

部署SpringBoot项目

创建SpringBoot项目

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

命令行启动

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

mkdir project   -- 创建目录命令

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

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

touch nohup.out  -- 创建文件

2.4、运行jar 文件

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文件

tail -f nohup.out

脚本文件启动

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

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

3.2、编辑 server.sh 内容

#!/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、操作命令

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

可能出现的问题

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

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

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

[root@Captian blog]# chmod 777 ./*.sh

部署Vue项目

初始工作

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

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

我们放到home文件中

/home/www/dist

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

cd /home
mkdir www
[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文件夹:

npm run build

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

npm run build:prod

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

2、上传到服务器

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

3、修改nginx配置

进入到nginx文件中

[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的路径。

#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

systemctl restart nginx

配置防火墙

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

添加端口

firewall-cmd --zone=public --add-port=9090/tcp --permanent

然后重启防火墙

systemctl restart firewalld.service

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

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

​ ——鲁迅

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码上言

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值