登录 |

openresty:把nginx变成应用服务器

2011年12月23日 下午 54:57 | 作者:hemon

http://openresty.org

  1. 安装基础库
    apt-get install libreadline-dev libpcre3-dev libssl-dev perl
  2. 安装libdrizzle
    wget http://agentzh.org/misc/nginx/drizzle7-2011.07.21.tar.gz
    tar xzvf drizzle7-2011.07.21.tar.gz
    cd drizzle7-2011.07.21/
    ./configure –without-server
    make libdrizzle-1.0
    make install-libdrizzle-1.0
  3. 安装openresty
    wget http://agentzh.org/misc/nginx/ngx_openresty-1.0.10.24.tar.gz
    tar xzvf ngx_openresty-1.0.10.24.tar.gz
    ./configure –with-luajit –with-http_drizzle_module
    make -j2 # 2是CPU双核,单核就直接 make
    make install
  4. 安装目录:/usr/local/openresty,写程序就是改nginx配置
    /usr/local/openresty/nginx/conf/nginx.conf
  5. 怎么用nginx.conf写程序呢?

    http://agentzh.org/misc/slides/nginx-conf-scripting/

    http://agentzh.org/misc/slides/recent-dev-nginx-conf/

    http://agentzh.org/misc/slides/nginx-state-of-the-art/

    http://agentzh.org/misc/slides/perl-lz-apps/

timeout

2011年10月30日 上午 47:09 | 作者:hemon

Nginx

client_header_timeout = 60
client_body_timeout = 60
send_timeout = 60

fastcgi_connect_timeout = 60
fastcgi_read_timeout = 60
fastcgi_send_timeout = 60

proxy_connect_timeout = 60
proxy_read_timeout = 60
proxy_send_timeout = 60

PHP-FPM

request_terminate_timeout = 0
request_slowlog_timeout = 0

PHP
max_input_time = 60
max_execution_time = 30

使用SQL-SERVER查询Excel的数据

2011年10月28日 下午 11:54 | 作者:hemon

这里用到了OPENROWSET,生成一个数据源:
OPENROWSET(‘Microsoft.Jet.OLEDB.4.0′, ‘Excel 8.0;HDR=Yes;IMEX=1;Database=C:\book1.xls’, [sheet1$])
文件名:C:\book1.xls
工作表名:sheet1

[sheet1$] $是结束符,工作表名是sheet1

文件名、工作表名可以含有汉字,空格……总之就是SQL-SERVER很NB。

例如:
SELECT * FROM OPENROWSET(‘Microsoft.Jet.OLEDB.4.0′, ‘Excel 8.0;HDR=Yes;IMEX=1;Database=C:\book1.xls’, [sheet1$])

实现Excel干不了的:JOIN联表查询!

SELECT *
FROM
OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;HDR=Yes;IMEX=1;Database=C:\book1.xls', [sheet1$]) AS a
LEFT OUTER JOIN
OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;HDR=Yes;IMEX=1;Database=C:\book1.xls', [sheet2$]) AS b
ON a.ID = b.ID

MySQL运行时启用general_log

2011年10月28日 上午 02:48 | 作者:hemon

可以运行时 启动/终止 MySQL查询log

// 1.1记录到mysql数据库mysql.general_log中,engine=CSV,文件在/var/lib/mysql/mysq/general_log.CSV
SET GLOBAL log_output=’TABLE’;
// 1.2.记录到文本文件
SET GLOBAL log_output=’FILE’;
SET GLOBAL general_log_file=’/tmp/general.log’; // 指定输出日志文件
// 2. 启动
SET GLOBAL general_log=ON;
// 3. 关闭
SET GLOBAL general_log=OFF;

// 4.1 读取mysql.general_log
SELECT * FROM mysql.general_log;.
tail -f /var/lib/mysql/mysq/general_log.CSV;
// 4.2 读取文本日志
tail -f /tmp/general.log;

PDO-MYSQL支持多语句执行

2011年10月28日 上午 37:31 | 作者:hemon

使用mysql_query/mysqli_query都不支持分号分隔的多语句执行,会报1064错误,神奇的PDO-MySQL->exec()通过了!
对于MySQL注入来说,无疑是大开了方便之门
对于MySQL开发来说,可以批量执行SQL,不用自己foreach一条一条搞了。

示例执行SQL:
select * from test;DELETE FROM test WHERE qq = ’1′

$link = mysql_connect('localhost','root','zzzizzz1');
mysql_select_db('xm_shop');
$result = mysql_query("select * from test;DELETE FROM test WHERE qq = '1'");
if(!$result){
        echo mysql_errno($link) . ": " . mysql_error($link) . "\n";
}
$mysqli = new mysqli("localhost", "root", "zzzizzz1", "xm_shop");
$result = $mysqli->query("select * from test;DELETE FROM test WHERE qq = '2'");
if (!$result) {
	echo $mysqli->errno . ": " . $mysqli->error . "\n";
}
/*
mysqli->multi_query() 支持多语句执行
*/
$result = $mysqli->multi_query("select * from test;DELETE FROM test WHERE qq = '2'");
if (!$result) {
	echo $mysqli->errno . ": " . $mysqli->error . "\n";
}

/*
PDO-MYSQL 支持多语句执行
*/
$db = new PDO('mysql:dbname=xm_shop;host=localhost', 'root', 'zzzizzz1');
$db->exec("select * from test;DELETE FROM test WHERE qq = '0'");

// stmt 设置ATTR_EMULATE_PREPARES = 0,禁用多语句查询
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
$sql = "select * from test;DELETE FROM test WHERE qq = '0'";
if( $stmt = $db->prepare($sql) ){
	$stmt->execute();
}

启用genneral_log,查看日志,3种扩展执行的查询并没有什么不同。
难道mysql-server的api是支持多语句执行,只有pdo扩展实现了多语句执行返回结果的处理?
1、mysql
Connect,root@localhost on
Init DB,xm_shop
Query,select * from test;DELETE FROM test WHERE qq = ’1′
2、mysqli
Connect,root@localhost on xm_shop
Query,select * from test;DELETE FROM test WHERE qq = ’2′
Quit,
3、PDO
Connect,root@localhost on xm_shop
Query,select * from test;DELETE FROM test WHERE qq = ’0′
Quit,

参考文献:

http://blog.ulf-wendel.de/?p=192

Ubuntu装NV显卡

2011年10月15日 下午 03:40 | 作者:hemon

sudo add-apt-repository ppa:ubuntu-x-swat/x-updates
sudo apt-get update
sudo apt-get install nvidia-current nvidia-settings

分页函数

2011年09月5日 下午 54:14 | 作者:hemon

以前开培训班,教学生写的一个分页函数

function page_html($page, $total, $size=10, $show=10, $url=null, $name='page'){
    $page  = intval($page);
    $page  = ($page == 0) ? 1 : $page;

    $max   = ceil($total/$size);
	$page  = ($page > $max) ? $max : $page;
	$show  = ($show < $max) ? $show : $max;

    $start = page_start($page, $max, $show);
    $end   = $start + $show;

    $url   = page_url($url, $name);

	for($i = $start; $i < $end; $i++){
		if( $i == $page){
			$html .= "$i";
		} else {
			$html .= "$i";
		}
	}
	return $html;
}

function page_url($url=null, $name='page'){
    $url = ( empty($url) ? $_SERVER['REQUEST_URI'] : $url);
    list($url, $query) = explode('?', $url, 2);

    if( !empty($query) ){
        $query = preg_replace("|$name=\d+&?|", '', $query);
        $query = trim($query, '&?') . '&';
    }

    $url .= '?' . $query . $name . '=';
    return $url;
}

function page_start($page, $max, $show=10){
	$start = 1;
	if( $max > $show ){
		$offset = floor($show/2);
		switch (1){
			//[12]3456789
			case ($page <= $offset):
				$start = 1;
				break;
			//1234567[89]
			case ($page > ($max - $offset)):
				$start = $max - $show + 1;
				break;
			//12[34567]89
			default:
				$start = $page - $offset;
				break;
		}
	}
	return $start;
}

shorewall限速

2011年08月28日 下午 57:49 | 作者:hemon

根据规则控制分配优先级/带宽占用。

  1. tcdevices 设备总带宽
    #INTERFACE      IN-BANDWIDTH    OUT-BANDWIDTH
    #eth1为外网访问接口,常用的还有ppp0这样的拨号连接
    eth1            4mbit           4mbit
  2. tcrules 规则
    #MARK   SOURCE          DESTINATION     PROTOCOL        PORT(S) SOURCE PORT(S)
    # MARK就是编号,设定规则source:port -> dest:port
    1       0.0.0.0/0       0.0.0.0/0       tcp             22
    1       0.0.0.0/0       0.0.0.0/0       udp             22
    2       0.0.0.0/0       0.0.0.0/0       tcp             53
    2       0.0.0.0/0       0.0.0.0/0       udp             53
    3       0.0.0.0/0       0.0.0.0/0       tcp             110
    3       0.0.0.0/0       0.0.0.0/0       udp             110
    4       0.0.0.0/0       0.0.0.0/0       tcp             80,443
  3. tcclasses 设置优先级/带宽
    #INTERFACE      MARK    RATE    CEIL            PRIORITY        OPTIONS
    #tcclasses针对tcrules里面对应的MARK,设定优先级Priority,带宽(最小RATE->最大CEIL)
    #full代表总带宽,full/2就是一半,full*50/100也是一半,可以用表达式,RATE初始带宽应该不高于100% full。
    eth1            1       2*full/100      full    1
    eth1            2       20*full/100     full    2
    eth1            3       10*full/100     full    3
    eth1            4       64*full/100     full    4
    eth1            5       4*full/100      full    5               default

参考文献:

http://www.shorewall.net/traffic_shaping.htm

http://www.shorewall.net/traffic_shaping.htm#perIP 每ip限速

http://wiki.nginx.org/NginxHttpCoreModule#limit_rate  Nginx 每个连接限速

http://www.ckollars.org/shaping.html

eth1            0mbit           3mbit

用u盘装ubuntu?Failed to determine codename for release

2011年08月26日 下午 03:41 | 作者:hemon

2010年12月20日, 3:09:56

月黑风高的晚上,回学校重装服务器,历经9×9=81难,先是机房被锁,请开锁师傅花了50大洋,然后又遇到服务器光驱坏了,用1G小u盘刻录ubuntu.iso安装,然后遇到了“Failed to determine codename for release”君,战了一通宵;今天清理桌面,把安装要点笔记加注释:

  1. 先用ultraiso以hdd+模式刻录iso到u盘
  2. 删除pool文件夹(如果你u盘大,也可以不删除,主要是为了第3步留出空间)
  3. 再把iso拷贝到u盘根目录
  4. 从u盘启动
  5. 安装过程中,会报错“Failed to determine codename for release”!解决办法就是把iso重新挂载到光驱:
    1. alt+f2  切换到cmd模式
    2. 检查当前设备名 ls /dev/sd*,以下假设u盘挂载后的设备名为sdb1
    3. 挂载iso到光驱设备:
      1. mkdir /mnt/sdb1
      2. mount /dev/sdb1 /mnt/sdb1
      3. mount -o loop -t iso9660 /mnt/sdb1/ubuntu-10.04-server-amd64.iso /cdrom
  6. alt+f1返回安装界面
  7. 选择”Load Installer Components from an ISO Image”
  8. The end.

webgrind支持dot生成流程图了!

2011年08月20日 下午 27:41 | 作者:hemon

webgrind已经更换到github了,https://github.com/jokkedk/webgrind

终于更新了一下下,

* Generate a call graph using gprof2dot.py // 使用gprof2dot.py生产流程图

安装dot

sudo apt-get install graphviz

配置dot路径

vim config.php

static $dotExecutable = ‘/usr/local/bin/dot’;

static $dotExecutable = ‘/usr/bin/dot’;

查看流程图

 

btw,xhprof的web_gui早就支持dot了。

https://github.com/facebook/xhprof

http://mirror.facebook.net/facebook/xhprof/doc.html

 1 2 3 4 5 6 7 8 9 10 ...14 15 16 下一页
©hemono.com 赣ICP备07004153号