分类: 后端

  • 如何修改 npm-debug.log 路径,无解

    目前没有找到修改 npm-debug.log 路径的方法,除非修改源码。

    npm 版本是 3.3.12

    这是 2014 年 stackoverflow 上的提问 Change npm-debug.log location,以及在 github 上讨论的 issue
    Put npm-debug.log file in the cache folder, not cwd #6744
    npm-debug.log could be written to /tmp or similar #1548
    以及假想解决方案
    Make npm-debug.log path configurable. #5252
    将 log 文件写入 app 目录的风险显而易见,尤其正式环境一般都不会开放 app 目录写权限。

    在源文件 npm/lib/util/error-handler.js 中 writeLogFile() 内部已经 hardcode writeStream(‘npm-debug.log’),当初是如何构思的?

    紧接着想到是否可以禁用 node-debug.log, 不过这并不是一个很好的解决方案.

     

    npm 命令行参考 官方 Shorthands and Other CLI Niceties

     

  • npm –prefix 指定目录

    最新上线一个 nodejs 项目,使用 pm2 做进程管理器。

    因为 pm2 更适合用作正式环境进程管理,不像开发环境使用 nodemon 实时监听文件改动重启 node 进程,所以每次发布之后需要执行 pm2 reload all 零延时载入代码。

    pm2 reload all 被配置到 package.json 的 scripts,执行命令 npm run pm2_reload 即可载入代码。

    现在的问题是 node 项目部署在 /www/ran-api,每次需要执行 cd /www/ran-api 进入该目录,才能执行 npm 命令,否则就会提示找不到 package.json 文件。

    后来终于在 npm 官方文档 https://docs.npmjs.com/misc/config 找到 –prefix 配置,也可以直接查看 npm 自带的 Markdown 教程,一般位于 /nodejs 安装目录/node_modules/npm/doc/misc/npm-config.md

    执行下述命令就不用再进入指定目录了:

    /nodejs/npm –prefix=/www/ran-api run pm2_reload

    其他命令也可以设置 –prefix,比如 npm config list

    [~]$ /nodejs/bin/npm config list –prefix=/www/ran-api
    ; cli configs
    ; 这是增加的命令行参数 prefix,会被设置到 npm.localPrefix
    prefix = “/www/ran-api”
    user-agent = “npm/3.3.12 node/v5.5.0 linux x64”

    ; node bin location = /nodejs/bin/node
    ; cwd = /home/ran
    ; HOME = /home/ran
    ; “npm config ls -l” to show all defaults.

  • Linux 安装 memcache

    下载 memcache

    wget http://memcached.org/latest

    如果没有安装 wget 命令,可以先通过 yum 命令安装 wget 命令

    yum install wget

    解压并安装

    tar -zxvf latest
    cd memcached-1.x.x
    ./configure && make && make test && sudo make install
    

    memcache 依赖 libevent,安装中如果出现

    for libevent directory… configure: error: libevent is required.

    系统会默认安装 libevent,memcache 找不到是因为没有找到相应头文件

    可以通过 yum 安装 libevent-devel,安装 libevent 相关开发使用的头文件

    yum install libevent-devel

    再次执行

    ./configure && make && make test && sudo make install

    此时可能出现错误

    prove ./t
    make: prove: Command not found
    make: *** [test] Error 127

    这是执行 make test 命令抛出的错误,可以忽略,重新执行

    sudo make install

    安装完成

    当以 root 身份安装,可以不使用 sudo 获取权限

  • PHP self 和 static 区别

    PHP self 指向定义的 class。

    PHP static 指向运行的 class,一般只有子类覆盖父类的 static 成员或者方法时,在父类中使用 static 会访问到子类。

    class ParentClass
    {
        public static function hello()
        {
            echo "ParentClass: hello\n";
        }
    
        public static function run()
        {
            self::hello();
            static::hello();
        }
    }
    
    class ChildClass extends ParentClass
    {
        public static function hello()
        {
            echo "ChildClass: hello\n";
        }
    }
    
    ParentClass::run();
    
    // 输出
    "ParentClass: hello"
    "ParentClass: hello"
    
    ChildClass::run();
    
    // 输出
    "ParentClass: hello"
    "ChildClass: hello"
  • 在Linux上进行PHP安装configure错误小结

    PHP 安装,从官网下载源码压缩包,进行 configure 遇到几个错误:

    configure 命令

    ./configure –prefix=/usr/local/php –with-config-file-path=/usr/local/php/etc –with-bz2 –with-curl –enable-ftp –enable-sockets –disable-ipv6 –with-gd –with-jpeg-dir=/usr/local –with-png-dir=/usr/local –with-freetype-dir=/usr/local –enable-gd-native-ttf –with-iconv-dir=/usr/local –enable-mbstring –enable-calendar –with-gettext –with-libxml-dir=/usr/local –with-zlib –with-pdo-mysql=mysqlnd –with-mysqli=mysqlnd –with-mysql=mysqlnd –enable-dom –enable-xml –with-libdir=lib64 –enable-pdo –enable-fpm

    BZip2 错误

    configure: error: Please reinstall the BZip2 distribution

    解决方案

    yum install bzip2
    yum install bzip2-devel

    bzip2 可能已经安装过,bzip2-devel 没有安装

    libcurl 错误

    configure: error: Please reinstall the libcurl distribution –
    easy.h should be in <curl-dir>/include/curl/

    解决方案

    yum install curl-devel

    GD 库错误

    configure: error: jpeglib.h not found

    解决方案

    yum install libjpeg
    yum -y install libjpeg-devel

    libjpeg 可能已经安装过,libjpeg-devel 没有安装

  • MySQL设置root远程访问、内网访问

    使用 root 登录 mysql 服务器

    use mysql;
    // 设置 root 支持来自任意客户端可以访问
    update user set host = ‘%’ where user = ‘root’;

    // 设置 root 支持来自内网 IP 192.168.* 可以访问
    update user set host = ‘192.168%’ where user = ‘root’;

    user 中可能有多条 root 记录,执行上述语句更新时,可以先查询出 host、user 列表(select host,user from mysql.user),执行 update 语句时增加一个 where 条件判断,只更新其中一个 host。

  • Linux开启启动脚本

    Linux开机启动,启动 mysql,log,web service 等,将执行脚本的命令放置在 /etc/rc.d/rc.local 中,系统启动之后自动执行。

    比如在 rc.local 增加一行 /web/server/nginx, 用于启动 nginx 服务。

    Linux启动步骤

    1. 加载BIOS
    2. 读取MBR
    3. Boot Loader / Grup
    4. 加载内核
    5. 用户层init依据inittab文件来设定运行等级
    6. init进程执行rc.sysinit
    7. 启动内核模块
    8. 执行不同运行级别的脚本程序(/etc/rc.d/rc $RUNLEVEL # $RUNLEVEL为缺省的运行模式 )
    9. 执行/etc/rc.d/rc.local(初始化完成后执行 rc.local 中的命令)
    10. 执行/bin/login程序,进入登录状态
  • 修改SVN提交log

    采用SVN进行代码管理,在平时开发中常遇到代码提之后,查看提交日志,发现提交的comment跟实际功能不够贴切,想要优化。或者代码提交之后发现没有写comment,想要补充。

    TortoiseGit 默认必须填写comment,TortoiseSVN没有限制,难免出现点击提交按钮时没有填写comment。

    使用TortoiseSVN修改comment,大致遇到

    ask the administrator to create a pre-revprop-change hook

    这样的提示。

    怎样 create a pre-revprop-change hook ?

    在 Linux 系统下:

    1. 进入 svn 仓库的 hooks 目录
    2. 拷贝 pre-revprop-change.tmpl 为 pre-revprop-change
    3. 给 pre-revprop-change 增加执行权限

    完成设置,重新使用 TortoiseSVN 修改comment即可。

  • SVN无法显示Log

    使用TortoiseSVN无法查看文件提交日志,在服务器执行 svn log wp-login.php 看到的信息是

    svn: Item is not readable

    此时修改 svnserve.conf 文件,将 anon-access = read 修改为 anon-access = none

    # 修改前
    anon-access = read
    # 修改后
    anon-access = none

    再次执行 svn log wp-login.php 可以看到文件提交日志。

  • 修改WordPress标题分隔符

    我的博客升级到 4.5.1 之后,网页标题的分隔符从中文竖线“|”变成了“-”,刚开始看着不太习惯,而且发现源代码中“–”被转义成了实体“&#8211;”,这样看着更不舒服。

    于是尝试修改WordPress网页标题分隔符。

    刚开始仍然想使用“|”作为分隔符,后来决定参考网页标题分隔符采取哪一种比较好。

    关于标题分隔符的建议:http://jingyan.baidu.com/article/bad08e1eae3fbb09c851213c.html

    发现腾讯和网易都采用“_”作为标题分隔符,于是也计划改成“_”。

    WordPress似乎没有提供配置修改网页标题分隔符。

    修改方法

    修改文件 wp-includes/general-template.php

    // 第 1022 行
    // 修改前
    $sep = apply_filters( 'document_title_separator', '-' );
    // 修改后
    $sep = apply_filters( 'document_title_separator', '_' );
    
    // 第 1038 行
    // 修改前
    $title = implode( " $sep ", array_filter( $title ) );
    // 修改后
    $title = implode( "$sep", array_filter( $title ) );