工作,学习,生活,这里将会有一些记录. 备用域名:http://meisw.wdlinux.cn 注册 | 登陆
浏览模式: 标准 | 列表2013年03月的文章

关于php中的magic_quotes_gpc和safe_mode

今天重新配置了下php ,查看apache log却发现如下警告信息:
PHP Warning: Directive ‘safe_mode’ is deprecated in PHP 5.3 and greater in Unknown on line 0
PHP Warning: Directive ‘magic_quotes_gpc’ is deprecated in PHP 5.3 and greater in Unknown on line 0

去官方看了下,用红色的block特别注明了,php5.3已经不推荐使用这个东东了,在 PHP6 中已经将其废弃:
magic_quotes_gpc boolean
Warning
This feature has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged.

magic_quotes_gpc=on 的配置下,插入数据时,Magic quotes 会自动将数据转义。 可以从一定程度上,让初学者带离脚本的安全风险。例如在没有任何保护措施的代码下,开启了 Magic quotes 后会少很多的风险,例如注入问题。以前看一些安全方面的文章,入侵者总是喜欢选择magic_quotes_gpc=off的站下手,如果看到其 magic_quotes_gpc=on ,估计就不会弄这个站了。
那么官方为什么要废除这个东东呢?搜索了一下,看到一个写得相当详细和全面的帖子,下面将其中提到的几个原因帖出:
为什么不使用 Magic quotes:

可移植性
无论此功能是否开启,它都会影响脚本的可移植性,因为它影响我们后续过滤数据的操作。

性能问题
在获取所有的外部数据之前都会被转义,这无疑会增加运行时的花销(而且并不是所有的数据都需要转义)。

造成困惑
正如上述所言,并非所有的数据都需要被转义。有可能出现的一种情况,就是当你为了获取未被转义的数据,而“疯狂的”使用 stripslashes 函数。

如何禁用 magic_quotes_gpc 和safe_mode :

1,用 php.ini 配置文件全局禁用
1
2

magic_quotes_gpc = Off
safe_mode = Off

2,使用 .htaccess 文件禁用(对于虚拟主机)
1
2

php_flag magic_quotes_gpc Off
php_flag safe_mode Off

禁用了这两个东东后,安全问题就更加不能忽视了。在数据入库前一定要addslashes ,出库后要记得stripslashes 。

看了下phpwind和 discuz的源码 ,发现phpwind就是直接用的addslashes和stripslashes 。而dz则是自己定义了两个函数:

function daddslashes($string, $force = 0)
{
!defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
if(!MAGIC_QUOTES_GPC || $force)
{
if(is_array($string))
{
foreach($string as $key => $val)
{
$string[$key] = daddslashes($val, $force);
}
} else
{
$string = addslashes($string);
}
}
return $string;
}

function dstripslashes($string)
{
if(is_array($string))
{
foreach($string as $key => $val)
{
$string[$key] = dstripslashes($val);
}
}
else
{
$string = stripslashes($string);
}
return $string;
}

dz的这两个函数的在原函数的基础上扩充了对数组数据的支持,用起来更方便。不过dz的这两个函数不够简洁,这里我给出两个简洁点的:

function addslashes_deep($string)
{
$string = is_array($string)?array_map('addslashes_deep', $string):addslashes($string);
return $string;  
}


function stripslashes_deep($string)
{
$string = is_array($string)?array_map('stripslashes_deep', $string):stripslashes($string);
return $string;  
}

在数据入库前和出库后都要记得:

//入库前
if(!get_magic_quotes_gpc())
{
$_GET=addslashes_deep($_GET);
$_POST=addslashes_deep($_POST);
$_REQUEST=addslashes_deep($_REQUEST);
//其它要处理的变量.......
}

//出库后
if(get_magic_quotes_gpc())
{
$_GET=stripslashes_deep($_GET);
$_POST=stripslashes_deep($_POST);
$_REQUEST=stripslashes_deep($_REQUEST);
//其它要处理的变量.......
}

PHP加速eAccelerator配置参数和版本解释

让你的php程序效率更高—eAccelerator加速

 eAccelerator是一个自由开放源码php加速器,优化和动态内容缓存,提高了php脚本的缓存性能,使得PHP脚本在编译的状态下,对服务器的开销几乎完全消除。 它还有对脚本起优化作用,以加快其执行效率。使您的PHP程序代码执效率能提高1-10倍。

 eAccelerator 0.9.5.3 for PHP 5.2.17(VC6编译):

 Thread Safe 版


 Non Thread Safe 版


 eAccelerator 0.9.6.1 for PHP 5.2.17(VC6编译):

 Thread Safe 版


 Non Thread Safe 版


 安装eAccelerator

 将php_eaccelerator.dll复制到PHP目录下的ext文件夹下,打开php.ini,在

extension=php_zip.dll

后面,添加

extension=php_eaccelerator.dll

(另起一行,不能在同一行),在

; Local Variables:
; tab-width: 4
; End:

[eaccelerator]
eaccelerator.shm_size = "64"
eaccelerator.cache_dir = "D:\php_eaccelerator"
eaccelerator.enable = "1"
eaccelerator.optimizer = "1"
eaccelerator.check_mtime = "1"
eaccelerator.debug = "0"
eaccelerator.filter = ""
eaccelerator.shm_max = "0"
eaccelerator.shm_ttl = "300"
eaccelerator.shm_prune_period = "600"
eaccelerator.shm_only = "0"
eaccelerator.compress = "1"
eaccelerator.compress_level = "9"
celerator.log_file = "D:\php_eaccelerator_log"
eaccelerator.keys = "shm_and_disk"
eaccelerator.session = "shm_and_disk"
eaccelerator.content = "shm_and_disk"
eaccelerator.allowed_admin_path = "D:\wwwroot\test\eaccelerator"

eaccelerator.shm_size:用来设置分配给eAccelerator用来缓存php的最大共享内存,单位是mb,如果设置为0,就使用默认大小。在linux系统中一个process能分配的最大内存是由/proc/sys/kernel/shmmax限制的,所以如果eA设置的内存超出了这个值,eA在初始化的时候会失败。shmmax的单位是字节(bytes)。
eaccelerator.cache_dir:用来设置硬盘缓存目录。eA用来存放预编译代码,session数据,内容和用户入口。默认值是"/tmp/eaccelerator"。
eaccelerator.enable:用来设置是否启用或禁用eAccelerator,设置1为启用,设置0为禁用。
eaccelerator.optimizer:开启或关闭优化,用户加速代码的执行。1为开启,0为关闭,优化仅仅在脚本被编译时候发生并且是在被缓存之前。
eaccelerator.check_mtime:在每次命中的时候Eaccelerator都会检查脚本的修改时间来判断是不是脚本发生的变化来决定是否需要重新编译。尽管检查比打开文件并编译要快,但仍然会带来一些开销的,因为每次都会有状态调用要完成。这个配置可以关闭这个检查。不过关闭检查带来的不利就是每次更新了文件需要认为手动的清除cache。默认检查是启用的,1是启用,0是禁用。
eaccelerator.debug:开启关闭debug日志。如果设置为1,将打印很多文件命中的信息到日志中。这个在排查eAccelerator的时候很有用。
eaccelerator.filter:用来决定哪个php文件被缓存。可以通过使用通配符(比如"*.php *.phtml")来匹配需要缓存的php脚本。如果以"!"开头,表示不匹配,这个参数默认是空,可以缓存所有编译好的php脚本需要注意的是eaccelerator.filter并不是基于URL工作的,而是基于文件的绝对路径,比如定义了"!/home"的话,那所有/home目录的脚本都不会被缓存。如果要定义多个匹配,使用空格或者制表符分开,而不是逗号。
eaccelerator.shm_max:设置内存缓存可以缓存文件的最大值。
eaccelerator.shm_ttl:当eAcelerator没有空余的共享内存的时候,就会把最少shm_ttl设置的秒数没访问过的脚本从缓存中释放掉,默认值是0,表示eA不会释放任何缓存。
eaccelerator.shm_prune_period:当没有足够的内存用来缓存的时候,eA会在上次清除缓存的时间超过了shm_prune_period设定的秒数时再次尝试清除过期的脚本。默认值为0,eA将不从缓存中清除过期的脚本。
eaccelerator.shm_only:启用或者禁用磁盘缓存。这个选项对session数据和内容的缓存没效果。默认值是0,允许eA使用磁盘和内存进行缓存。
eaccelerator.compress:当使用eaccelerator_content_* 的api时,eA可以在缓存前对内容进行压缩。默认值为1表示启用,禁用为0。
eaccelerator.compress_level:内容缓存的压缩等级。默认值为9,是最大压缩级别。
eaccelerator.log_file:设置eaccelerator日志的存放目录。如果这选项没有配置,则数据都被记录到stderr,如果使用php的是apache,则数据都被记录到apache的error log中。
eaccelerator.keys | session | content:这些设置控制着eA缓存用户内容的存放地点。可用的值有shm_and_disk 使用内存和磁盘进行缓存(默认值)shm 在内存中缓存数据,当内存满了或者数据大小超过了eaccelerator.shm_max设置的值就缓存到磁盘上shm_only 只使用内存缓存数据disk_only 只使用磁盘缓存数据none 不缓存数据。
eaccelerator.allowed_admin_path:控制面板所在目录,比如你的网站目录为D:\wwwroot\test\,可以建立一个文件夹eaccelerator,将control.php复制进里面,这里的值就为D:\wwwroot\test\eaccelerator
   保存php.ini后,重启IIS,查看你的phpinfo
 with eAccelerator v0.9.5.3, Copyright (c) 2004-2006 eAccelerator, by eAccelerator
 复制代码

 这就是eAccelerator加载成功了。

 开启你网站程序的eAccelerator功能
   eAccelerator配置好后,会自动对php程序进行加速。但是,有的php程序可以主动调用eaccelerator的一些缓存功能,让程序运行更快,比如:discuz,将config目录下config_global.php里的$_config['memory']['eaccelerator']设置为 1 即可。

$_config['memory']['eaccelerator'] = '1';

至此,已经全部配置完毕。

 PS:eAccelerator为啥不用最新的0.9.6.1呢,因为,0.9.6开始,就取消了user cache功能,也就是步骤四中所说,php程序主动调用eAccelerator的缓存功能。0.9.5的最后版本就是0.9.5.3。

CentOS6.3关闭ipv6及tty

http://xlogin.blog.51cto.com/3473583/1031311

mysql 5.5安装提示ncurses问题

安装mysql5.5.17的时候报错如下:
-- Could NOT find OpenSSL (missing: OPENSSL_LIBRARIES OPENSSL_INCLUDE_DIR) 
-- Could NOT find Curses (missing: CURSES_LIBRARY CURSES_INCLUDE_PATH) 
CMake Error at cmake/readline.cmake:83 (MESSAGE):
Curses library not found. Please install appropriate package,

remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.
Call Stack (most recent call first):
cmake/readline.cmake:118 (FIND_CURSES)
cmake/readline.cmake:214 (MYSQL_USE_BUNDLED_READLINE)
CMakeLists.txt:257 (MYSQL_CHECK_READLINE)


-- Configuring incomplete, errors occurred!


解决办法:
[root@vps mysql-5.5.17]# rm -f CMakeCache.txt
[root@vps mysql-5.5.17]# yum -y install ncurses-devel

Nginx 多核cpu 优化:(Core) + worker_processes (worker_cpu_affinity)

  1. 配置14 CPU (4 Core) + 4 worker_processes (每个worker_processes 使用1个CPU)   
  2. [reistlin@reistlin.com ~]$ cat /proc/cpuinfo | grep processor   
  3. processor       : 0  
  4. processor       : 1  
  5. processor       : 2  
  6. processor       : 3  
  7. worker_processes 4;   
  8. worker_cpu_affinity 0001 0010 0100 1000;   
  9. 配置28 CPU (8 Core) + 8 worker_processes (每个worker_processes 使用1个CPU)   
  10. [reistlin@reistlin.com ~]$ cat /proc/cpuinfo | grep processor   
  11. processor       : 0  
  12. processor       : 1  
  13. processor       : 2  
  14. processor       : 3  
  15. processor       : 4  
  16. processor       : 5  
  17. processor       : 6  
  18. processor       : 7  
  19. worker_processes 8;   
  20. worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;   
  21. 配置316 CPU (16 Core) + 16 worker_processes (每个worker_processes 使用1个CPU)   
  22. [reistlin@reistlin.com ~]$ cat /proc/cpuinfo | grep processor   
  23. processor       : 0  
  24. processor       : 1  
  25. processor       : 2  
  26. processor       : 3  
  27. processor       : 4  
  28. processor       : 5  
  29. processor       : 6  
  30. processor       : 7  
  31. processor       : 8  
  32. processor       : 9  
  33. processor       : 10  
  34. processor       : 11  
  35. processor       : 12  
  36. processor       : 13  
  37. processor       : 14  
  38. processor       : 15  
  39. worker_processes 16;   
  40. worker_cpu_affinity 0000000000000001 0000000000000010 0000000000000100 0000000000001000 0000000000010000 0000000000100000 0000000001000000 0000000010000000 0000000100000000 0000001000000000 0000010000000000 0000100000000000 0001000000000000 0010000000000000 0100000000000000 1000000000000000 

linux csf 防火墙 防止少量的ddos cc攻击很有效

http://blog.51yip.com/server/1503.html

Incorrect key file for table mysql表损坏的修复方法

今天查询mysql的时候,报这样的错误,Incorrect key file for table './tg/dxad.MYI'; try to repair it. mysql表损坏的情况是很少见的,下面的方法适用于myisam,其他存储引擎,不知道能不能这样修复。
1,myisamchk修改表
查看复制打印?
[root@localhost tg]# myisamchk -of ./dxad.MYI     //修复第一步 
- recovering (with keycache) MyISAM-table './dxad.MYI' 
Data records: 12597637 
Found block that points outside data file at 1630252996 
Data records: 12597456 
[root@localhost tg]# myisamchk -r ./dxad.MYI     //修复第二步 
- recovering (with sort) MyISAM-table './dxad.MYI' 
Data records: 12597456 
- Fixing index 1 
[root@localhost tg]# myisamchk ./dxad.MYI     //修复第三步 
 
Checking MyISAM file: ./dxad.MYI 
Data records: 12597456   Deleted blocks:       0 
- check file-size 
- check record delete-chain 
- check key delete-chain 
- check index reference 
- check data record references index: 1 
- check record links 
myisamchk带的参数,可以用man看一下。操作后重新启动一下数据库。
[root@localhost tg]# /etc/init.d/mysqld restart 
这样操作后,还是有问题,会报 #145 - Table "XXXXX" is marked as crashed and should be repaired。如下图:

myisamchk 修复表后报的错误
2,命令行下repair修复表
查看复制打印?
mysql> repair table dxad;  //dxad是表名 
如下图

命令行下repair修复表成功
到这儿,myisam表损坏就修复好了。

MySQL MyISAM 表转换为InnoDB的方法

1.备份数据库: mysqldump -u[user] -p[password] [databasename] > [dbfile.sql] # 备份数据库。
2. /usr/local/mysql/bin/mysqladmin -u root -p shutdown # 停止数据库 或者 service mysql stop。
3. InnoDB 表不支持全文搜索(fulltext search),那么,记得要将备份出来的数据库sql,删掉有关 Fulltext 的索引。
4. cd /usr/local/mysql/support-files/ 找寻适合主机内存的设定文件,必将设定文件拷贝到 /etc/my.cnf。
5. vi /etc/my.cnf ,将以下几项批注取消掉。以 my-large.cnf 为例。

  innodb_data_file_path = ibdata1:10M:autoextend
innodb_buffer_pool_size = 256M
innodb_additional_mem_pool_size = 20M
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
    加上 default-storage-engine=innodb 
加上这段之后,以后新增的数据表型态都即是 InnoDB,不然每次新增一次数据表,SQL 后面得加上 ENGINE=InnoDB;

6 .将刚刚备份出来的sql,将ENGINE=MyISAM改成ENGINE=InnoDB。
7. /usr/local/mysql/bin/safe_mysqld --user=mysql &  ,或service mysql start 启动数据库
8. 建立一个新的数据库(数据库名称跟备份出来的数据库名称一样)。
9. mysql -u[user] -p[password] [database_name] < [dbfile] # 将改好的数据汇入数据库中!

说明:
* 设定文件的选择是参照内存大小来选择。
my-huge.cnf - 1G~2G 、my-large.cnf - 512M 、 my-medium.cnf - 32M - 64M 、my-small.cnf <= 64M 。
InnoDB:my-innodb-heavy-4G.cnf
* 假如不会将备份出来的数据库改型态,那么您可以用下面这个指令,直接改变数据表的型态。

 ALTER TABLE [tablename] ENGINE=InnoDB 如有存放全文索引功能的话,转换会失败的。

* 如你有一批数据表要改,可以用下面的指令:
 mysql_convert_table_format [opt] -- ENGINE=InnoDB dbname [tablename]
但千万注意不要改变 mysql 数据库的数据型态,因为 mysql数据库存放的是 MySQL 内部的管理信息,所以必须保持 MyISAM 的格式。

* 加大 tablespace 空间:
innodb_data_file_path = ibdata1:1G;ibdata2:1G:autoextend:max2G

 上面的意思是,tablespace 包含 ibdata1 & ibdata2 两个文件,若文件不存在,则建立容量各为1G的文件。一旦未来 InnoDB 需要,更多的空间,则 ibdata2 将每次自动增加 8MB,直到2G为止。

 * MySQL 3.23.n,innodb_data_home & innodb_data_file_path设定是必须要有的,MySQL 4.0.0 之后的版本则是非必须的。

Records:281234