• 项目部署与服务器环境配置(CentOS版本)


    概要

    本文章主要针对Java项目的服务器环境配置和项目部署进行介绍和说明

    MySQL数据库安装与配置

    待补充

    Redis缓存数据库安装与配置

    安装前的准备工作

    前往Redis版本库根据自己的需求选择对应的版本下载
    在这里插入图片描述

    # 将redis解压到/opt文件夹下,可以使用-C指定到解压的文件夹
    tar -zvxf redis-5.0.2.tar.gz -C /home
    # 进入到redis的解压目录redis-5.0.2
    cd /home/redis-5.0.2
    # 安装gcc,因redis是c语言编写的,所以需要先安装gcc
    yum -y install gcc
    # 可可利用此脚本查看gcc版本
    gcc -v
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    开始安装和配置测试

    # 配置redis编译及安装位置
    ./configure --prefix=/usr/local/redis
    # 执行编译及安装指令:
    make & make install
    
    # 若上面两步无法执行,则按如下解决
    make
    make install PREFIX=/usr/local/redis
    
    # 拷贝redis配置文件:
    cp redis.conf /usr/local/redis/bin/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    测试redis是否安装成功

    # redis的三种启动方式
    # 前台启动,在任何目录下执行
    redis-server`
    # 后台启动,在任何目录下执行
    redis-server &
    # 启动服务时,指定配置文件
    redis-server redis.conf &
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    redis客户端连接redis服务

    # 默认连接127.0.0.1(本机)的6379端口上的redis服务
    redis-cli
    # 连接127.0.0.1(本机)的指定端口上的redis服务
    redis-cli -p 端口号
    # 连接指定ip主机上的指定端口的redis服务
    redis-cli -h ip地址 -p 端口
    
    # 退出客户端:在客户端执行命令:
    exit 
    # 或者 
    quit
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    关闭redis服务

    # 通过kill命令:(暴力关闭,容易丢失数据)
    ps -ef|grep redis	# 获取redis进程ID
    kill -9 redis进程ID
    # 通过redis-cli命令关闭:(正常用这个方式关闭)
    redis-cli shutdown
    
    • 1
    • 2
    • 3
    • 4
    • 5

    配置redis开机启动

    # 创建脚本文件
    vim /etc/init.d/redis
    
    • 1
    • 2

    编辑脚本内容如下

    #!/bin/bash
    #chkconfig: 2345 10 90  
    #description: Start and Stop redis   
    PATH=/usr/local/bin:/sbin:/usr/bin:/bin   
    REDISPORT=6379  
    EXEC=/usr/local/redis/bin/redis-server   #对应你自己的配置地址
    REDIS_CLI=/usr/local/redis/bin/redis-cli   #对应你自己的配置地址
    PIDFILE=/var/run/redis.pid   
    CONF="/usr/local/redis/bin/redis.conf"  #对应你自己的配置地址
    AUTH="1234"  
    case "$1" in   
         start)   
                 if [ -f $PIDFILE ]   
                 then   
                         echo "$PIDFILE exists, process is already running or crashed."  
                 else  
                         echo "Starting Redis server..."  
                         $EXEC $CONF   
                 fi   
                 if [ "$?"="0" ]   
                 then   
                         echo "Redis is running..."  
                 fi   
                 ;;   
         stop)   
                 if [ ! -f $PIDFILE ]   
                 then   
                         echo "$PIDFILE exists, process is not running."  
                 else  
                         PID=$(cat $PIDFILE)   
                         echo "Stopping..."  
                        $REDIS_CLI -p $REDISPORT  SHUTDOWN    
                         sleep 2  
                        while [ -x $PIDFILE ]   
                        do  
                                 echo "Waiting for Redis to shutdown..."  
                                sleep 1  
                         done   
                         echo "Redis stopped"  
                 fi   
                 ;;   
         restart|force-reload)   
                 ${0} stop   
                 ${0} start   
                 ;;   
         *)   
                echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2  
                 exit 1  
    esac
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    修改权限

    chmod 755 /etc/init.d/redis
    
    • 1

    启动redis

    /etc/init.d/redis start
    
    • 1

    设置开机启动

    # 进入目录
    cd /etc/init.d
    # 设置reids自启动
    chkconfig redis on
    # 重启Linux
    reboot
    # 重启后查看redis服务
    chkconfig --list
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    redis启动/停止

    # 启动服务:
    service redis start
    # 停止服务:
    service redis stop
    # 服务状态:
    service redis status
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    nginx代理安装与配置

    安装配置

    1. 安装依赖包

      //一键安装上面四个依赖
      yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
      
      • 1
      • 2
    2. 下载并解压安装包

      //创建一个文件夹
      cd /usr/local
      mkdir nginx
      cd nginx
      //下载tar包
      wget http://nginx.org/download/nginx-1.13.7.tar.gz
      tar -xvf nginx-1.13.7.tar.gz
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
    3. 安装nginx

      //进入nginx目录
      cd /usr/local/nginx
      //进入目录
      cd nginx-1.13.7
      //执行命令 考虑到后续安装ssl证书 添加两个模块
      ./configure --with-http_stub_status_module --with-http_ssl_module
      //执行make命令
      make
      //执行make install命令
      make install
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
    4. 启动nginx服务

      /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
      
      • 1
    5. 配置nginx.conf

      # 打开配置文件
      vi /usr/local/nginx/conf/nginx.conf
      
      • 1
      • 2

      将端口号改成8089(随便挑个端口),因为可能apeache占用80端口,apeache端口尽量不要修改,我们选择修改nginx端口。
      将localhost修改为你服务器的公网ip地址。
      在这里插入图片描述

    6. 重启nginx

      /usr/local/nginx/sbin/nginx -s reload
      
      • 1

      查看nginx进程是否启动:

      ps -ef | grep nginx

    7. 若想使用外部主机访问nginx,需要关闭服务器防火墙或开放nginx服务端口,端口为上一步nginx.conf的配置端口:

      centOS6及以前版本使用命令: systemctl stop iptables.service
      centOS7关闭防火墙命令: systemctl stop firewalld.service

      关闭防火墙会导致服务器有一定风险,所以建议是单独开放服务端口
      开放80端口: firewall-cmd --zone=public --add-port=80/tcp --permanent
      查询端口号80 是否开启: firewall-cmd --query-port=80/tcp
      重启防火墙: firewall-cmd --reload
      随后访问该ip:端口 即可看到nginx界面。

    8. 访问服务器ip查看

    9. 安装完成一般常用命令
      进入安装目录中,
      命令: cd /usr/local/nginx/sbin
      启动,关闭,重启,命令:
      ./nginx 启动
      ./nginx -s stop 关闭
      ./nginx -s reload 重启

      Nginx的其他配置

      一、配置SSL以及无www域名自动跳转到带www域名

      打开nginx配置文件

      vi /usr/local/nginx/conf/nginx.conf
      
      • 1

      按如下示例修改

      http: {
      	# 排至SSL访问
        	server {
      	    listen                		443;
      	    server_name            		www.domain.com;
      	    ssl_certificate           	/usr/local/nginx/conf/1_www.domain.com.crt;
      	    ssl_certificate_key    		/usr/local/nginx/conf/2_www.domain.com.key;
      	    ssl on;
      	    access_log               	/data/wwwlogs/access_nginx.log combined;
      	    root                  		/data/wwwroot/default/;
      	    index               		index.html index.htm index.jsp;
         }
         # 配置域名跳转,注意,下面的域名跳转一定要在正常ssl的server之后
         server {
      	    listen             			80;
      	    server_name     			domain.com,www.domain.com;
      	    return              		301  https://www.domain.com$request_uri;
         }
         server {
      	    listen              		443;
      	    server_name       			domain.com;
      	    return               		301  https://www.domain.com$request_uri;
        }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24

      重启nginx

      /usr/local/nginx/sbin/nginx -s reload
      
      • 1

      二、解决跨域问题

      打开nginx配置文件

      vi /usr/local/nginx/conf/nginx.conf
      
      • 1

      按如下示例修改

       location / {
      	 proxy_set_header		Host $host;
      	 root					/usr/local/soft/www/myProjectName;
      	 index					index.html index.htm;
      	
      	 if (!-e $request_filename) {
      	     rewrite ^(.*)$ /index.html?s=$1 last;
      	 }
      	 # 这个很重要,访问跨域资源之前浏览器会先连接至服务器进行校验
      	 if ($request_method = 'OPTIONS') {
      	     return 200;
      	 }
      	 # 允许 所有头部 所有域 所有方法
      	 add_header				'Access-Control-Allow-Origin' '*';
      	 add_header				'Access-Control-Allow-Headers' 'X-Requested-With,Content-Type,language';
      	 add_header				'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
      
      	 # 配置vue依赖缓存
      	 gzip					on;
      	 gzip_min_length		1k;
      	 gzip_comp_level		9;
      	 gzip_types				text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
      	 gzip_vary				on;
      	 gzip_disable			"MSIE [1-6]\.";
      }
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26

    ###三、配置nginx开机启动

    vi nginx.service
    
    • 1

    根据nginx安装路径配置启动参数

    [Unit]
    Description=nginx - high performance web server 
    Documentation=http://nginx.org/en/docs/
    After=network. target remote-fs.target nss -lookup. target
    
    [Service] 
    Type=forking
    PIDFile=/usr/local/nginx/logs/nginx.pid
    ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
    ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    ExecReload=/bin/kill -s HUP $MAINPID 
    ExecStop=/bin/kill -s QUIT $MAINPID
    PrivateTmp=true
    
    [Install] 
    WantedBy=multi-user.target
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    将启动配置文件复制到system路径下

    cp ./nginx.service /usr/lib/systemd/system/
    
    • 1

    重启配置服务

    systemctl daemon-reload
    
    • 1

    测试nginx服务配置

    # 查看nginx状态
    systemctl status nginx # 或service nginx status
    # 启动nginx服务
    systemctl start nginx # 或service nginx start
    # 停止nginx服务
    systemctl stop nginx # 或 service nginx stop
    # 或者上面停止启动合并为重启
    systemctl restart nginx # 或 service nginx restart
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    Tomcat安装与配置(若Spring Boot项目jar包部署可忽略)

    待补充

    其他补充问题

    使用七牛云进行CDN加速

    参考cdn实战-七牛云

  • 相关阅读:
    rust编程-rust所有权理解(chapter 4.1)
    springboot服务端接口公网远程调试 - 实现HTTP服务监听【端口映射】
    曼哈顿距离与切比雪夫距离的相互转化
    一个vim函数坑你安逸
    AiTrust下预训练和小样本学习在中文医疗信息处理挑战榜CBLUE表现
    关于并发和并行,Go和Erlang之父都弄错了?
    OracleRAC 安装配置过程中的问题
    微调baichuan2-7b遇到的显存坑
    二叉树【Java】
    10分钟了解7个Java11的新功能
  • 原文地址:https://blog.csdn.net/g610567970/article/details/127707512