Aug 14

博客迁移记#2 不指定

felix021 @ 2011-8-14 12:10 [IT » 其他] 评论(5) , 引用(0) , 阅读(7932) | Via 本站原创
Godaddy的虚拟主机用了一年半,不想用了。价格不算很便宜,虽然配置还行,但毕竟是虚拟主机,限制多。

前几天在 czyhd同学 的推荐下,到forum.ubuntu.org.cn去买了那边代购的burstNET的VPS。

是OpenVZ的Linux VPS,很便宜,35/月,1G CPU + 512MB RAM + 20G HDD + 1TB/month 流量。显然是超售的,不过不要紧,够用就行。

说到VPS,现在低价VPS主要就是Xen和OpenVZ型的。

Xen型的VPS虚拟化的层次比较低(内核和硬件之间),所以可用性还较高,可以自己编译安装内核模块,挂载SWAP,而且不容易超售(所以价格偏高一点)。

OpenVZ型的VPS,虚拟化层次比较高(内核和App之间),内核是固定的,没法定制化(比如说没有ppp模块,不能安装PPTP VPN),不能用SWAP,一旦申请的内存超过VPS的内存配额,就会失败。可用性略低一点。不过对于上层用户看来,还是一个完整的机器,而且"重启"速度超快,一般只要两三秒即可。

搞了VPS,有些事情就必须自己考虑了,比如说安全问题,比如说内存资源的管理。

安全上,不想搞太多,禁止ROOT登录,并装了个 denyhosts ,将多次尝试SSH登录失败的IP ban掉。就这样。

按照以前的思路,先搞了个Apache+PHP+MySQL,结果一开机内存就占用250M。原先在Godaddy上面,因为是虚拟主机,所以是不允许程序长时间运行,所以我用Dropbox备份网站的时候,是每隔4个小时跑一次。本以为在VPS上面就没有这个限制了,但是我发现,Dropbox的内存占用非常高……一打开就需要占用150M,并且随着时间增加,占用的内存会不断增加,最终把所有RAM耗尽。

于是需要优化。首先当然是要 Apache 换成nginx。用了论坛上oneleaf提供给VPS的lnmp.sh,很快就换了过来,然后发现内存占用没少……ps aux看了一下,发现php-fpm进程跑了10个,nginx的work也有5个,于是修改/etc/php5/fpm/php5-fpm.conf  把 pm.max_children = 10改成5;修改 /etc/nginx/nginx.conf 把worker_process 5修改成3。重启以后效果显著,内存占用降到了180M。

MySQL也是个内存大户,修改/etc/mysql/my.cnf,将query_cache_size降到8M,key_buffer降到4M,重启后,内存占用减少接近20M。

更进一步,使用dropbear来替换openssh server。openssh server的sshd占用内存比较大(4M+),如果一次多开几个terminal(比如用secureCRT,这个非常正常),内存占用马上就会上升一大截。Dropbear则只需要占用大约1M,相当节约。不过有个坏处就是,Dropbear的socks5 proxy似乎不如sshd,像twitter和facebook就总是打不开。

经过内存优化,开机以后内存占用仅154M,对于我这个以个人Blog为主的小站而言,已经比较充裕,不是很有必要进一步再压榨性能了 :D

不过还有一些棘手的问题,下回再写。

p.s. 这里有一个超省内存的教程 http://www.lowendbox.com/blog/yes-you-can-run-18-static-sites-on-a-64mb-link-1-vps/
Jul 22

无聊的scanf 不指定

felix021 @ 2011-7-22 16:36 [IT » 程序设计] 评论(1) , 引用(0) , 阅读(6930) | Via 本站原创
#include <stdio.h>

int main()
{
    int a, b;
    scanf("%d%*[, ]%d", &a, &b);
    printf("%d + %d = %d\n", a, b, a + b);
    return 0;
}

<= 1 2
=> 1 + 2 = 3

<= 1,2
=> 1 + 2 = 3

<= 1 , 2
=> 1 + 2 = 3
Jun 29

[linux] mv的一个BUG? 不指定

felix021 @ 2011-6-29 11:50 [IT » 其他] 评论(3) , 引用(0) , 阅读(10571) | Via 本站原创
如下
felix021@localhost:/tmp/test$ mkdir -p a/c/d b/c/d
felix021@localhost:/tmp/test$ mv b/c a/
mv: cannot move `b/c' to a subdirectory of itself, `a/c'
Jun 24
使用以下脚本来编译安装,需要把安装文件下载到 [ROOT]/src/ 下。

目录结构:

[ROOT]
    /src
        /php-5.2.17.tar.bz2
        ...
    /httpd
    /php
    /fcgi-bin

#!/bin/bash

set -x

ROOT="/home/felix021/lamp"
ROOT_E=${ROOT//\//\\\/}        # escape for sed, / => \/

WWWROOT="/home/felix021/lamp/wwwroot"
WWWROOT_E=${WWWROOT//\//\\\/}  # escape for sed, / => \/

HTTPD_PORT=80

FCGI_CHILDREN=4
FCGI_MAX_REQUESTS=1000

# 下载好的源码文件
fastcgi="mod_fastcgi-2.4.6"        #.tar.gz
httpd="httpd-2.2.17"                #.tar.bz2
php="php-5.2.17"                    #.tar.bz2
eaccelerator="eaccelerator-0.9.6.1" #.tar.bz2

SRCROOT=${ROOT}/src
mkdir -p $ROOT/{httpd,php,fcgi-bin}
mkdir -p $WWWROOT

# fcgi-bin (for apache + mod_fastcgi) 
# 创建这个脚本可以用来包装php-cgi,可以控制启动的FCGI进程数量
if [ ! -e "${ROOT}/fcgi-bin/php.cgi" ]; then
    > ${ROOT}/fcgi-bin/php.cgi echo "#!/bin/sh
# Shell Script To Run PHP5 using mod_fastcgi under Apache 2.x

### Set PATH ###
PHP_CGI=${ROOT}/php/bin/php-cgi
PHP_FCGI_CHILDREN=${FCGI_CHILDREN}
PHP_FCGI_MAX_REQUESTS=${FCGI_MAX_REQUESTS}

### no editing below ###
export PHP_FCGI_CHILDREN
export PHP_FCGI_MAX_REQUESTS
exec \$PHP_CGI
"
    chmod +x ${ROOT}/fcgi-bin/php.cgi
fi

# php
cd $SRCROOT
if [ ! -d "${php}" ]; then
    tar jxf ${php}.tar.bz2
    cd $php
    ./configure --prefix=${ROOT}/php --with-gd --with-iconv \
        --with-curl --enable-fastcgi --with-openssl --enable-mbstring  #有需要的话自己再增加一些模块吧
    echo Making... Please wait. "tail -f `pwd`/php.log" to view make output.
    make -j4 &> php.log
    make install
    cp php.init-dist ${ROOT}/php/lib/php.ini
fi

# eaccelerator
if [ ! -d "${eaccelerator}" ]; then
    tar jxf ${eaccelerator}.tar.bz2
    cd $eaccelerator
    export PHP_PREFIX=${ROOT}/php
    $PHP_PREFIX/bin/phpize
    ./configure --enable-eaccelerator=shared \
        --with-php-config=$PHP_PREFIX/bin/php-config
    make -j4 &> eaccelerator.log
    make install
    echo Please edit php.ini to enable eaccelerator.so
fi


# httpd
cd $SRCROOT
if [ ! -d "${httpd}" ]; then
    tar jxf ${httpd}.tar.bz2
    cd ${httpd}
    ./configure --prefix=${ROOT}/httpd --enable-rewrite
    echo Making... Please wait. "tail -f `pwd`/httpd.log" to view make output.
    make -j4 &> httpd.log
    make install

    conf_path="${ROOT}/httpd/conf/httpd.conf"

    # 下面这段sed脚本是把端口、DocRoot替换了,并允许目录下使用.htaccess来配置url rewrite
    sed -i $conf_path \
        -e "s/^Listen 80$/Listen ${HTTPD_PORT}/" \
        -e "s/\/.*\/htdocs\>/${WWWROOT_E}/" \
        -e "s/AllowOverride None/AllowOverride FileInfo/"

    fcgi_path="${ROOT}/fcgi-bin/"

    # 在配置中加入FastCgi相关的内容
    echo "
LoadModule fastcgi_module modules/mod_fastcgi.so

FastCgiServer \"${fcgi_path}php.cgi\"
AddHandler php-fastcgi .php

ScriptAlias /fcgi-bin/ \"${fcgi_path}\"
Action php-fastcgi "/fcgi-bin/php.cgi"
AddType application/x-httpd-php .php

<Directory \"${fcgi_path}\">
    SetHandler fastcgi-script
    Options FollowSymlinks +ExecCGI
    Order Allow,Deny
    Allow from All
</Directory>
"  >> $conf_path

fi

# mod_fastcgi for httpd
cd $SRCROOT
if [ ! -d "${fastcgi}" ]; then
    tar zxf ${fastcgi}.tar.gz
    cd ${fastcgi}
    cp Makefile.AP2 Makefile
    make top_dir=${ROOT}/httpd -j4 &> fastcgi.log
    make top_dir=${ROOT}/httpd install
fi

echo Done. Please run "${ROOT}/httpd/bin/apachectl start"
Jun 20
=> How to call a php function in a php extension

详细的说明参见:

[php-5.2.17]
    /Zend/zend_execute_API.c +623 & +636    call_user_function_ex
    /ext/standard/basic_functions.c +5174    PHP_FUNCTION(call_user_func_array)                                                                       
    /ext/pcre/pcre.c +833  (in function "preg_do_repl_func")

http://man.chinaunix.net/develop/php/php_manual_zh/html/zend.calling-user-functions.html

http://www.phpfreaks.com/forums/index.php?topic=10272.0

PHP_FUNCTION (caller)
{
    //call_user_function_ex
    zval *function, *str, *arr;
    zval **params[10];

    MAKE_STD_ZVAL(function);
    ZVAL_STRING(function, "var_dump", 1);  //I wanna call var_dump

    /*  //pass a string as its param
    MAKE_STD_ZVAL(str);
    ZVAL_STRING(str, "Hello, world!", 1);

    params[0] = &str;
    */

    //pass an array as its param
    MAKE_STD_ZVAL(arr);
    array_init(arr);
    add_assoc_string(arr, "name", "felix021", 1);
    params[0] = &arr;

    zval *ret;
    if (call_user_function_ex(CG(function_table), NULL,
            function, &ret, 2, params, 0, NULL TSRMLS_CC) != FAILURE)
    {
        *return_value = *ret;
        zval_copy_ctor(return_value);
        zval_ptr_dtor(&ret);
        return;
    }
    else {
        RETURN_FALSE;
    }
}

Jun 18
网上摘下来的代码,没什么好说的了(因为那一堆 mcrypt_* 函数太乱了,用就是了):
<?php

class DES
{
    public static function pkcs5_pad ($text, $blocksize) {
        $pad = $blocksize - (strlen($text) % $blocksize);
        return $text . str_repeat(chr($pad), $pad);
    }

    public static function pkcs5_unpad($text) {
        $pad = ord($text{strlen($text)-1});
        if ($pad > strlen($text)) {
            return false;
        }
        if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
            return false;
        }
        return substr($text, 0, -1 * $pad);
    }

    public static function encrypt($key, $data) {
        $size = mcrypt_get_block_size('des', 'ecb');
        $data = DES::pkcs5_pad($data, $size);
        $td = mcrypt_module_open('des', '', 'ecb', '');
        $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        @mcrypt_generic_init($td, $key, $iv);
        $data = mcrypt_generic($td, $data);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return $data;
    }

    public static function decrypt($key, $data) {
        $td = mcrypt_module_open('des','','ecb','');
        $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        $ks = mcrypt_enc_get_key_size($td);
        @mcrypt_generic_init($td, $key, $iv);
        $decrypted = mdecrypt_generic($td, $data);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        $result = DES::pkcs5_unpad($decrypted);
        return $result;
    }
}

/*  test code

$in = "04fMaWegkH1/BL9CNYxgusFpYK8wdraBX06mPiRmxJP+uVm31GQvyw==";
$des = base64_decode($in);
echo DES::decrypt("12345678", $des);
echo "\n";

$in = "cea3e8e1659582206e0be32539729e9f";
$des = DES::encrypt("12345678", $in);
$out = base64_encode($des);
echo $out;
echo "\n";

// */

?>
Jun 18
在"libc 4.6.27 and later, and glibc 2.1 and later"中,提供了 rpc/des_crypt.h这个头文件,其中有几个函数,比如:
void des_setparity(char *key);
int ecb_crypt(char *key, char *data, unsigned datalen, unsigned mode);

这个函数可以用于DES的加密/解密。详情可以看 man des_crypt ,以下说 ecb_crypt() 函数几个比较坑爹的地方:

1. 虽然提供给DES的密钥(key)是8个字节,但是实际上只用到了其中的56个bit,另外8个bit是用于奇偶校验的(从用户处取得一个64位长的密码key ,去除64位密码中作为奇偶校验位的第8、16、24、32、40、48、56、64位,剩下的56位作为有效输入密钥)。所以需要调用 des_setparity(key) 来处理key。

2. 必须在data后面补上1~8个 "\x8",以将datalen补齐到8的倍数。对,是1~8个。假设要加密的data是32个字节,需要先补齐到40个字节。

3. 传给des_setparity()的key和给ecb_crypt的data会被直接改写。

以下是样例代码:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <rpc/des_crypt.h>

//注意:这里有个坑,需要保证 data 可以存放的字符串在后面补了\8 不会越界。否则结果是不确定的。
void des_encrypt(const char *key, char *data, int len)
{
    char pkey[8];
    strncpy(pkey, key, 8);
    des_setparity(pkey);
    do {
        data[len++] = '\x8';
    } while (len % 8 != 0);
    ecb_crypt(pkey, data, len, DES_ENCRYPT);
}

void des_decrypt(const char *key, char *data, int len)
{
    char pkey[8];
    strncpy(pkey, key, 8);
    des_setparity(pkey);
    ecb_crypt(pkey, data, len, DES_DECRYPT);
}

int main(int argc, char *argv[])
{
    /*
    * char data[4096] = "cea3e8e1659582206e0be32539729e9f";
    * des_encrypt("12345678", data, strlen(data));
    * printf("%s\n", data);
    * //should be "04fMaWegkH1/BL9CNYxgusFpYK8wdraBX06mPiRmxJP+uVm31GQvyw=="
    */

    char data[4096];
    int i = 0;
    while (EOF != (data[i] = fgetc(stdin))) {
        i++;
    }
    data[i] = '\0';
    des_decrypt("12345678", data, strlen(data));
    printf("%s\n", data);
    /*
    * echo -n 04fMaWegkH1/BL9CNYxgusFpYK8wdraBX06mPiRmxJP+uVm31GQvyw== | base64 -d | ./des
    * should be "cea3e8e1659582206e0be32539729e9f"
    */
    return 0;
}
Jun 15

坑爹的crontab #2 不指定

felix021 @ 2011-6-15 12:25 [IT » 操作系统] 评论(2) , 引用(0) , 阅读(14474) | Via 本站原创
我喜欢在自己的 $HOME 下面建立一个 bin ,然后添加到 $PATH 中,这样我可以方便地执行自己的程序:

$ mkdir ~/bin
$ vi .bashrc
export PATH=$PATH:~/bin

不过坑爹的 crontab 对 .bashrc 并不买账,通过

* * * * *  echo $PATH > ~/cronpath.txt

可以看出, crontab 执行命令时没有把环境变量给切过来。

$ cat ~/cronpath.txt
/usr/bin:/bin
分页: 23/99 第一页 上页 18 19 20 21 22 23 24 25 26 27 下页 最后页 [ 显示模式: 摘要 | 列表 ]