Python 强大的模式匹配

一些很 sb 的正则需求, 对子模式要求苛刻
不同情况的子模式顺序不同
这就要用到命名的子模式了

1
2
3
4
5
6
7
8
9
import re

regex = re.match(r'(?P<first>\d+)qwe(?P<last>\d+)', '123qwe321')

print(regex.group(1))
print(regex.group('first'))
print(regex.group(2))
print(regex.group('last'))

1
2
3
4
5
6
7
8
C:\Python34\python.exe E:/python/tmp.py
123
123
321
321

Process finished with exit code 0

Nginx 配置笔记

引子

本篇记录学习 nginx 的点滴

主要配置

整合 ThinkPHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
vim nginx.conf

location / {
root /var/www/html;
index index.html index.php;
# ThinkPHP hide index.php
try_files $uri $uri/ /index.php?s=$uri&$args;
}

location ~ \.php {
root /var/www/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi.conf;
}

设置 HTTPS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
ssl_certificate server.crt; # 注意 bundle.crt
ssl_certificate_key server.key;

# 强制 HTTPS

server {
listen 80;
rewrite ^(.*)$ https://$host$1 permanent;
}

server {
listen 443;
# blahblah....
}

参考资料

https://s.how/nginx-ssl/

Ubuntu14.04 导入第三方源安装 LNMP 堆栈

引子

编译安装的缺点是, 操作麻烦, 针对不同衍生版本有不同的操作.
本篇记录在 Ubuntu 下通过导入第三方源来安装 LAMP 堆栈.

MariaDB
https://downloads.mariadb.org/mariadb/repositories/#mirror=tuna&distro=Ubuntu&distro_release=trusty--ubuntu_trusty&version=10.1

1
2
3
4
5
6
apt-get install software-properties-common
apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
add-apt-repository 'deb [arch=amd64,i386,ppc64el] http://mirrors.tuna.tsinghua.edu.cn/mariadb/repo/10.1/ubuntu trusty main'

apt-get update
apt-get install mariadb-server

PHP
https://launchpad.net/~ondrej/+archive/ubuntu/php

1
2
3
4
5
6
add-apt-repository ppa:ondrej/php

apt-get update
apt-get install php5.6 php5.6-mysql php5.6-mbstring php5.6-curl php5.6-gd php5.6-fpm

apt-get install php7.1 php7.1-mysql php7.1-mbstring php7.1-curl php7.1-gd php7.1-fpm

nginx

1
2
3
4
add-apt-repository ppa:nginx/stable

apt-get update
apt-get install nginx

附几个国内镜像源

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
vim /etc/apt/sources.list

# AliYun
deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse

# Tsinghua
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty main multiverse restricted universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-backports main multiverse restricted universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-proposed main multiverse restricted universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-security main multiverse restricted universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-updates main multiverse restricted universe
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty main multiverse restricted universe
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-backports main multiverse restricted universe
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-proposed main multiverse restricted universe
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-security main multiverse restricted universe
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-updates main multiverse restricted universe

微信支付那些事

引子

客户需求, 要做微信支付, 之前没有接触过支付接口, 想着不就是调用 API 嘛, 能有多难, 没想到还真的有很多坑, 算是在这里总结吐槽一下吧.

这里暂时不提前辈们讨论过的一些坑, 单纯的只说说我自己遇到的乡村野坑

支付目录

一些带有灵活路由的框架例如 thinkPHP, 在做微信支付的时候, 设置支付授权目录的时候一定要写完整的路由 uri, 不能落下入口文件(通常是 index.php)
我在配置的时候没有写 index.php, 因为我的服务器配置了 Apache 的 rewrite, 可是没想到微信这里还是要求很严格的需要完整的 uri

学到的东西

从这些坑爬出来之后, 不禁反思, 能从中学到什么知识呢

  • jQuery 的 ajax 返回值默认是 string 类型, 除非指定 dataType 为 json, 才会被 js 解析为 json 对象. 这一点我在一开始没有注意, 被恶狠狠的坑了一把.

  • 微信内置浏览器不会自动更新文件缓存, 这意味着如果你修改了 css 或者 js 文件, 需要清理微信缓存才能生效(debugx5.qq.com), 如果是生产环境则需要在引用的时候加上时间戳以强制微信浏览器重新加载此文件

Ubuntu 14.04 编译安装 Tengine + PHP5.6

引子

本篇从头开始讲解Ubuntu下编译安装 Tengine 和 PHP

确保已经安装编译器!

1
apt-get install build-essential -y

懒人专用

1
2
3
4
5
6
7
apt-get install gcc make build-essential libxml2 libxml2-dev openssl bzip2 libbz2-dev curl libpng12-dev libmcrypt-dev -y
apt-get install libcurl4-gnutls-dev -y
apt-get install libcurl4-openssl-dev -y

wget "http://tengine.taobao.org/download/tengine-2.1.2.tar.gz" "ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz" "http://cn2.php.net/distributions/php-5.6.23.tar.gz"

for tar in *.tar.gz; do tar -zxvf $tar; done

编译 Tengine

ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz
http://tengine.taobao.org/download/tengine-2.1.2.tar.gz

1
2
3
4
5
6
# 安装依赖
apt-get install openssl libssl-dev -y

# 编译
./configure --with-pcre=/path/to/pcre-8.39
make && make install

编译 PHP

http://cn2.php.net/distributions/php-5.6.23.tar.gz

1
2
3
4
5
6
7
8
9
10
# 安装依赖
apt-get install libxml2 libxml2-dev bzip2 libbz2-dev curl libpng12-dev libmcrypt-dev -y
apt-get install libcurl4-gnutls-dev -y
apt-get install libcurl4-openssl-dev -y

# 编译
./configure --prefix=/usr/local/php --enable-fpm --with-config-file-path=/usr/local/php/etc --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-gettext --enable-mbstring --enable-exif --with-iconv --with-mcrypt --with-mhash --with-openssl --enable-bcmath --enable-soap --with-libxml-dir --enable-pcntl --enable-shmop --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-sockets --with-curl --with-gd --with-zlib --enable-zip --with-bz2 --without-sqlite3 --without-pdo-sqlite --with-pear --enable-opcache --with-fpm-group=www-data --with-fpm-user=www-data

make && make install

安装完毕之后, 开始配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 配置php-fpm
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

# 整合nginx+php
vim nginx.conf

location / {
root /var/www/html;
index index.html index.php;
}

location ~ \.php$ {
root /var/www/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $DOCUMENT_ROOT$fastcgi_script_name;
include fastcgi_params;
}

MySQL 相关

使用apt-get install mysql-server 之后, 发觉mysql_connect()这类函数会报错, 提示

mysql_connect(): [2002] No such file or directory

这是表示没有找到mysql.sock文件, 只需去mysql的my.cnf中找一下真实路径, 然后做一下软连接即可, 也可能是mysqld.sock.

ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock

这个是4.6版本, 仅支持5.5以及以上的 mysql :
https://files.phpmyadmin.net/phpMyAdmin/4.6.3/phpMyAdmin-4.6.3-all-languages.tar.gz

另外, 如果使用 apt-get 安装的 mysql5.6, 则需要确定机器内存大于 2GB
或者使用 swap, 否则会安装失败

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
On Ubuntu 14.04, I do the following to solve the problem:

Create a 4G swap file:
sudo fallocate -l 4G /swapfile

Change its permission to only root could access and change:
sudo chmod 600 /swapfile

Make it swap:
sudo mkswap /swapfile

Activate:
sudo swapon /swapfile

Now you can try install mysql again, it should success this time. Just remember to remove the previous unsuccessful installation before you do so.

OPcache

需要如下配置才能启用 OPcache :

1
2
3
4
5
6
7
8
9
10
vim php.ini

zend_extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/opcache.so

opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=5000
opcache.revalidate_freq=60
opcache.fast_shutdown=1

整合ThinkPHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
vim nginx.conf

location / {
root /var/www/html;
index index.html index.php;
# ThinkPHP hide index.php
try_files $uri $uri/ /index.php?s=$uri&$args;
}

location ~ \.php {
root /var/www/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
set $path_info "";
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
}

参考资料

http://www.yanshiba.com/archives/727
http://php.net/manual/zh/opcache.installation.php
http://www.th7.cn/system/lin/201410/74518.shtml
http://havee.me/internet/2014-04/nginx-gzip-compression.html
http://blog.csdn.net/tinico/article/details/18033573
http://www.jb51.net/article/82276.htm
http://askubuntu.com/questions/457923/why-did-installation-of-mysql-5-6-on-ubuntu-14-04-fail