初果实验室

  • redis-cli 连接 redis-server 被拒绝

    今天在使用 redis-cli   登录 redis-server 时出现下面的错误,

    Could not connect to Redis at 127.0.0.1:6379: Connection refused

    经查询原因是:

    配置文件 /etc/redis/redis.conf 中保护模式是开启的,

    protected-mode yes
    
    bind 127.0.0.1 ::1

    解决办法:

    protected-mode no
    
    bind 0.0.0.0

    然后重新启动服务

    service redis-server restart

    再使用 redis-cli 就可以正常连接了。

  • 如何安全地存储用户密码

    https://mp.weixin.qq.com/s?__biz=MzIwMTMzNDY3NQ==&mid=2247483848&idx=1&sn=ca475807807837c6ff58ffe7034e6054&chksm=96eecb34a1994222fff02013e508e41a2f10a0850ce71c56c90624fbe19741c35bc3fd48dde4&token=1440046956&lang=zh_CN#rd

  • go解析yaml文件配置

    gopkg.in/yaml.v2 是 go 语言解析 yaml 格式配置文件的开源库,

    源码地址如下:

    https://github.com/go-yaml/yaml

    使用安装步骤如下:

    在命令行执行

    go get gopkg.in/yaml.v2

    示例代码:

    conf.yaml 文件

    rabbitmq:
        host: localhost
        port: 5672
        username: admin
        password: password

    需要注意 yaml 文件里面的字符都是小写

    go 代码示例:

    package main
    
    import (
            "fmt"
            "io/ioutil"
            "log"
    
            yaml "gopkg.in/yaml.v2"
    )
    
    type Conf struct {
            RabbitMQ struct {
                    Host     string `yaml:"host"`
                    Port     int64  `yaml:"port"`
                    Username string `yaml:"username"`
                    Password string `yaml:"password"`
            }
    }
    
    func readConf(filename string) (*Conf, error) {
            buf, err := ioutil.ReadFile(filename)
            if err != nil {
                    return nil, err
            }
    
            var conf Conf
            err = yaml.Unmarshal(buf, &conf)
            if err != nil {
                    return nil, fmt.Errorf("in file %q: %v", filename, err)
            }
    
            return &conf, nil
    }
    
    func main() {
            conf, err := readConf("conf.yaml")
            if err != nil {
                    log.Fatal(err)
            }
            fmt.Print("%v", conf)
    }

     

  • CentOS Nginx Server SSL配置错误一例

    今日配置 CentOS Nginx Server SSL 时,遇到 nginx error log 报告下述错误消息:

    no “ssl_certificate” is defined in server listening on SSL port while SSL handshaking

    经过检查,原来是 default_server 缺失导致;

    listen 443 ssl http2 default_server;

    如果 nginx 有多个 server ,至少有一个server里面需要填写default_server。

     

  • 使用 imagemagick 压缩图片

    以Ubuntu为例,使用 imagemagick 压缩图片,

    首先安装 imagemagick

    apt install imagemagick

    若未找到,可以使用 apt update 更新后再安装,

    找出大于 5M 的 jpg 或者 png图片,使用 convert 命令结合 resize 参数进行压缩

    for f in $(find . -size +5M | grep -E "jpg|png$");do convert $f -resize 25% $f;done;
    
    for f in $(find . -size +4M | grep -E "jpg|png$");do convert $f -resize 30% $f;done;
    
    for f in $(find . -size +3M | grep -E "jpg|png$");do convert $f -resize 40% $f;done;
    
    for f in $(find . -size +2M | grep -E "jpg|png$");do convert $f -resize 45% $f;done;
    
    for f in $(find . -size +1M | grep -E "jpg|png$");do convert $f -resize 50% $f;done;
  • php 扩展 xml

    如果php未安装扩展xml,可能会出现如下错误。

    Composer detected issues in your platform: Your Composer dependencies require the following PHP extensions to be installed: xml

    解决办法

    apt install php7.4-xml