分类: 服务器端

  • nginx 笔记

    nginx 笔记

    基础配置

    # daemon on;
    # worker_processes 1;
    error_log logs/travis.error.log error;
    pid logs/travis.nginx.pid;
    
    events {
        accept_mutex off;
    }
    
    http {
    
        server {
            listen 9000;
    
            include mime.types;
    
            location / {
                rewrite ^ /static/edge/index.html last;
            }
    
            location /admin-api/ {
                proxy_pass https://admin-dev.openresty.com.cn;
                proxy_set_header Host admin-dev.openresty.com.cn;
                proxy_ssl_name "admin-dev.openresty.com.cn";
                proxy_ssl_server_name on;
            }
    
            location /static/ {
                alias fe/dist/static/;
            }
        }
    }

    启动 nginx

    nginx -p $PWD -c conf/travis.conf

    其中,-p $PWD 指定当前目录为工作目录。-c 指定配置文件。

    reload

    找到配置中的 pid 文件,从里面找到 pid

    kill -s HUP ${pid}

    域名 A 返回 a 文件,域名 B 返回 b 文件

    如果同一个项目下,我们有两个 robots.txt 文件,希望根据域名输出不同的文件,可以用条件判断 + rewrite

    注意,nginx 不支持 else,只能纯 if

    server {
        location /robots.txt {
            if ($host = mywordle.org) {
                rewrite ^ /robots.mywordle.org.txt break;
            }
            if ($host = mywordgame.com) {
                rewrite ^ /robots.mywordgame.com.txt break;
            }
            try_files $uri =404;
        }
    }
  • 在树莓派上启用 PostgreSQL 对外服务

    在树莓派上启用 PostgreSQL 对外服务

    以前写过一篇笔记《树莓派4 安装 OpenResty + PostgreSQL》,记录如何在树莓派上装 PostgreSQL,不过那时候只是为了在上面做开发,没有考虑过对外服务。如今为了能够在别的机器上做开发,所以要想办法配置一下对外服务。

    0. 系统

    • Raspberry Pi 4B
    • Debian 10 buster 更新到最新
    • 如上篇文章所述安装和配置 PostgrSQL

    1. 判断本地运行状态。

    # 查看服务状态
    sudo service --status-all
    # [ + ]  postgresql
    
    # 查看端口
    sudo netstat -plunt | grep postgres
    # tcp        0      0 127.0.0.1:5432            0.0.0.0:*               LISTEN      6629/postgres

    服务在运行,端口也在侦听,直接连接,失败,被服务器拒绝。

    2. 安装防火墙工具调整规则

    猜测可能跟防火墙有关,iptables 我不熟,所以安装 ufw 帮忙:

    # 安装
    sudo apt install ufw
    
    # 启动端口
    sudo ufw allow 5432
    sudo ufw allow from 10.0.0.10 # 我的 iMac

    3. 修改侦听端口

    修改防火墙后还是连不上。使用 Telnet 工具可以本地连接,但不能远程连接,推断应该是侦听端口的问题。回去仔细看了一下端口状态,觉得应该是端口没配好,所以修改配置,侦听 0.0.0.0,然后重启 PostgreSQL 服务,再连接就成功了。

    listen_addresses = '0.0.0.0' 
    port = 5432
    host    all             all              0.0.0.0/0                       md5
    host    all             all              ::/0                            md5

    参考链接:

  • LeanCloud 笔记

    慢慢记。

    慎用 await Promise.all(items.map(item => ....))

    很容易造成 409 too many requests 问题。

    最好用

    const newItems = [];
    for (const item of items) {
      item = await doSomeAsyncJob();
      newItems.push(item);
    }

    Pointer 时尽量用 query

    取单一对象的时候,方法有很多,比如 createWithoutData + fetch。不过如果如果对象内部属性有 Pointer,且我们希望一次性把 Pointer 取回来的话,最好用 query,因为只有它支持 .include(),可以一次性拉取全部需要的数据,减少请求次数,减少发生 too many requests 的可能。

  • 树莓派4 安装 OpenResty + PostgreSQL

    树莓派4 安装 OpenResty + PostgreSQL

    为了给 OpenResty.org 添加论坛链接,在本地搭建开发环境,选择用树莓派搞。记录一下,以备将来回顾。

    (更多…)
  • 解决 Raspberry Pi 4 安装 php-mbstring/php-curl 的问题

    解决 Raspberry Pi 4 安装 php-mbstring/php-curl 的问题

    最近要在 flarum 上做二次开发,尝试直接用 php -S localhost:8080 未果,于是打算在树莓派上搭个开发环境,省得它整日落灰。

    因为在本地创建过仓库,所以这次直接从 GitHub clone 项目下来,然后打算执行 composer install 安装依赖。结果提示差了 php-mbstring(解决汉字等多字节字符)和 php-curl(用于远程请求)两个模块。然后我就打算用 apt install php-curl 安装模块,没想到失败了,仔细看错误信息,因为这个模块依赖 libcurl3,但是系统里是 libcurl4,所以不行。

    那就安装 libcurl3 呗,结果系统认为明显 libcurl4 更新,不给装 3,哪怕删了重装都不行。

    后来查了半天,找到答案。原来我添加的源是 stretch 的,也就是面向 Debian 9 的;而 Raspberry Pi 的系统是基于 Debian 10,也就是 buster 的,所以依赖处理上,两方面就冲突了。

    这个时候,需要用 sh -c 'echo "deb https://packages.sury.org/php/ buster main" > /etc/apt/sources.list.d/php.list' 把属于“buster”的源添加进系统,接着删掉之前 stretch 的源,然后 apt update 之后,就可以正常安装了。


    参考链接:https://github.com/oerdnj/deb.sury.org/issues/1193

  • 配置 nginx 支持目录别名

    配置 nginx 支持目录别名

    我有一个小项目,我们假定它叫 up,是用 PHP 写的私人图床,部署在服务器上,域名是 up.meathill.com。因为是 PHP,不需要编译,直接 `git clone` 仓库然后配置 nginx 指过去就好。

    后来想做一个在线预览的功能,是个单页应用,也放在这个仓库里,目录是 `up/fe`,项目用 Vue + Webpack 来做,源码放在 `up/fe/src`,生成的文件放在 `up/fe/dist`。

    这样一来,我就需要在原本的 nginx 里配置一个虚拟目录 `/admin`,指向生成的文件。预览的文件路径就是 `/admin/${file}`,所以我还要把未命中的文件重定向到 `index.html

    经过反复摸索,最终的配置如下:

    {
            location / {
                    # 未直接命中的请求都交给根目录里的 index.php 处理
                    try_files $uri $uri/ /index.php$args;
            }
    
            # 默认的 nginx 1.14 + PHP 7.1 + php-fpm 配置
            location ~ .php$ {
                    include snippets/fastcgi-php.conf;
                    fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
            }
    
            # /admin 的访问指向编译后的前端资源
            location /admin {
                    alias /mnt/www/uploader/fe/dist;
            }
    
            # 直接访问的地址解析,正则其实不必要
            location ~ "/admin/(?<page>d{8}-d{6})$" {
                    alias /mnt/www/uploader/fe/dist/index.html;
                    # 这里的定义很重要,默认是 application/octet-stream 会启动下载
                    default_type text/html;
            }
            
            # 把不希望看到的请求屏蔽掉
            location /fe {
                    return 404;
            }
    }
  • crontab 笔记

    crontab 笔记

    在服务器上定时跑程序。重点:

    1. 要执行的程序,比如 php,必须全局可用
    2. 执行的程序,必须处理内部的文件结构,因为执行路径不太确定

    生成日期时间配置,可以使用这个工具

    生成日志

    1 0 * * * php /var/www/laravel/artisan check:daily > /var/log/laravel.log

    每天凌晨 00:01 执行 php,跑一个 laravel command,并且把日志放在 /var/log/laravel.log

    续写日志,而不是覆盖

    使用 >> 替换 >

  • wget 笔记

    wget 笔记

    抓取整站

    wget -r https://target.site/
  • 解决 PHP 7.2.8 + MySQL 8.0.12 连接失败的问题

    解决 PHP 7.2.8 + MySQL 8.0.12 连接失败的问题

    这两天又反复遇到这个问题,先写解决方案:

    1. 使用 caching_sha2_password  插件

    修改用户密码,并且用插件生成,可以解决 WordPress 的问题。

    ALTER USER 'user'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'password';

    2. 修改 my.cnf,使用原生密码

    使用 Laravel + MySQL 8.0 的时候,遇到

    SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client

    修改 /etc/mysql/my.cnf (我是 Ubuntu 16.04),添加下面一行:

    [mysqld]
    default_authentication_plugin= mysql_native_password

    然后重置密码:

    ALTER USER 'user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
    (更多…)
  • Failed to start php-fpm.service: Unit php-fpm.service is masked.

    Failed to start php-fpm.service: Unit php-fpm.service is masked.

    周末手一抖把服务器从 17.10 升级到了 18.04,然后博客就挂掉了。

    根据提示信息,nginx 应该正常工作,问题多半出在 PHP 上。service --status-all 之后,果然 php7.1-fpm 没有启动。然后照常 service php7.1-fpm start,咦,奇怪,报错了:

    Failed to start php7.1-fpm.service: Unit php7.1-fpm.service is masked.
    

    换用 service php-fpm start 也一样,区别就是 service 名字不太一样。然后 Google 之,没找到很靠谱的说法,但是找到一个类似的情况,发生于使用 do-release-upgrade 升级到 16.04 时,php5-fpm 启动不了,报类似的错误,解决方案是升级到 php7。

    如此一来我也试试好了,因为直接 apt install php-fpm 会解析出来 php7.2,所以我尝试 service php7.2-fpm start,果然可以。既然如此,干脆升级到 7.2 好了,反正我也没啥特殊要求。

    于是修改站点配置文件,把 php 接口指向 7.2 的 socket,然后安装几个欠缺的模块,终于又把博客跑起来了。