Ubuntu服务器部署SpringBoot+Vue项目,数据库,jdk,redis,nginx等

使用的是xftp和xshell工具,服务器版本是ubuntu20.04,部署项目为springboot+vue分离项目。打包后的文件放到自定义位置。

常用

  • 文件夹或文件授权

    • sudo chmod 600 ××× (只有所有者有读和写的权限)

    • sudo chmod 644 ××× (所有者有读和写的权限,组用户只有读的权限)

    • sudo chmod 700 ××× (只有所有者有读和写以及执行的权限)

    • sudo chmod 666 ××× (每个人都有读和写的权限)

    • sudo chmod 777 ××× (每个人都有读和写以及执行的权限)

  • 管理员登入

    sudo su
    
  • 切换用户

    su 用户名
    
  • 创建文件夹

    mkdir 文件名
    
  • 重启

    reboot
    

jdk部署

在官网下载相对应的包并下载。
jdk下载地址
在这里插入图片描述

在/usr文件夹下创建java文件夹,将jdk的包导入进去并解压,可以用xftp进行可视化操作,也可以用xshell命令行操作
xshell操作

  • 安装工具

    yum install lrzsz
    
  • 切换到/usr/java文件夹下 ,上传文件

    rz
    
  • 解压文件(可先用ls查看文件夹下文件,并复制压缩包名)

    tar -zxvf 压缩包名
    
  • 找到文件 /etc/profile ,在最下面添加如下代码(注意好文件名)

    export JAVA_HOME=/usr/java/jdk1.8.0_221
    export CLASSPATH=$JAVA_HOME/lib/
    export PATH=$PATH:$JAVA_HOME/bin
    export PATH JAVA_HOME CLASSPATH
    
  • 查看文件是否保存成功

    cat profile
    
  • 使用source命令(source命令:依次执行文件所有语句)

    source /etc/profile
    
  • java命令验证

    java -version
    

安装数据库

  • 更新源

    sudo apt-get update
    
  • 安装mysql(Ubuntu在20.04版本中,源仓库中MySQL的默认版本已经更新到8.0。因此可以直接安装)

    sudo apt-get install mysql-server
    

    启动mysql-services服务

  • systemctl status mysql
    
  • 登入mysql(测试是否安装成功)

    • 以root用户登陆
    sudo mysql -u root -p 
    

    或者:#可以不需要指定用户名密码

    sudo mysql 
    
  • 获取mysql随机账号用户名和初始密码(userpassword)(此方法可用于重置密码)

    sudo cat /etc/mysql/debian.cnf
    
  • 然后使用获取到的用户名和密码登录MySQL

    mysql -u<user> -p<password>
    
  • 修改root密码

    use mysql; 
    
    • 将字段置为空
    update user set authentication_string='' where user='root'; 
    
    • 修改密码

      alter user 'root'@'localhost' identified with mysql_native_password by '修改的密码'; 
      
  • 保存退出(可重新登入检查密码是否修改成功)

    quit;
    
  • 修改编码方式为utf-8

    • 编辑配置文件

      sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
      
    • 在[mysqld]下添加

      sql_mode='STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION'
      collation-server = utf8_unicode_ci
      init-connect='SET NAMES utf8'
      character-set-server = utf8
      
      

    !!如果不能修改文件,则要修改权限

sudo chmod 777 mysqld.cnf
sudo chmod a+w mysqld.cnf

修改文件后

 sudo chmod 644 mysqld.cnf
  • 配置远程连接

    #bind-address=127.0,0.1
    #mysalx-bind-address=127,0,0,1
    #注释掉这两行
    
  • 重启mysql服务

    service mysql restart
    
  • 登入mysql检查mysql目前编码方式

    show variables like "char%";
    
  • 修改用户权限

    use mysql;
    
    update user set host = '%' where user = 'root';
    
  • 刷新权限

    flush privileges;
    
  • 查看用户的权限

    select user, host, plugin from user where user = 'root';
    

    此时root的host为%,可以尝试用远程。

    mysql相关命令

  • 查看MySQL配置文件

    sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
    
  • 启动MySQL数据库服务

    sudo systemctl start mysql.service
    
  • 查看MySQL运行状态

    sudo systemctl status mysql.service
    
  • 停止MySQL数据库服务

    sudo systemctl stop mysql.service
    
  • 重启MySQL数据库服务

    sudo systemctl restart mysql.service
    
  • 确认mysql服务是否启动成功

    sudo netstat -tap | grep mysql
    
  • 查看数据库

    show databases;
    
  • 如何新增用户

    CREATE USER 'admin'@'localhost' IDENTIFIED BY '你要设置的密码'; 
    
  • 卸载mysql

    sudo rm /var/lib/mysql/ -R
    
    sudo apt-get autoremove mysql* --purge
    
    sudo rm /etc/mysql/ -R
    

部署Redis

  • 更新软件包
    sudo apt-get update
    
  • 确认安装并使用空间
    sudo apt-get install redis-server
    
  • 执行完成后,查看redis服务的状态
    service redis status 
    

配置redis

执行命令whereis redis 可以查看配置文件所在位置,一般在/etc/redis/redis.conf

  • 要远程连接redis,需要注释掉redis配置中的 # bind 127.0.0.1
  • 修改 protected-mode yes 改为:protected-mode no
  • 设置密码,配置文件中添加 requirepas 密码
  • 端口号:默认端口是6379 ,可以通过port修改
  • 重启redis
    service redis restart
    

设置jar包不间断运行

打jar包前,配置好ip地址,以免照成没必要的bug,我们把它们放在自己想放的位置之后,在此路径下开始命令行操作

  • 创建并编写文件
    vi start.sh
    

在里面写上java -jar jar包名

  • 赋予管理员权限
    chmod 777 start.sh
    
  • 不间断运行文件内的指令
    nohup ./start.sh &
    

部署nginx

  • 更新源
    sudo apt-get update
    
  • 安装nginx
    sudo apt-get install nginx
    
    • 检查nginx是否安装,输入如下命令后若出现版本号则安装成功
    nginx -v
    

配置nginx

安装nginx后,nginx的默认目录是/etc/nginx,在该目录中有nginx.conf文件进行配置

user  root;
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       80;
        #名称,可自定义,一般写ip地址
        server_name  ip地址;
    
        #charset koi8-r;
    
        #access_log  logs/host.access.log  main;
    
       try_files $uri $uri/ /index.html;
    
    location / {
    
    
        #vue包存放位置
            root   /home/unodolar/yungongju/dist;            
    		# 此处的  实际上是引用下面的转发,否则在 Vue 路由刷新时可能会抛出 404
    		try_files $uri $uri/ ;
    		
    		index  index.html index.htm;
        }	  


# 由于路由的资源不一定是真实的路径,无法找到具体文件
		# 所以需要将请求重写到 index.html 中,然后交给真正的 Vue 路由处理请求资源
		location  {
    
    
		  rewrite ^.*$ /index.html last;
		}
		
		# 关键步骤,这里表示将所有的 /api/ 开头的请求都转发到下面 proxy_pass 指定的链接中
		# 为了防止在访问页面时请求就被 Nginx 代理转发,这里需要更具体的配置,才能和前端访问请求区分开
		location /api/ {
    
    
	      # 后端的真实接口
	      proxy_pass http://IP地址:9090;
	      proxy_redirect off;
	      proxy_set_header Host $host;
	      proxy_set_header X-Real-IP $remote_addr;
	      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	      proxy_set_header   Cookie $http_cookie;
	      # for Ajax
	      #fastcgi_param HTTP_X_REQUESTED_WITH $http_x_requested_with;
	      proxy_set_header HTTP-X-REQUESTED-WITH $http_x_requested_with;
	      proxy_set_header HTTP_X_REQUESTED_WITH $http_x_requested_with;
	      proxy_set_header x-requested-with $http_x_requested_with;
	      client_max_body_size 10m;
	      client_body_buffer_size 128k;
	      proxy_connect_timeout 90;
	      proxy_send_timeout 90;
	      proxy_read_timeout 90;
	      proxy_buffer_size 128k;
	      proxy_buffers 32 32k;
	      proxy_busy_buffers_size 128k;
	      proxy_temp_file_write_size 128k;
	}


        #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

    server nginx restart
    
  • 重启nginx

    nginx -s reload
    
  • 停止nginx

    systemctl stop nginx
    

小白路漫漫,让我们一起加油!!!

猜你喜欢

转载自blog.csdn.net/weixin_52473844/article/details/130499600