将 Ngrok Conf Editor 加入自启动项

Centos 7:

1
vi /usr/lib/systemd/system/ngrokConfEditor.service

1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
Description=ngrok conf editor
Documentation=https://github.com/jshensh/ngrokConfEditor
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/node-v4.4.2-linux-armv7l/bin/forever start /ngrokConfEditor/app.js
ExecStop=/usr/local/node-v4.4.2-linux-armv7l/bin/forever stop /ngrokConfEditor/app.js
PrivateTmp=true

[Install]
WantedBy=multi-user.target
1
2
systemctl enable ngrokConfEditor.service
systemctl reboot

Raspbian:

1
vi /etc/init.d/ngrokconfeditor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/sh
### BEGIN INIT INFO
# Provides: ngrokconfeditor
# Required-Start: $remote_fs
# Required-Stop: $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start or stop the HTTP Proxy.
### END INIT INFO
case $1 in
start)
/usr/local/node-v4.4.2-linux-armv7l/bin/forever start /ngrokConfEditor/app.js
;;
stop)
/usr/local/node-v4.4.2-linux-armv7l/bin/forever stop /ngrokConfEditor/app.js
;;
*)
echo "Usage: $0 (start|stop)"
;;
esac
1
2
update-rc.d ngrokconfeditor defaults
reboot

CentOS 安装 pip

之前有的朋友问我,CentOS版本怎么安装python的pip,我之前给出的Ubuntu下apt-get的方法
ubuntu 需要先安装下pip吧,

1
apt-get install python-pip

安装requests

1
pip install requests

但是有的朋友由于是centos的,直接 yum install pip 或者 yum install python-pip 都是不行的。
其实不能yum那就直接下载编译安装呗。。。

方法如下:
截至写本文的时候,pip最新为 8.1.1

1
wget --no-check-certificate https://github.com/pypa/pip/archive/8.1.1.tar.gz

注意:wget获取https的时候要加上:–no-check-certificate

1
2
3
tar zvxf 8.1.1.tar.gz    #解压文件
cd pip-8.1.1/
python setup.py install

OK,这样就安装好pip了,

下面来安装 requests吧。

1
pip install requests

转载自 http://www.linuxde.net/2014/05/15576.html 有删改

树莓派系统的备份

本文所在环境:
PC:Ubuntu 15.10 live
Pi2:Centos 7.2,已将 Boot 分离(可参考教程 将树莓派系统迁移至 U 盘上

第一步,压缩系统分区
打开 GParted
Screenshot from 2016-04-06 17-54-44
切换至你的设备
Screenshot from 2016-04-06 17-56-50
卸载设备
Screenshot from 2016-04-06 17-57-24
按需压缩
Screenshot from 2016-04-06 17-58-23
有个警告提示,Apply 即可
Screenshot from 2016-04-06 17-59-05
等待操作完成即可进行下一步操作
Screenshot from 2016-04-06 10-02-59

第二步,导出全盘
Screenshot from 2016-04-06 10-16-07
注:

  1. 所有操作均需提权后才能进行
  1. fdisk -l 出来最后分区的 End Block 是 dd 中的 count 参数

第三步,还原分区(可选)
按第一步操作,还原分区大小

还原备份文件

1
dd if=/YourPath/img.img of=/dev/YourDevice

即第二步中的 if of 参数对换并去掉其他的无用参数

树莓派教程:利用 Ngrok 穿透内网访问树莓派

我的树莓派接在局域网中,如果在外网对其进行访问,以前只能选择花生壳,而现在则有了开源方案 Ngrok。Ngrok 可以选择用官网的服务,不过很可惜的是被墙了,如果不怕麻烦,也可以翻墙使用,我们这里选择自建 Ngrok 服务。Ngrok 分为服务端和客户端,服务端需安装在有独立 IP 的外网中,我用的是 Ramnode KVM VPS,客户端安装在处于内网的树莓派中。

编译服务端、客户端
下载 golang,准备编译环境:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
wget http://www.golangtc.com/static/go/1.6/go1.6.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.6.linux-amd64.tar.gz
cat >>/etc/profile<<EOF
export PATH=$PATH:/usr/local/go/bin
EOF
source /etc/profile
#检查下是否成功
go version
#在当前用户目录下新建go目录作为项目目录
mkdir -p $HOME/go
#用cat的方法在尾部增加配置配置golang的 GOROOT GOPATH
cat >>$HOME/.bash_profile<<EOF
export GOROOT=/usr/local/go
export GOPATH=\$HOME/go
export PATH=\$PATH:\$GOROOT/bin
EOF
#让配置生效
source $HOME/.bash_profile
#检查下go的env环境变量
go env

下载 ngrok

1
2
git clone https://github.com/inconshreveable/ngrok.git
cd ngrok

生成自签名 SSL 证书,example.com 替换为自己的域名:

1
2
3
4
5
openssl genrsa -out base.key 2048
openssl req -new -x509 -nodes -key base.key -days 10000 -subj "/CN=example.com" -out base.pem
openssl genrsa -out server.key 2048
openssl req -new -key server.key -subj "/CN=example.com" -out server.csr
openssl x509 -req -in server.csr -CA base.pem -CAkey base.key -CAcreateserial -days 10000 -out server.crt

将原始的 ngrokroot.crt 替换为生成的 pem 文件:

1
2
3
cp base.pem assets/client/tls/ngrokroot.crt
cp server.crt assets/server/tls/snakeoil.crt
cp server.key assets/server/tls/snakeoil.key

编译 Ngrok 服务端:

1
GOOS=linux GOARCH=amd64 make release-server

编译 Ngrok 客户端:

1
GOOS=linux GOARCH=arm make release-client

将服务端和客户端分别拷贝到 VPS 和树莓派的可执行路径下。

设置域名解析
把域名 example.com 解析到 VPS 上,假如只需要一个二级域名 abc,可直接设置将其 A 记录解析到 VPS 的 IP 上,如果需要很多二级域名,可以直接把 * 解析到 IP 上。
7hvs9nrxl84n4uoe

服务端启动
从命令行启动:

1
./bin/ngrokd -tlsKey=server.key -tlsCrt=server.crt -domain=example.com -httpAddr=:8080 -httpsAddr=:8081

设置为开机自动启动:

1
vim /etc/init.d/ngrokd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/sh
### BEGIN INIT INFO
# Provides: ngrokd
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: ngrokd
# Description:
#
### END INIT INFO

NAME=ngrokd
DAEMON=/home/user/ngrok/bin/$NAME
KEY=/home/user/ngrok/server.key
CRT=/home/user/ngrok/server.crt
DOMAIN="example.com"
HTTPADDR=":8080"
HTTPSADDR=":8081"

[ -x "$DAEMON" ] || exit 0

case "$1" in
start)
echo "Starting $NAME..."
start-stop-daemon --start --chuid seogod --exec $DAEMON --quiet --oknodo --background -- -tlsKey=$KEY -tlsCrt=$CRT -domain=$DOMAIN -httpAddr=$HTTPADDR -httpsAddr=$HTTPSADDR || return 2
;;
stop)
echo "Stopping $NAME..."
start-stop-daemon --stop --exec $DAEMON --quiet --oknodo --retry=TERM/30/KILL/5 || return 2
;;
restart)
$0 stop && sleep 2 && $0 start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac
exit 0
1
update-rc.d ngrokd defaults

客户端启动
对默认设置文件 /root/ngrok.cfg 进行编辑:

1
2
3
4
5
6
7
8
9
10
11
12
server_addr: example.com:4443
trust_host_root_certs: false
tunnels:
ngrok:
hostname: 'ngrok.pi.imjs.work:38482'
auth: "AuthUser:AuthPassWord"
proto:
http: 80
ssh:
remote_port: 34356
proto:
tcp: 22

从命令行启动:

1
./bin/ngrok start ngrok ssh

当客户端使用 http/https 协议连接,可指定一个二级域名,服务端会分配该二级域名给客户端作为入口,比如 web.example.com; 当客户端使用 tcp 协议连接,则服务端不会分配二级域名,改为监控一个随机端口,比如 example.com:12345,remote_port 可由客户端对该端口进行指定,比如 example.com:34356。

客户端和服务端建立连接后,访问 web.example.com:8080 可以穿透内网访问树莓派的 web 服务,执行 ssh -p34356 pi@example.com 可以穿透内网登陆树莓派的 ssh 服务。

设置为开机自动启动(使用了面板的同学请不要设置!!!):

1
vim /etc/init.d/ngrok
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/bin/sh
### BEGIN INIT INFO
# Provides: ngrok
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: ngrok
# Description:
#
### END INIT INFO

NAME=ngrok
DAEMON=/root/$NAME
CONFIG=/root/.ngrok
TUNNELS="ngrok ssh"

[ -x "$DAEMON" ] || exit 0

case "$1" in
start)
echo "Starting $NAME..."
start-stop-daemon --start --chuid pi --exec $DAEMON --quiet --oknodo --background -- -config $CONFIG start $TUNNELS || return 2
;;
stop)
echo "Stopping $NAME..."
start-stop-daemon --stop --exec $DAEMON --quiet --oknodo --retry=TERM/30/KILL/5 || return 2
;;
restart)
$0 stop && sleep 2 && $0 start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac
exit 0
1
update-rc.d ngrok defaults

参考

node.js 解析 POST 数据并返回 JSON 对象

网上许多教程都有部分遗漏,被坑惨的我查了官方教程(http://expressjs.com/en/4x/api.html#req.body),写篇文章做个笔记

后端:

1
2
3
4
5
6
7
8
9
10
11
12
var express = require('express');
var app = express();
var http = require('http').Server(app);

var bodyParser = require('body-parser');
var multer = require('multer');
var upload = multer();


app.post('/getJSON', upload.array(), function(req, res) {
res.send(JSON.stringify(req.body) || "");
});

前端:

1
2
3
4
5
6
7
8
9
10
$.ajax({
type: 'post',
url: '/getJSON',
dataType: 'json',
timeout: 5000,
data: $("#dataForm").serialize(),
success: function(re) {
console.log(re);
}
});

nginx与ngrok共存(nginx反代ngrok)

记得之前刚搭建起来ngrok时当时很兴奋觉得很舒服,但是很快产生了一个问题,ngrok占用80和443的话,那nginx该怎么办呢,虽然想过ngrok监听别的端口,但是一些程序却必须使用80和443,然后当时就想到了反代,但是泛域名反代而且端口不同,如何正确处理主机头成了问题,反代一个域名可以将主机头写成固定的,但是泛域名则是不确定主机头的,网上查了许久资料均未找到答案,后来不报希望的试了下,竟然成功了,今日想起来这件事情,便写博文记录下来。
下,竟然成功了,今日想起来这件事情,便写博文记录下来。

涉及到的配置文件
nginx的nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
user nginx;
#上面用户可根据情况换成root(不推荐)
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}

ngrok反向代理文件(主要!)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
server {
listen 80;
server_name *.0n0.win;
root html;
index index.html index.htm index.php;
location / {
proxy_pass http://127.0.0.1:81;
proxy_redirect off;
proxy_set_header Host $host:81;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}
server {
listen 443;
server_name *.0n0.win;
ssl on;
ssl_certificate /root/ngrok/rootCA.pem;
ssl_certificate_key /root/ngrok/rootCA.key;
root html;
index index.html index.htm index.php;
location / {
proxy_pass https://127.0.0.1:444;
proxy_redirect off;
proxy_set_header Host $host:444;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}

我的nginx包含的模块
那两个模块可根据自己需要自行删除或添加

1
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-ipv6 --with-http_spdy_module --add-module=/root/mk/ngx_http_substitutions_filter_module --add-module=/root/mk/headers-more-nginx-module

两个模块的git
两个都是反代时替换内容用到的

1
2
3
mkdir /root/mk &&cd /root/mk
git clone https://github.com/openresty/headers-more-nginx-module.git
git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module.git

转载自 https://blog.ni-co.moe/p/345.html

PHP_PDO 调用mysql 带返回参数的存储过程

1
2
3
4
5
6
7
DROP PROCEDURE IF EXISTS pro_test;

create procedure pro_test(in val VARCHAR(50) ,out rtn int)
begin
declare err INT default 0;
-- 如果出现异常,会自动处理并rollback
declare exit handler for sqlexception ROLLBACK ;
1
2
-- 启动事务
start transaction;
1
2
3
4
5
6
7
8
9
10
insert into test_user values(NULL,1,'啊是大三的');
-- set err = @@IDENTITY; -- = 获取上一次插入的自增ID;
set err =last_insert_id(); -- 获取上一次插入的自增ID
insert into test_user VALUES(NULL,val,err);

-- 运行没有异常,提交事务
commit;
-- 设置返回值为1
set rtn=1;
end;
1
2
3
4
5
$name = '成都市';
$stmt = $db->prepare("CALL pro_test(?,@sp_result);");
$stmt->bindParam(1, $name);
$stmt->execute ();
$outputArray = $db->query("select @sp_result")->fetch(PDO::FETCH_ASSOC);
1
print_r($outputArray["@sp_result"]);

转载自:http://blog.csdn.net/auspi12341/article/details/17167069

解决 mariadb 各种报错

系统 Ubuntu 14.04.4 LTS x64,阿里云

说来也是一个很诡异的案例,使用 lnmp 1.2(http://lnmp.org)搭建的环境,安装过程中就一直出错,好不容易编译上去了,却死活无法启动,当然最终还是解决了,写篇文章供日后参考。

1. 编译过程报错 Inconsistency detected by ld.so
原文:

1
2
Inconsistency detected by ld.so: dl-version.c: 224: _dl_check_map_versions: Assertion `needed != ((void *)0)' failed!
make: *** No targets specified and no makefile found. Stop.

百度半天无果,必应了下找到答案:http://bbs.vpser.net/viewthread.php?action=printable&tid;=12636

1
2
3
4
5
6
7
mv /usr/bin/cmake /usr/bin/cmake.backup
wget http://www.cmake.org/files/v3.0/cmake-3.0.2.tar.gz
tar zxf cmake-3.0.2.tar.gz
cd cmake-3.0.2
./configure
make && make install
ln -sf /usr/local/bin/cmake /usr/bin/cmake

2. 编译过程再报错 cc: internal compiler error: Killed

1
2
3
4
cc: internal compiler error: Killed (program cc1)
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://bugzilla.redhat.com/bugzilla> for instructions.

参考 http://bbs.vpser.net/viewthread.php?tid=11757,内存不足,添加 swap 解决问题

3. 启动报错 Can’t read from messagefile ‘/usr/share/mysql/errmsg.sys’
导致这个问题产生的原因挺多的。。搜到几个挺有用的结果,但尼玛不是我的问题。
从这里找到的灵感:http://bugs.mysql.com/bug.php?id=69677
默认配置文件路径 /etc/mysql/my.cnf
lnmp 的在 /etc/my.cnf
不知道为什么 mysql_install_db 没有把默认配置文件的路径正常初始化,导致加载错文件,最终只能覆盖默认路径下的配置文件解决问题。

使用国内镜像源来加速python pypi包的安装

pipy国内镜像目前有:

http://pypi.douban.com/ 豆瓣
http://pypi.hustunique.com/ 华中理工大学
http://pypi.sdutlinux.org/ 山东理工大学
http://pypi.mirrors.ustc.edu.cn/ 中国科学技术大学

对于pip这种在线安装的方式来说,很方便,但网络不稳定的话很要命。使用国内镜像相对好一些,

如果想手动指定源,可以在pip后面跟-i 来指定源,比如用豆瓣的源来安装web.py框架:

1
pip install web.py -i http://pypi.douban.com/simple

注意后面要有/simple目录!!!

要配制成默认的话,需要创建或修改配置文件(linux的文件在~/.pip/pip.conf,windows在%HOMEPATH%\pip\pip.ini),修改内容为:

1
2
[global]
index-url = http://pypi.douban.com/simple

这样在使用pip来安装时,会默认调用该镜像。
更多配置参数见:http://www.pip-installer.org/en/latest/configuration.html

转载自 http://topmanopensource.iteye.com/blog/2004853