技术文章

  • 部署 Let's Encrypt 免费 SSL 证书及自动续期

    2026-02-02 10:30:22 96 0 技术文章

    一、环境说明

    本文使用 Let's Encrypt + acme.sh 为 Docker Nginx 部署免费 SSL 证书,实现证书自动申请、自动续期和自动部署。

    项目配置
    操作系统AlmaLinux 9.7
    Web 服务Nginx
    部署方式Docker Compose
    证书机构Let's Encrypt
    证书管理acme.sh
    验证方式HTTP-01
    证书算法ECC P-256

    本次以以下四个域名为例:

    • cuiliang.com
    • www.cuiliang.com
    • cuiliang.com.cn
    • www.cuiliang.com.cn

    使用一张多域名证书统一管理,最终跳转到 https://www.cuiliang.com。

    二、安装 acme.sh

    考虑到服务器位于中国大陆,优先使用 Gitee 安装,避免 GitHub 访问不稳定导致安装失败。

    1. Gitee 安装(推荐)

    git clone https://gitee.com/neilpang/acme.sh.git
    
    cd acme.sh
    
    ./acme.sh --install -m your-email@example.com
    

    将邮箱替换为自己的邮箱。

    2. GitHub 安装(备用)

    如果服务器可以正常访问 GitHub,也可以使用官方安装方式:

    curl https://get.acme.sh | sh -s email=your-email@example.com
    

    安装完成后,acme.sh 默认安装在:

    /root/.acme.sh/
    

    后续使用:

    ~/.acme.sh/acme.sh
    

    执行相关操作。

    三、配置 Let's Encrypt

    设置 Let's Encrypt 为默认证书颁发机构:

    ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
    

    四、配置 Docker Nginx

    当前 Docker Compose 已经挂载了网站目录和 SSL 证书目录:

    volumes:
      - ${PROJECT_FOLDER}:/var/www/html/:rw
      - ./nginx/ssl:/etc/nginx/ssl
    

    当前 .env 配置:

    PROJECT_FOLDER=../www
    

    Docker Compose 项目目录为 /root/docker,因此对应的宿主机目录为:

    宿主机目录容器目录
    /root/www/var/www/html
    /root/docker/nginx/ssl/etc/nginx/ssl

    直接复用现有目录,不需要增加 Docker Volume。

    1. 修改 Nginx 配置

    将 HTTP 和 HTTPS 分开配置:

    # HTTP:证书验证及 HTTPS 跳转
    server {
        listen 80;
    
        server_name cuiliang.com www.cuiliang.com
                    cuiliang.com.cn www.cuiliang.com.cn;
    
        # Let's Encrypt HTTP-01 验证
        location ^~ /.well-known/acme-challenge/ {
            root /var/www/html;
            default_type text/plain;
            autoindex off;
            try_files $uri =404;
        }
    
        # 其他请求统一跳转 HTTPS
        location / {
            return 301 https://www.cuiliang.com$request_uri;
        }
    }
    
    
    # HTTPS:正式业务服务
    server {
        listen 443 ssl http2;
    
        server_name cuiliang.com www.cuiliang.com
                    cuiliang.com.cn www.cuiliang.com.cn;
    
        # 统一跳转到主域名
        if ($host != www.cuiliang.com) {
            return 301 https://www.cuiliang.com$request_uri;
        }
    
        ssl_certificate /etc/nginx/ssl/www.cuiliang.com.pem;
        ssl_certificate_key /etc/nginx/ssl/www.cuiliang.com.key;
    
        ssl_session_timeout 5m;
        ssl_protocols TLSv1.2 TLSv1.3;
    
        ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
    
        location / {
            proxy_pass http://go:8080;
    
            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 X-Forwarded-Proto $scheme;
        }
    }
    

    注意:首次部署时,如果新证书文件尚未生成,可以暂时保留原有 HTTPS 证书路径,签发成功后再切换。

    2. 创建验证目录

    mkdir -p /root/www/.well-known/acme-challenge
    

    验证目录映射关系:

    宿主机:
    /root/www/.well-known/acme-challenge/
    
    Docker:
    /var/www/html/.well-known/acme-challenge/
    

    3. 检查验证路径

    创建测试文件:

    echo test123 > /root/www/.well-known/acme-challenge/test.txt
    

    访问:

    curl http://www.cuiliang.com/.well-known/acme-challenge/test.txt
    

    正常返回:

    test123
    

    测试完成后删除:

    rm -f /root/www/.well-known/acme-challenge/test.txt
    

    五、申请 Let's Encrypt 证书

    使用 HTTP-01 验证方式申请证书。

    ~/.acme.sh/acme.sh --issue \
      --server letsencrypt \
      --webroot /root/www \
      -d cuiliang.com \
      -d www.cuiliang.com \
      -d cuiliang.com.cn \
      -d www.cuiliang.com.cn \
      --keylength ec-256
    

    参数说明:

    参数说明
    --server letsencrypt使用 Let's Encrypt
    --webroot指定 HTTP 验证目录
    -d指定证书覆盖的域名
    --keylength ec-256使用 ECC P-256 算法

    注意:

    1. 所有域名都需要正确解析到服务器。
    2. 公网 TCP 80 端口必须可以访问。
    3. 验证路径不能被 Nginx 的 301 跳转、鉴权或业务路由拦截。

    六、安装证书

    创建证书目录:

    mkdir -p /root/docker/nginx/ssl
    

    安装证书:

    ~/.acme.sh/acme.sh --install-cert \
      -d cuiliang.com \
      --key-file /root/docker/nginx/ssl/www.cuiliang.com.key \
      --fullchain-file /root/docker/nginx/ssl/www.cuiliang.com.pem \
      --reloadcmd "docker exec nginx nginx -t && docker exec nginx nginx -s reload"
    

    证书文件:

    /root/docker/nginx/ssl/
    ├── www.cuiliang.com.pem
    └── www.cuiliang.com.key
    

    其中:

    • .pem:完整证书链。
    • .key:证书私钥,需要妥善保管。

    Nginx 使用对应的容器内路径:

    ssl_certificate /etc/nginx/ssl/www.cuiliang.com.pem;
    ssl_certificate_key /etc/nginx/ssl/www.cuiliang.com.key;
    

    检查并 Reload:

    docker exec nginx nginx -t &&
    docker exec nginx nginx -s reload
    

    七、自动续期

    acme.sh 安装时会自动创建 Cron 定时任务,不需要手动添加。

    查看定时任务:

    crontab -l
    

    通常会看到类似:

    0 0 * * * "/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" > /dev/null
    

    acme.sh 会定期检查证书有效期,到达续期条件后自动完成:

    1. 申请新证书。
    2. 自动完成 HTTP-01 域名验证。
    3. 更新宿主机证书文件。
    4. 执行 Nginx 配置检查。
    5. 自动 Reload Docker Nginx。

    整个过程不需要手动添加 TXT 记录,也不需要人工更换证书。

    八、常用维护命令

    # 查看证书列表
    ~/.acme.sh/acme.sh --list
    
    # 查看 Cron 定时任务
    crontab -l
    
    # 查看证书有效期
    openssl x509 \
      -in /root/docker/nginx/ssl/www.cuiliang.com.pem \
      -noout -subject -dates
    
    # 检查 Nginx 配置
    docker exec nginx nginx -t
    
    # Reload Nginx
    docker exec nginx nginx -s reload
    

    九、总结

    通过 Let's Encrypt + acme.sh,可以实现 Docker Nginx 的免费 SSL 证书自动化管理。

    主要特点:

    • 免费使用 Let's Encrypt 证书。
    • 国内服务器优先通过 Gitee 安装 acme.sh。
    • 不需要阿里云 AccessKey。
    • 不需要手动添加 DNS TXT 记录。
    • 不需要额外增加 Docker Volume。
    • 支持多个业务域名统一管理。
    • 自动申请、自动续期、自动部署。
    • 续期后自动 Reload Docker Nginx。

    整个方案只需要首次配置一次,后续证书续期和部署均由 acme.sh 自动完成。

    • 使用 po4a 进行技术文档翻译:从入门到实践

      2026-01-23 19:23:47 2269 0 技术文章

      在开源项目中,文档的多语言支持是提升项目影响力的重要环节。最近在为 Yii3 框架文档添加简体中文翻译时,我深入使用了 po4a 这个强大的翻译工具。本文将分享使用 po4a 进行技术文档翻译的完整流程和实践经验。

      什么是 po4a?

      po4a(PO for Anything)是一个基于 gettext 的文档翻译工具,最初为 Debian 项目开发,但现在已经广泛应用于各种文档翻译场景。它的核心优势在于:

      • 格式无关:支持 Markdown、POD、XML、HTML 等多种格式
      • 版本控制友好:翻译文件(.po)是纯文本,易于纳入 Git 管理
      • 增量更新:源文件更新时,自动合并已有翻译,避免重复工作
      • 团队协作:支持多人协作翻译,便于分工

      项目结构

      在开始之前,先了解典型的 po4a 项目结构:

      docs/
      ├── _translations/        # 翻译配置和 PO 文件
      │   ├── po4a.conf         # 主配置文件
      │   ├── pot/              # 模板文件(.pot)
      │   └── po/               # 翻译文件(.po)
      │       └── zh-CN/        # 简体中文翻译
      ├── src/                  # 源文件和翻译输出
      │   ├── en/               # 英文源文件
      │   └── zh-CN/            # 简体中文翻译输出
      

      这种结构将翻译工作与源文件分离,既保持了源文件的整洁,又便于管理翻译进度。

    • Docker 配置代理完整指南

      2026-01-21 17:18:48 2880 0 技术文章

      背景

      在国内环境下使用 Docker 时,经常遇到拉取镜像缓慢或超时的问题。虽然可以配置镜像加速器,但随着国内镜像源的不稳定,使用代理成为了更可靠的解决方案。

      问题现象

      执行 docker build 或 docker pull 时出现以下错误:

      ERROR: failed to solve: DeadlineExceeded: failed to resolve source metadata for docker.io/xxx
      dial tcp xxx.xxx.xxx.xxx:443: i/o timeout
      

      核心原理

      Docker 架构分为两部分:

      • Docker Client: 命令行工具,接收用户命令
      • Docker Daemon: 后台服务,实际执行拉取镜像等操作 当我们在终端设置 export HTTP_PROXY=... 时,只影响 Docker Client,而真正需要网络访问的 Docker Daemon 并未使用代理。

      解决方案

      为 Docker Daemon 配置代理(推荐)

      1. 创建 systemd 配置目录
        sudo mkdir -p /etc/systemd/system/docker.service.d
        
      2. 创建代理配置文件
        sudo vim /etc/systemd/system/docker.service.d/http-proxy.conf
        
      3. 添加代理配置
        [Service]
        Environment="HTTP_PROXY=http://your-proxy-ip:port"
        Environment="HTTPS_PROXY=http://your-proxy-ip:port"
        Environment="NO_PROXY=localhost,127.0.0.1,*.local,registry.internal.com"
        

        配置说明:

      • HTTP_PROXY: HTTP 协议代理地址
      • HTTPS_PROXY: HTTPS 协议代理地址(Docker Hub 使用 HTTPS)
      • NO_PROXY: 不使用代理的地址列表,通常包括:
        • localhost, 127.0.0.1: 本地地址
        • *.local: 本地域名
        • 内网私有镜像仓库域名
      1. 重载配置并重启 Docker
        # 重载 systemd 配置
        sudo systemctl daemon-reload
        # 重启 Docker 服务
        sudo systemctl restart docker
        
      2. 验证配置
        # 查看 Docker 服务的环境变量
        sudo systemctl show --property=Environment docker
        

        预期输出:

        Environment=HTTP_PROXY=http://your-proxy-ip:port HTTPS_PROXY=http://your-proxy-ip:port NO_PROXY=localhost,127.0.0.1
        
      3. 测试代理连接
        # 测试是否能通过代理访问 Docker Hub
        curl -x http://your-proxy-ip:port -I https://registry-1.docker.io/v2/
        # 拉取测试镜像
        docker pull hello-world
        
    • 在 macOS(Apple Silicon)根目录下创建目录的正确方式

      2025-10-29 17:30:05 3308 0 技术文章

      在 macOS 上,我想在根目录(/)下创建一个目录,比如 /data1,用于放置项目数据。

      mkdir /data1
      

      结果系统提示:

      mkdir: /data1: Read-only file system
      

      一开始我以为是权限问题,于是又试了:

      sudo mkdir /data1
      

      但依然报错,说明即使 sudo 也不行。

      为什么会这样?

      从 macOS Big Sur(11) 开始,苹果对系统盘启用了 Signed System Volume(SSV)签名保护机制。

      这意味着:

      系统卷 / 是 只读的 APFS 快照;

      即使是 root 用户,也不能直接修改;

      mount -uw / 在 Apple Silicon(M1/M2/M3)上会报错:

      mount_apfs: volume could not be mounted: Permission denied
      mount: / failed with 66
      

      简单来说:根目录是锁死的。

    • 通过 pyenv 配合 venv 来创建项目专属的虚拟环境

      2024-12-03 17:37:48 6871 0 技术文章

      安装 pyenv

      curl https://pyenv.run | bash
      

      这条命令会自动安装以下工具:

      • pyenv
      • pyenv-doctor(检查 pyenv 安装的健康状况)
      • pyenv-virtualenv(用于创建 Python 虚拟环境)

      更新 Shell 配置

      vim ~/.bashrc
      
      echo -e 'export PATH="$HOME/.pyenv/bin:$PATH"\n' \
          'eval "$(pyenv init --path)"\n' \
          'eval "$(pyenv init -)"\n' \
          'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
      
      source ~/.bashrc
      

      安装 Python 版本

      pyenv install 3.11.9
      

      设置全局或本地 Python 版本

      设置一个全局的 Python 版本(用于所有项目):

      pyenv global 3.11.9
      

      你还可以为特定目录设置本地版本,例如在某个项目目录下:

      pyenv local 3.8.12
      

      这会创建一个 .python-version 文件,记录该项目使用的 Python 版本。

      创建虚拟环境(使用 venv)

      python -m venv venv
      

      激活虚拟环境

      对于 Bash/Zsh(Linux/macOS):

      source venv/bin/activate
      

      对于 Windows(CMD):

      .\venv\Scripts\activate
      
    • 解决 Python 3.8+ 遇到的 OpenSSL 版本不兼容的问题

      2024-11-09 16:40:57 10577 0 技术文章

      在安装 Python3.8 时可能会遇到与系统自带 OpenSSL 版本不兼容的问题。Python3.8 需要的 OpenSSL 版本为 1.0.2 或 1.1.x。如果系统中默认的 OpenSSL 版本低于需求(通常是 OpenSSL 1.0.1),会导致如下错误:

      Could not build the ssl module!
      Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().
      
    • MySQL 容器异常 - mbind: Operation not permitted

      2024-06-03 10:39:11 9432 0 技术文章
      2024-06-03 09:23:25+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.27-1debian10 started.
      2024-06-03 09:23:25+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
      2024-06-03 09:23:25+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.27-1debian10 started.
      2024-06-03T09:23:25.944402Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
      2024-06-03T09:23:25.944530Z 0 [Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead.
      2024-06-03T09:23:25.944549Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.27) starting as process 1
      2024-06-03T09:23:25.970311Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
      2024-06-03T09:23:26.546899Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
      2024-06-03T09:23:26.881580Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
      2024-06-03T09:23:26.881612Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
      2024-06-03T09:23:26.899326Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
      2024-06-03T09:23:26.899366Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
      2024-06-03T09:23:26.989154Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
      2024-06-03T09:23:27.078117Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.27'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
      2024-06-03T09:23:27.078165Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
      mbind: Operation not permitted
      mbind: Operation not permitted
      mbind: Operation not permitted
      
      • macOS 终端使用 Solarized Dark xterm-256color 主题

        2023-11-25 12:38:22 2383 0 技术文章

        1. 下载主题文件

        Solarized 是一款经典的终端配色方案,护眼又高对比度。可以在官方仓库下载 macOS 终端主题:

        git clone https://github.com/altercation/solarized.git
        

        主题路径位于:

        solarized/osx-terminal.app-colors-solarized/
        

        其中包括两个文件:

        • Solarized Dark xterm-256color.terminal
        • Solarized Light xterm-256color.terminal

        2. 导入主题

        1. 打开 Terminal.app
        2. 依次点击:偏好设置 (Preferences) → Profiles
        3. 点击左下角的 齿轮图标 → 导入...
        4. 选择 Solarized Dark xterm-256color.terminal
        5. 设置为默认主题:在主题上右键 → 选择「设为默认」

        完成后重新打开终端,就能看到舒适的 Solarized Dark 效果。

        3. 优化选中文本配色

        1. 打开 终端 → 设置 → 配置文件 → 文本 → 选中颜色
        2. 点击右侧的小色块(打开颜色选取器)
        3. 在弹出的窗口上方点 “滑块图标”(像三根小杠的那个),切换模式
        4. 在上方的下拉菜单中选择:RGB 滑块
        5. 在十六进制颜色编号框输入 284B5A

        4. 设置字体

        在 macOS 终端中,个人比较喜欢 SF Mono Terminal 11,苹果官方风格,简洁干净。 JetBrains Mono 字体也相当不错,专为开发设计,字符区分度高(比如 0/O, 1/l/I),自带连字(可以关闭),粗细分布合理,长时间看不累。

        • VS Code 设置编辑器及终端的字体和字号

          2023-09-16 12:38:22 1040 0 技术文章

          作为一名开发者,我们都希望拥有一个舒适的编码环境。最近在配置 VS Code 的字体时遇到了一些问题,经过一番调试,终于找到了完美的解决方案。今天分享这个过程,希望能帮到有相似困扰的朋友。

          第一次尝试:基础配置

          {
            "editor.fontFamily": "SF Mono, monospace",
            "editor.fontSize": 13,
            "terminal.integrated.fontFamily": "SF Mono, monospace"
            "terminal.integrated.fontSize": 13
          }
          

          结果:字体和字号都不生效。

          第二次尝试:带空格的字体加单引号

          {
            "editor.fontFamily": "'SF Mono', monospace",
            "editor.fontSize": 13,
            "terminal.integrated.fontFamily": "'SF Mono', monospace"
            "terminal.integrated.fontSize": 13
          }
          

          结果:字号生效,字体不生效。

          然而,在配置过程中还遇到了一个令人困扰的错误:

          终端仅支持等宽字体。如果这是新安装的字体,请确保重新启动 VS Code。

          最终打开目录 /System/Applications/Utilities/Terminal.app/Contents/Resources/Fonts/

          双击 SFMono-Regular.otf → “安装字体”

          问题解决!

          以下是完整的 ~/Library/Application\ Support/Code/User/settings.json

          {
              "workbench.colorTheme": "Solarized Dark",
              "roo-cline.allowedCommands": [
                  "git log",
                  "git diff",
                  "git show"
              ],
              "roo-cline.deniedCommands": [],
              "security.workspace.trust.untrustedFiles": "open",
              "editor.fontFamily": "'SF Mono', Menlo, monospace",
              "editor.fontSize": 13,
              "terminal.integrated.fontFamily": "'SF Mono Terminal', Menlo, monospace",
              "terminal.integrated.fontSize": 13
          }
          
          • Undefined index: constraint_name 报错解决方法

            2022-12-09 16:32:12 5136 0 技术文章

            错误日志

            [error][yii\base\ErrorException:8] yii\base\ErrorException: Undefined index: constraint_name in /path/to/vendor/yiisoft/yii2/db/mysql/Schema.php:394
            

            原因

            MySQL 8.0.21 中返回的列名大小写不一致,导致 Yii2 在获取数据库模式时出错。

            解决方法

            1. 编辑 common/config/main-local.php,设置 PDO 属性 PDO::ATTR_CASE => PDO::CASE_LOWER
              'db' => [
               'class' => 'yii\db\Connection',
               'dsn' => 'mysql:host=localhost;dbname=db_name',
               'username' => 'username',
               'password' => '******',
               'attributes' => [PDO::ATTR_CASE => PDO::CASE_LOWER],
              ],
              
            2. 修改 /path/to/vendor/yiisoft/yii2/db/mysql/Schema.php:394
              foreach ($rows as $row) {
               $row = array_change_key_case($row, CASE_LOWER);
              

            推荐第一种方法,使列名明确指定为一致的大小写。

            参考文章:https://github.com/yiisoft/yii2/issues/18171

            • 入职满 7 周年纪念日

              2022-01-20 12:38:22 4833 0 技术文章

              IMG_0354.PNG

              • 入职满 6 周年纪念日

                2021-01-20 12:38:22 4369 0 技术文章

                IMG_8718.PNG

                • Docker 清理无效容器镜像及日志的方法

                  2020-04-12 12:38:22 4120 0 技术文章

                  清除无效容器、网络、镜像、缓存

                  • docker system prune:删除停止的容器、未使用的网络、悬空的镜像和构建缓存。
                  • docker system prune -a:删除停止的容器、未使用的网络、所有未使用的镜像(不仅仅是悬空的镜像)和构建缓存。

                  清除日志

                  1. 创建 clean_docker_log.sh,内容如下:
                    #!/bin/bash
                    

                  logs=$(find /var/lib/docker/containers/ -name *-json.log)

                  for log in $logs

                      do
                              echo "clean logs : $log"
                              cat /dev/null > $log
                      done
                  

                  echo "Docker logs cleanup completed."

                  
                  2. 给脚本增加可执行权限
                  

                  chmod +x clean_docker_log.sh

                  
                  3. 执行脚本
                  

                  ./clean_docker_log.sh

                  
                  
                  • 使用 Docker 基于 Debian 安装 Java 环境失败的解决方法

                    2019-11-15 17:09:34 27503 0 技术文章
                    # 安装 default-jre 需要手动创建 /usr/share/man/man1 目录
                    RUN mkdir -p /usr/share/man/man1 \
                        && apt-get update \
                        && apt-get install -y \
                        default-jre \
                    

                    参考文章:https://stackoverflow.com/questions/55795154/docker-how-to-install-openjdk-jre-12-on-top-of-debian-jessie-slim

                    • 升级 Catalina 后出现 Read-only file system 问题解决方案

                      2019-10-07 18:03:36 28284 0 技术文章

                      由于项目的根目录在 /data1 目录,需要在根目录创建 data1 目录,但是提示:

                      mkdir: data1: Read-only file system
                      

                      先关闭 sip,终端输入 sudo mount -uw /

                      然后再创建目录或者创建软链接,即可成功!

                      • Yii China 是官方唯一认可的中文社区

                        2019-01-30 11:48:58 29174 1 技术文章

                        今天是 Yii Framework 2.0.16 版本发布 的日子,经过我们坚持不懈的进行国际化翻译,以及在中国进行大量的宣传和推广,Yii China 已经成为全球最大的 Yii 中文社区。

                        希望更多的 Yiiers 加入我们社区! https://www.yiichina.com

                        官方提供的社区推荐地址: https://github.com/yiisoft/yii2/wiki/communities#chinese

                      • GitLab 实现仓库完全迁移,包括所有提交记录、分支、标签

                        2018-12-28 18:20:29 27904 0 技术文章

                        具体命令如下:

                        cd <项目目录>
                        git fetch --all
                        git fetch --tags
                        git remote set-url origin <项目的新仓库地址>
                        git push origin --all 
                        git push --tags 
                        

                        参考文章:

                        https://developer.atlassian.com/blog/2016/01/totw-copying-a-full-git-repo/

                      • Mac OSX 目录操作遇到 Operation not permitted 问题

                        2018-12-12 11:41:28 32623 0 技术文章

                        今天在安装 PHP 扩展的时候发现使用 sudo 还依然提示 "Operation not permitted"。

                        这是由于 10.13 版本加强了权限的限制,即 rootless 机制,可以进入恢复模式关闭 rootless 机制。

                        官方说明:https://developer.apple.com/videos/play/wwdc2015/706/

                        解决方案

                        1. 重启 Mac,按 Command + R(Win 键盘:win + R)进入恢复模式

                        2. 打开终端,输入命令:

                          csrutil disable
                          
                        3. 重启后让机器正常启动,可以在终端查看 rootless 状态:

                          csrutil status
                          

                          显示

                          System Integrity Protection status: disabled.
                          

                          正常情况下 rootless 已经关闭。

                        4. 要想重新开启 rootless 机制,参考步骤 1,输入命令:

                          csrutil enable
                          
                      • 使用 pdflatex 生成 Yii 2 中文权威指南 PDF

                        2018-12-02 16:46:45 27548 0 技术文章

                        问题来源

                        https://github.com/yiisoft-contrib/yiiframework.com/issues/142 https://en.wikibooks.org/wiki/LaTeX/Internationalization#Chinese

                        解决方案

                        https://github.com/yiisoft-contrib/yiiframework.com/blob/master/commands/GuideController.php#L85

                        修改为:

                        // https://en.wikibooks.org/wiki/LaTeX/Internationalization#Chinese
                        // TODO this does not work yet. See https://github.com/yiisoft-contrib/yiiframework.com/issues/142
                        file_put_contents("$pdfTarget/main.tex", str_replace(['\usepackage[british]{babel}', '\begin{document}', '\end{document}'], ['\usepackage{CJKutf8}', '\begin{document}' . PHP_EOL . '\begin{CJK}{UTF8}{gbsn}', '\end{CJK}' . PHP_EOL . '\end{document}'], file_get_contents("$pdfTarget/main.tex")));
                        

                        {gbsn} 是“宋体”,所以前提是服务器已经安装中文的“宋体”字体。

                      • Yii 2 将 Gravatar 头像图片做本地化缓存

                        2018-09-15 18:11:52 27204 0 技术文章

                        Gravatar 是全球公认头像,但头像图片有时候访问奇慢无比,将整个页面的加载速度降了下来,如果能将头像图片缓存到本地,并设置过期时间,那性能会飞速提高,以下代码是 Yii2 的自定义 Helper 中的代码片断,用于实现 Gravatar 本地化访问。

                        public function gravatar($email, $s = 80, $atts = [], $d = 'mp', $r = 'g')
                        {
                            $key = md5(strtolower(trim($email)));
                            $img_file = Yii::getAlias("@webroot/uploads/avatar/$key.jpg");
                            $img_url = Yii::getAlias("@web/uploads/avatar/$key.jpg");
                            $noavatar_file = Yii::getAlias('@webroot/images/noavatar.jpg');
                            $noavatar_url = Yii::getAlias('@web/images/noavatar.jpg');
                        
                            // 缓存时间 30 天,超过 30 天会重新到 gravatar.com 去取
                            if (!file_exists($img_file) || filemtime($img_file) < time() - 3600 * 24 * 30) {
                                file_put_contents($img_file, file_get_contents("https://www.gravatar.com/avatar/$key?s=$s&d=$d&r=$r"));
                            }
                        
                            // 没有设置过头像的用户,显示自定义头像
                            if (md5_file($img_file) == md5_file($noavatar_file)) {
                                $img_url = $noavatar_url;
                            }
                        
                            return Html::img($img_url, $atts);
                        }