ubuntu下配置nginx+php-fpm的file not find问题

第一种情况下sudo vim /etc/nginx/sites-enable/default下配置

1
2
3
4
5
6
7
8
9
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_read_timeout 300;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

第二种情况就是 /etc/php5/fpm/pool.d/www.conf

文件配置下user = XXX必须与你/etc/nginx/nginx.conf的配置下user =XXX 一置,而且XXX必须是有访问权限。

phpmyadmin页面提示mcrypt extension is missing错误解决

这是一个mcrypt文件位置错误的bug

详细的bug信息请参考以下网址:

https://bugs.launchpad.net/ubuntu/+source/phpmyadmin/+bug/1236035

#mv -i /etc/php5/conf.d/mcrypt.ini /etc/php5/mods-available/

#sudo php5enmod mcrypt

#/etc/init.d/apache2 restart

ubuntu下phpmyadmin显示缺少 mcrypt 扩展。请检查 PHP 配置。

更新:2014-05-24
一般来说出现这个问题是由于没有安装mcrypt和配置导致的。
ubuntu下安装:sudo apt-get install php5-mcrypt libmcrypt-dev
安装之后如果仍然显示错误,则需要配置php扩展,一般来说配置文件都是在/etc/php5/下
你会在/etc/php5/mods-available/下看到配置文件mcrypt.ini,也可能在/etc/php5/conf.d/下,这时你只要将它链接到/etc/php5/fpm/conf.d/下就可以(cli和cgi类似就行)
命令:ln -s /etc/php5/mods-available/mcrypt.ini /etc/php5/fpm/conf.d/20-mcrypt.ini

多进程写文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 多进程写文件
function write_file($filename, $content)
{

$lock = $filename . '.lck';//与文件名关联的加锁文件
$write_length = 0;
while(true) {
if( file_exists($lock) ) {//判断是否已存在该锁,存在则证明已有进程在写文件,休眠100毫秒
usleep(100);//休眠100毫秒
} else {
touch($lock);//加锁
$write_length = file_put_contents($filename, $content, FILE_APPEND);//写文件
break;//退出循环
}
}
if( file_exists($lock) ) {
unlink($lock);//解锁
}
return $write_length;
}

apache rewriteEngine开启后出现403

apache伪静态URL配置RewriteEngine on出现403错误

解决方案:
1.在apache配置文件httpd.conf中,修改两项配置: Options FollowSymLinks、AllowOverride All。
2.把Options Indexes注释掉,也就是前面加个“#”,变成#Options Indexes

80端口被PID为4的系统进程所占用

装apache的时候80端口被PID为4的系统进程所占用 由于工作中需要直接从MySQL后台读取数据,所以安装了PHPnow,装的过程中提示Apache安装失败,80端口被占用。 在cmd中输入netstat –ano命令,发现80端口被一个PID为4的服务所占用,打开任务管理器,发现PID为4的进程为系统进程,其描述信息为NT Kernel & System,在服务里面又找不到PID为4的服务,所以没法停掉服务。 这种情况基本上就是谷歌和度娘的事了,搜出来的结果大部分不靠谱,要么说迅雷等下载软件会占用80端口,要么说IIS占用了该端口。事实上,我机器上这些服务都没启动。那么究竟是什么原因呢?还好,偶然间看到一个页面说MS SQL Server的报表服务会占用80端口(写日志的时候已经找不到链接了,恕未引用),于是打开windows服务,找到SQL Server Reporting Services (MYSQLSERVER),把它停掉,再安装PHPnow,发现果然能顺利安装。 以后遇到类似问题,关掉SQL Server Reporting Services (MYSQLSERVER)也可以作为一种尝试了。

一些查看端口占用和关闭端口的命令,已443端口为例

1
2
3
4
5
6
netstat -ano|findstr "443" //搜索443端口占用情况,并找到进程ID
TCP 0.0.0.0:443 0.0.0.0:0 LISTENING 720 //找到PID为720
UDP 0.0.0.0:59443 *:* 7584
-----------------------------------------------
tasklist|findstr "720" //查找PID为720对应的进程名
QQDownload.exe 720 Console 1 56,156 K //找到是QQ旋风占用,结束掉进程。OK

用余弦定理实现文本相似度算法

续上一篇文章的算法,这次自己改成PHP版本,当然如果有优化的地方请指出,以便大家学习。

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
header('Content-Type: text/html; charset=UTF-8');
$text1 = <<<EOF
Fragment A (By Rousseau):
À l’instant, au lieu de la personne particulière de chaque contractant, cet acte d’association produit un Corps moral et collectif, composé d’autant de membres que l’assemblée a de voix, lequel reçoit de ce même acte son unité, son moi commun, sa vie et sa volonté. Cette personne publique, qui se forme ainsi par l’union de toutes les autres, prenait autrefois le nom de Cité , et prend maintenant celui de République ou de Corps politique: lequel est appelé par ses membres État quand il est passif, Souverain quand il est actif, Puissance en le comparant à ses semblables. À l’égard des associés, ils prennent collectivement le nom de peuple, et s’appellent en particulier citoyens, comme participant à l’autorité souveraine, et sujets, comme soumis aux lois de l’État. (Rousseau, Du contrat social, I.6)
EOF;

$text2 = <<<EOF
Fragment B (By Hobbes):
Art goes yet further, imitating that Rationall and most excellent worke of Nature, Man. For by Art is created that great LEVIATHAN called a COMMON-WEALTH, or STATE, (in latine CIVITAS) which is but an Artificiall Man; though of greater stature and strength than the Naturall, for whoseprotection and defence it was intended; and in which, the Soveraignty is an Artificiall Soul, as giving life and motion to the whole body; The Magistrates, and other Officers of Judicature and Execution, artificiall Joynts; Reward and Punishment (by which fastned to the seat of the Soveraignty, every joynt and member is moved to performe his duty) are the Nerves, that do the same in the Body Naturall; The Wealth and Riches of all the particular members, are the Strength; Salus Populi (the Peoples Safety) its Businesse; Counsellors, by whom all things needfull for it to know, are suggested unto it, are the Memory; Equity and Lawes, an artificiall Reason and Will; Concord, Health; Sedition, Sicknesse; and Civill War, Death. Lastly, the Pacts and Covenants, by which the parts of this Body Politique were at first made, set together, and united, resemble that Fiat, or the Let Us Make Man, pronounced by God in the Creation. (Hobbes, Leviathan, "Introduction")
EOF;


echo similarAlgorithm($text1 , $text2);
/*
* 文本相似度匹配
* @author juice
* @param text1 匹配文本1
* @param text2 匹配文本2
* @return double 返回相似度(余弦值)
*/

function similarAlgorithm($text1 = '' , $text2 = ''){
$charset = 'UTF-8' ;
$text1Array = mbStringToArray($text1 , $charset ,TRUE);
$text2Array = mbStringToArray($text2 , $charset ,TRUE);
$textSumArray = array();
foreach($text1Array as $key => $val){//统计该字在第一文本中出现的次数
if(isset($textSumArray[$val])){
$textSumArray[$val][0]++;
} else {
$textSumArray[$val][0] = 1;
$textSumArray[$val][1] = 0;
}
}
foreach($text2Array as $key => $val){//统计该字在第二文本中出现的次数
if(isset($textSumArray[$val])){
$textSumArray[$val][1]++;
}else {
$textSumArray[$val][0] = 0;
$textSumArray[$val][1] = 1;
}
}
$sqdoc1 = 0;//平方和
$sqdoc2 = 0;//平方和
$denominator = 0;
foreach($textSumArray as $key => $val){
$denominator += $val[0] * $val[1];
$sqdoc1 += $val[0] * $val[0];
$sqdoc2 += $val[1] * $val[1];
}
return $denominator / sqrt($sqdoc1*$sqdoc2);
}
/*
* 分割字符串方法 (支持中文分割)
* @author juice
* @param str 需要分割的字符串
* @param charset 字符串的编码,默认UTF-8
* @param convertedToHex 是否返回该字符的16进制,TRUE返回每个字符16进制,FALSE则返回原本字符
* @return double 返回相似度(余弦值)
*/

function mbStringToArray($str = '',$charset = 'UTF-8' , $convertedToHex = FALSE) {
$strlen = mb_strlen($str);
if( $convertedToHex === TRUE ){
while($strlen){
$array[] = bin2hex(mb_substr($str,0,1,$charset));
$str = mb_substr($str,1,$strlen,$charset);
$strlen = mb_strlen($str);
}
}else{
while($strlen){
$array[] = mb_substr($str,0,1,$charset);
$str = mb_substr($str,1,$strlen,$charset);
$strlen = mb_strlen($str);
}
}
return $array;
}
?>

php curl请求返回400 Bad Request的解决方法

1
2
3
4
5
&nbsp;
<h1>400 Bad Request</h1>
&nbsp;

<center>nginx/1.2.0</center>

当你使用php的curl方法时会返回400 Bad Request,这有如下几种原因:

1.远端有检测客户端过滤行为。
2.远端有redir行为。
3.请求中间层。
4.近端有过滤行为。
5.某个过程有缓存机制。
6.脚本文件编码问题。

前面5种都是服务器的限制,做一些伪装就可以,但是很多人会忽略掉第六个原因,这是因为Unicode、Unicode big endian和UTF-8编码的txt文件的开头会多出几个字节,分别是FF、FE(Unicode),FE、FF(Unicode big endian),EF、BB、BF(UTF-8)。一般只要换成UTF-8编码就没有可以解决了。